private static boolean testChecksum(byte[] checksum, byte[] payload) { byte[] payloadChecksum = cryptography().sha512(payload); for (int i = 0; i < checksum.length; i++) { if (checksum[i] != payloadChecksum[i]) { return false; } } return true; }
private boolean testChecksum(ByteBuffer buffer) { byte[] payloadChecksum = cryptography().sha512(buffer.array(), buffer.arrayOffset() + buffer.position(), length); for (int i = 0; i < checksum.length; i++) { if (checksum[i] != payloadChecksum[i]) { return false; } } return true; }
/** * First 4 bytes of sha512(payload) */ private byte[] getChecksum(byte[] bytes) throws NoSuchProviderException, NoSuchAlgorithmException { byte[] d = cryptography().sha512(bytes); return new byte[]{d[0], d[1], d[2], d[3]}; }
private CryptoBox(Builder builder) { initializationVector = builder.initializationVector; curveType = builder.curveType; R = cryptography().createPoint(builder.xComponent, builder.yComponent); encrypted = builder.encrypted; mac = builder.mac; }
public static byte[] getRipe(byte[] publicSigningKey, byte[] publicEncryptionKey) { return cryptography().ripemd160(cryptography().sha512(publicSigningKey, publicEncryptionKey)); }
private String exportSecret(byte[] privateKey) { if (privateKey.length != PRIVATE_KEY_SIZE) { throw new IllegalArgumentException("Private key of length 32 expected, but was " + privateKey.length); } byte[] result = new byte[37]; result[0] = (byte) 0x80; System.arraycopy(privateKey, 0, result, 1, PRIVATE_KEY_SIZE); byte[] hash = cryptography().doubleSha256(result, PRIVATE_KEY_SIZE + 1); System.arraycopy(hash, 0, result, PRIVATE_KEY_SIZE + 1, 4); return Base58.encode(result); }
private byte[] getSecret(String walletImportFormat) throws IOException { byte[] bytes = Base58.decode(walletImportFormat); if (bytes[0] != WIF_FIRST_BYTE) throw new IOException("Unknown format: 0x80 expected as first byte, but secret " + walletImportFormat + " was " + bytes[0]); if (bytes.length != WIF_SECRET_LENGTH) throw new IOException("Unknown format: " + WIF_SECRET_LENGTH + " bytes expected, but secret " + walletImportFormat + " was " + bytes.length + " long"); byte[] hash = cryptography().doubleSha256(bytes, 33); for (int i = 0; i < 4; i++) { if (hash[i] != bytes[33 + i]) throw new IOException("Hash check failed for secret " + walletImportFormat); } return Arrays.copyOfRange(bytes, 1, 33); }
public PrivateKey(boolean shorter, long stream, long nonceTrialsPerByte, long extraBytes, Pubkey.Feature... features) { byte[] privSK; byte[] pubSK; byte[] privEK; byte[] pubEK; byte[] ripe; do { privSK = cryptography().randomBytes(PRIVATE_KEY_SIZE); privEK = cryptography().randomBytes(PRIVATE_KEY_SIZE); pubSK = cryptography().createPublicKey(privSK); pubEK = cryptography().createPublicKey(privEK); ripe = Pubkey.getRipe(pubSK, pubEK); } while (ripe[0] != 0 || (shorter && ripe[1] != 0)); this.privateSigningKey = privSK; this.privateEncryptionKey = privEK; this.pubkey = cryptography().createPubkey(Pubkey.LATEST_VERSION, stream, privateSigningKey, privateEncryptionKey, nonceTrialsPerByte, extraBytes, features); }
public static byte[] calculateTag(long version, long stream, byte[] ripe) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); Encode.varInt(version, out); Encode.varInt(stream, out); out.write(ripe); return Arrays.copyOfRange(cryptography().doubleSha512(out.toByteArray()), 32, 64); } catch (IOException e) { throw new ApplicationException(e); } }
@Override public void setContext(InternalContext ctx) { this.ctx = ctx; this.cryptography = cryptography(); this.powRepo = ctx.getProofOfWorkRepository(); this.messageRepo = ctx.getMessageRepository(); } }
private byte[] calculateMac(byte[] key_m) { try { ByteArrayOutputStream macData = new ByteArrayOutputStream(); writeWithoutMAC(macData); return cryptography().mac(key_m, macData.toByteArray()); } catch (IOException e) { throw new ApplicationException(e); } }
public CryptoBox(byte[] data, byte[] K) throws IOException { curveType = 0x02CA; // 1. The destination public key is called K. // 2. Generate 16 random bytes using a secure random number generator. Call them IV. initializationVector = cryptography().randomBytes(16); // 3. Generate a new random EC key pair with private key called r and public key called R. byte[] r = cryptography().randomBytes(PRIVATE_KEY_SIZE); R = cryptography().createPublicKey(r); // 4. Do an EC point multiply with public key K and private key r. This gives you public key P. byte[] P = cryptography().multiply(K, r); byte[] X = Points.getX(P); // 5. Use the X component of public key P and calculate the SHA512 hash H. byte[] H = cryptography().sha512(X); // 6. The first 32 bytes of H are called key_e and the last 32 bytes are called key_m. byte[] key_e = Arrays.copyOfRange(H, 0, 32); byte[] key_m = Arrays.copyOfRange(H, 32, 64); // 7. Pad the input text to a multiple of 16 bytes, in accordance to PKCS7. // 8. Encrypt the data with AES-256-CBC, using IV as initialization vector, key_e as encryption key and the padded input text as payload. Call the output cipher text. encrypted = cryptography().crypt(true, data, key_e, initializationVector); // 9. Calculate a 32 byte MAC with HMACSHA256, using key_m as salt and IV + R + cipher text as data. Call the output MAC. mac = calculateMac(key_m); // The resulting data is: IV + R + cipher text + MAC }
Builder generate() { long signingKeyNonce = nextNonce; long encryptionKeyNonce = nextNonce + 1; byte[] ripe; do { privEK = Bytes.truncate(cryptography().sha512(seed, Encode.varInt(encryptionKeyNonce)), 32); privSK = Bytes.truncate(cryptography().sha512(seed, Encode.varInt(signingKeyNonce)), 32); pubSK = cryptography().createPublicKey(privSK); pubEK = cryptography().createPublicKey(privEK); ripe = cryptography().ripemd160(cryptography().sha512(pubSK, pubEK)); signingKeyNonce += 2; encryptionKeyNonce += 2; } while (ripe[0] != 0 || (shorter && ripe[1] != 0)); nextNonce = signingKeyNonce; return this; } }
public byte[] getRipe() { return cryptography().ripemd160(cryptography().sha512(getSigningKey(), getEncryptionKey())); }
public boolean isSignatureValid(Pubkey pubkey) throws IOException { if (isEncrypted()) throw new IllegalStateException("Payload must be decrypted first"); return cryptography().isSignatureValid(getBytesToSign(), payload.getSignature(), pubkey); }
public InventoryVector getInventoryVector() { return new InventoryVector( Bytes.truncate(cryptography().doubleSha512(nonce, getPayloadBytesWithoutNonce()), 32) ); }
public void sign(PrivateKey key) { if (payload.isSigned()) { payload.setSignature(cryptography().getSignature(getBytesToSign(), key)); } }
public void encrypt() throws IOException { encrypt(cryptography().createPublicKey(plaintext.getFrom().getPublicDecryptionKey())); }
public static BitmessageAddress createIdentityFromPrivateKey(String address, byte[] privateSigningKey, byte[] privateEncryptionKey, long nonceTrialsPerByte, long extraBytes, int behaviourBitfield) { BitmessageAddress temp = new BitmessageAddress(address); PrivateKey privateKey = new PrivateKey(privateSigningKey, privateEncryptionKey, createPubkey(temp.getVersion(), temp.getStream(), cryptography().createPublicKey(privateSigningKey), cryptography().createPublicKey(privateEncryptionKey), nonceTrialsPerByte, extraBytes, behaviourBitfield)); BitmessageAddress result = new BitmessageAddress(privateKey); if (!result.getAddress().equals(address)) { throw new IllegalArgumentException("Address not matching private key. Address: " + address + "; Address derived from private key: " + result.getAddress()); } return result; }
public Plaintext build() { if (from == null) { from = new BitmessageAddress(Factory.createPubkey( addressVersion, stream, publicSigningKey, publicEncryptionKey, nonceTrialsPerByte, extraBytes, behaviorBitfield )); } if (to == null && type != Type.BROADCAST && destinationRipe != null) { to = new BitmessageAddress(0, 0, destinationRipe); } if (type == Type.MSG && ackMessage == null && ackData == null) { ackData = cryptography().randomBytes(Msg.ACK_LENGTH); } if (ttl <= 0) { ttl = TTL.msg(); } return new Plaintext(this); } }