/** * 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[] getAddress(byte[] publicKey) { byte[] hash = Hash.sha3(publicKey); return Arrays.copyOfRange(hash, hash.length - 20, hash.length); // right most 160 bits }
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 String buildEventSignature(String methodSignature) { byte[] input = methodSignature.getBytes(); byte[] hash = Hash.sha3(input); return Numeric.toHexString(hash); } }
static byte[] getEthereumMessageHash(byte[] message) { byte[] prefix = getEthereumMessagePrefix(message.length); byte[] result = new byte[prefix.length + message.length]; System.arraycopy(prefix, 0, result, 0, prefix.length); System.arraycopy(message, 0, result, prefix.length, message.length); return Hash.sha3(result); }
/** * 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))); }
static String buildMethodId(String methodSignature) { byte[] input = methodSignature.getBytes(); byte[] hash = Hash.sha3(input); return Numeric.toHexString(hash).substring(0, 10); } }
/** * Utility method to provide the transaction hash for a given transaction. * * @param rawTransaction we wish to send * @param credentials of the sender * @return encoded transaction hash */ public static byte[] generateTransactionHash( RawTransaction rawTransaction, Credentials credentials) { byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); return Hash.sha3(signedMessage); }
private static byte[] nameHash(String[] labels) { if (labels.length == 0 || labels[0].equals("")) { return EMPTY; } else { String[] tail; if (labels.length == 1) { tail = new String[] { }; } else { tail = Arrays.copyOfRange(labels, 1, labels.length); } byte[] remainderHash = nameHash(tail); byte[] result = Arrays.copyOf(remainderHash, 64); byte[] labelHash = Hash.sha3(labels[0].getBytes(StandardCharsets.UTF_8)); System.arraycopy(labelHash, 0, result, 32, labelHash.length); return Hash.sha3(result); } }
/** * Utility method to provide the transaction hash for a given transaction. * * @param rawTransaction we wish to send * @param chainId of the intended chain * @param credentials of the sender * @return encoded transaction hash */ public static byte[] generateTransactionHash( RawTransaction rawTransaction, byte chainId, Credentials credentials) { byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials); return Hash.sha3(signedMessage); }
/** * Given an arbitrary piece of text and an Ethereum message signature encoded in bytes, * returns the public key that was used to sign it. This can then be compared to the expected * public key to determine if the signature was correct. * * @param message RLP encoded message. * @param signatureData The message signature components * @return the public key used to sign the message * @throws SignatureException If the public key could not be recovered or if there was a * signature format error. */ public static BigInteger signedMessageToKey( byte[] message, SignatureData signatureData) throws SignatureException { return signedMessageHashToKey(Hash.sha3(message), signatureData); }
public static String getAddress(String publicKey) { String publicKeyNoPrefix = Numeric.cleanHexPrefix(publicKey); if (publicKeyNoPrefix.length() < PUBLIC_KEY_LENGTH_IN_HEX) { publicKeyNoPrefix = Strings.zeros( PUBLIC_KEY_LENGTH_IN_HEX - publicKeyNoPrefix.length()) + publicKeyNoPrefix; } String hash = Hash.sha3(publicKeyNoPrefix); return hash.substring(hash.length() - ADDRESS_LENGTH_IN_HEX); // right most 160 bits }
/** * Keccak-256 hash function. * * @param hexInput hex encoded input data with optional 0x prefix * @return hash value as hex encoded string */ public static String sha3(String hexInput) { byte[] bytes = Numeric.hexStringToByteArray(hexInput); byte[] result = sha3(bytes); return Numeric.toHexString(result); }
/** * Generate a smart contract address. This enables you to identify what address a * smart contract will be deployed to on the network. * * @param address of sender * @param nonce of transaction * @return the generated smart contract address */ public static byte[] generateContractAddress(byte[] address, BigInteger nonce) { List<RlpType> values = new ArrayList<>(); values.add(RlpString.create(address)); values.add(RlpString.create(nonce)); RlpList rlpList = new RlpList(values); byte[] encoded = RlpEncoder.encode(rlpList); byte[] hashed = Hash.sha3(encoded); return Arrays.copyOfRange(hashed, 12, hashed.length); }
public EthSendTransaction signAndSend(RawTransaction rawTransaction) throws IOException { String hexValue = sign(rawTransaction); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send(); if (ethSendTransaction != null && !ethSendTransaction.hasError()) { String txHashLocal = Hash.sha3(hexValue); String txHashRemote = ethSendTransaction.getTransactionHash(); if (!txHashVerifier.verify(txHashLocal, txHashRemote)) { throw new TxHashMismatchException(txHashLocal, txHashRemote); } } return ethSendTransaction; } }
@Test public void testDecodeIndexedDynamicBytesValue() { DynamicBytes bytes = new DynamicBytes(new byte[]{ 1, 2, 3, 4, 5}); String encoded = TypeEncoder.encodeDynamicBytes(bytes); String hash = Hash.sha3(encoded); assertThat(FunctionReturnDecoder.decodeIndexedValue( hash, new TypeReference<DynamicBytes>() {}), equalTo(new Bytes32(Numeric.hexStringToByteArray(hash)))); }
@Test public void testDecodeIndexedStringValue() { Utf8String string = new Utf8String("some text"); String encoded = TypeEncoder.encodeString(string); String hash = Hash.sha3(encoded); assertThat(FunctionReturnDecoder.decodeIndexedValue( hash, new TypeReference<Utf8String>() {}), equalTo(new Bytes32(Numeric.hexStringToByteArray(hash)))); }
@Test public void testDecodeIndexedDynamicArrayValue() { DynamicArray<Uint256> array = new DynamicArray<>(new Uint256(BigInteger.TEN)); String encoded = TypeEncoder.encodeDynamicArray(array); String hash = Hash.sha3(encoded); assertThat(FunctionReturnDecoder.decodeIndexedValue( hash, new TypeReference<DynamicArray>() {}), equalTo(new Bytes32(Numeric.hexStringToByteArray(hash)))); } }
@Test public void testSignTransaction() throws Exception { boolean accountUnlocked = unlockAccount(); assertTrue(accountUnlocked); RawTransaction rawTransaction = createTransaction(); byte[] encoded = TransactionEncoder.encode(rawTransaction); byte[] hashed = Hash.sha3(encoded); EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed)) .sendAsync().get(); String signature = ethSign.getSignature(); assertNotNull(signature); assertFalse(signature.isEmpty()); }