/** * Keccak-256 hash function. * * @param input binary encoded input data * @return hash value */ public static byte[] sha3(byte[] input) { return sha3(input, 0, input.length); }
public static byte calculateChecksum(byte[] initialEntropy) { int ent = initialEntropy.length * 8; byte mask = (byte) (0xff << 8 - ent / 32); byte[] bytes = sha256(initialEntropy); return (byte) (bytes[0] & mask); }
/** * Checksum address encoding as per * <a href="https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md">EIP-55</a>. * * @param address a valid hex encoded address * @return hex encoded checksum address */ public static String toChecksumAddress(String address) { String lowercaseAddress = Numeric.cleanHexPrefix(address).toLowerCase(); String addressHash = Numeric.cleanHexPrefix(Hash.sha3String(lowercaseAddress)); StringBuilder result = new StringBuilder(lowercaseAddress.length() + 2); result.append("0x"); for (int i = 0; i < lowercaseAddress.length(); i++) { if (Integer.parseInt(String.valueOf(addressHash.charAt(i)), 16) >= 8) { result.append(String.valueOf(lowercaseAddress.charAt(i)).toUpperCase()); } else { result.append(lowercaseAddress.charAt(i)); } } return result.toString(); }
public static Bip32ECKeyPair generateKeyPair(byte[] seed) { byte[] i = hmacSha512("Bitcoin seed".getBytes(), seed); byte[] il = Arrays.copyOfRange(i, 0, 32); byte[] ir = Arrays.copyOfRange(i, 32, 64); Arrays.fill(i, (byte) 0); Bip32ECKeyPair keypair = Bip32ECKeyPair.create(il, ir); Arrays.fill(il, (byte) 0); Arrays.fill(ir, (byte) 0); return keypair; }
private byte[] getIdentifier() { return sha256hash160(getPublicKeyPoint().getEncoded(true)); }
data.put(parentPublicKey); data.putInt(childNumber); byte[] i = hmacSha512(getChainCode(), data.array()); byte[] il = Arrays.copyOfRange(i, 0, 32); byte[] chainCode = Arrays.copyOfRange(i, 32, 64); byte[] i = hmacSha512(getChainCode(), data.array()); byte[] il = Arrays.copyOfRange(i, 0, 32); byte[] chainCode = Arrays.copyOfRange(i, 32, 64);
private byte[] getIdentifier() { return sha256hash160(getPublicKeyPoint().getEncoded(true)); }
public static byte[] getAddress(byte[] publicKey) { byte[] hash = Hash.sha3(publicKey); return Arrays.copyOfRange(hash, hash.length - 20, hash.length); // right most 160 bits }
@Test public void testSha3String() { assertThat(Hash.sha3String(""), is("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); assertThat(Hash.sha3String("EVWithdraw(address,uint256,bytes32)"), is("0x953d0c27f84a9649b0e121099ffa9aeb7ed83e65eaed41d3627f895790c72d41")); }
public static Bip32ECKeyPair generateKeyPair(byte[] seed) { byte[] i = hmacSha512("Bitcoin seed".getBytes(), seed); byte[] il = Arrays.copyOfRange(i, 0, 32); byte[] ir = Arrays.copyOfRange(i, 32, 64); Arrays.fill(i, (byte) 0); Bip32ECKeyPair keypair = Bip32ECKeyPair.create(il, ir); Arrays.fill(il, (byte) 0); Arrays.fill(ir, (byte) 0); return keypair; }
private static byte[] generateMac(byte[] derivedKey, byte[] cipherText) { byte[] result = new byte[16 + cipherText.length]; System.arraycopy(derivedKey, 16, result, 0, 16); System.arraycopy(cipherText, 0, result, 16, cipherText.length); return Hash.sha3(result); }
public static byte[] sha256hash160(byte[] input) { byte[] sha256 = sha256(input); RIPEMD160Digest digest = new RIPEMD160Digest(); digest.update(sha256, 0, sha256.length); byte[] out = new byte[20]; digest.doFinal(out, 0); return out; } }
/** * Checksum address encoding as per * <a href="https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md">EIP-55</a>. * * @param address a valid hex encoded address * @return hex encoded checksum address */ public static String toChecksumAddress(String address) { String lowercaseAddress = Numeric.cleanHexPrefix(address).toLowerCase(); String addressHash = Numeric.cleanHexPrefix(Hash.sha3String(lowercaseAddress)); StringBuilder result = new StringBuilder(lowercaseAddress.length() + 2); result.append("0x"); for (int i = 0; i < lowercaseAddress.length(); i++) { if (Integer.parseInt(String.valueOf(addressHash.charAt(i)), 16) >= 8) { result.append(String.valueOf(lowercaseAddress.charAt(i)).toUpperCase()); } else { result.append(lowercaseAddress.charAt(i)); } } return result.toString(); }
data.put(parentPublicKey); data.putInt(childNumber); byte[] i = hmacSha512(getChainCode(), data.array()); byte[] il = Arrays.copyOfRange(i, 0, 32); byte[] chainCode = Arrays.copyOfRange(i, 32, 64); byte[] i = hmacSha512(getChainCode(), data.array()); byte[] il = Arrays.copyOfRange(i, 0, 32); byte[] chainCode = Arrays.copyOfRange(i, 32, 64);
public static String buildEventSignature(String methodSignature) { byte[] input = methodSignature.getBytes(); byte[] hash = Hash.sha3(input); return Numeric.toHexString(hash); } }
public static Credentials loadBip39Credentials(String password, String mnemonic) { byte[] seed = MnemonicUtils.generateSeed(mnemonic, password); return Credentials.create(ECKeyPair.create(sha256(seed))); }
contract.getContractAddress()). addSingleTopic(EventEncoder.encode(event)). addOptionalTopics(Hash.sha3String(identifier)). addNullTopic(); final CompletableFuture<List<ModelProvenanceElement>> result = new CompletableFuture<>();
/** * Keccak-256 hash function that operates on a UTF-8 encoded String. * * @param utf8String UTF-8 encoded string * @return hash value as hex encoded string */ public static String sha3String(String utf8String) { return Numeric.toHexString(sha3(utf8String.getBytes(StandardCharsets.UTF_8))); }
/** * Generates a BIP-39 compatible Ethereum wallet. The private key for the wallet can * be calculated using following algorithm: * <pre> * Key = SHA-256(BIP_39_SEED(mnemonic, password)) * </pre> * * @param password Will be used for both wallet encryption and passphrase for BIP-39 seed * @param destinationDirectory The directory containing the wallet * @return A BIP-39 compatible Ethereum wallet * @throws CipherException if the underlying cipher is not available * @throws IOException if the destination cannot be written to */ public static Bip39Wallet generateBip39Wallet(String password, File destinationDirectory) throws CipherException, IOException { byte[] initialEntropy = new byte[16]; secureRandom.nextBytes(initialEntropy); String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy); byte[] seed = MnemonicUtils.generateSeed(mnemonic, password); ECKeyPair privateKey = ECKeyPair.create(sha256(seed)); String walletFile = generateWalletFile(password, privateKey, destinationDirectory, false); return new Bip39Wallet(walletFile, mnemonic); }