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; }
this.networkListener = new DefaultMessageListener(labeler, builder.listener); Singleton.initialize(cryptography);
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; } }
/** * @param k a private key, typically should be 32 bytes long * @return an InputStream yielding the decrypted data * @throws DecryptionFailedException if the payload can't be decrypted using this private key * @see <a href='https://bitmessage.org/wiki/Encryption#Decryption'>https://bitmessage.org/wiki/Encryption#Decryption</a> */ public InputStream decrypt(byte[] k) throws DecryptionFailedException { // 1. The private key used to decrypt is called k. // 2. Do an EC point multiply with private key k and public key R. This gives you public key P. byte[] P = cryptography().multiply(R, k); // 3. Use the X component of public key P and calculate the SHA512 hash H. byte[] H = cryptography().sha512(Arrays.copyOfRange(P, 1, 33)); // 4. 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); // 5. Calculate MAC' with HMACSHA256, using key_m as salt and IV + R + cipher text as data. // 6. Compare MAC with MAC'. If not equal, decryption will fail. if (!Arrays.equals(mac, calculateMac(key_m))) { throw new DecryptionFailedException(); } // 7. Decrypt the cipher text with AES-256-CBC, using IV as initialization vector, key_e as decryption key // and the cipher text as payload. The output is the padded input text. return new ByteArrayInputStream(cryptography().crypt(false, encrypted, key_e, initializationVector)); }
public byte[] getRipe() { return cryptography().ripemd160(cryptography().sha512(getSigningKey(), getEncryptionKey())); }
BitmessageAddress(long version, long stream, byte[] ripe) { try { this.version = version; this.stream = stream; this.ripe = ripe; ByteArrayOutputStream os = new ByteArrayOutputStream(); Encode.varInt(version, os); Encode.varInt(stream, os); if (version < 4) { byte[] checksum = cryptography().sha512(os.toByteArray(), ripe); this.tag = null; this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } else { // for tag and decryption key, the checksum has to be created with 0x00 padding byte[] checksum = cryptography().doubleSha512(os.toByteArray(), ripe); this.tag = Arrays.copyOfRange(checksum, 32, 64); this.publicDecryptionKey = Arrays.copyOfRange(checksum, 0, 32); } // but for the address and its checksum they need to be stripped int offset = Bytes.numberOfLeadingZeros(ripe); os.write(ripe, offset, ripe.length - offset); byte[] checksum = cryptography().doubleSha512(os.toByteArray()); os.write(checksum, 0, 4); this.address = "BM-" + Base58.encode(os.toByteArray()); } catch (IOException e) { throw new ApplicationException(e); } }
@Override public void putObject(Item item) { try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement("INSERT INTO POW (initial_hash, data, version, " + "nonce_trials_per_byte, extra_bytes, expiration_time, message_id) " + "VALUES (?, ?, ?, ?, ?, ?, ?)") ) { ps.setBytes(1, cryptography().getInitialHash(item.object)); writeBlob(ps, 2, item.object); ps.setLong(3, item.object.getVersion()); ps.setLong(4, item.nonceTrialsPerByte); ps.setLong(5, item.extraBytes); if (item.message == null) { ps.setObject(6, null); ps.setObject(7, null); } else { ps.setLong(6, item.expirationTime); ps.setLong(7, (Long) item.message.getId()); } ps.executeUpdate(); } catch (IOException | SQLException e) { LOG.debug("Error storing object of type " + item.object.getPayload().getClass().getSimpleName(), e); throw new ApplicationException(e); } }
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) ); }