private Sha3Hash(byte[] data, boolean hashed) { if (hashed) { this.data = data; } else { this.data = HashUtil.sha3(data); } }
/** * SHA1 Hash Method. * * @param input data * @return hashed data */ public static byte[] sha1(byte[] input) { return hash(input, HASH_SHA_1_ALGORITHM_NAME); }
/** * Compute an address from an encoded public key. * * @param pubBytes an encoded (uncompressed) public key * @return 20-byte address */ public static byte[] computeAddress(byte[] pubBytes) { return HashUtil.sha3omit12( Arrays.copyOfRange(pubBytes, 1, pubBytes.length)); }
@Test public void PBKDF2_SHA256_Random_Test() { String password = "testpassword"; int iterations = 262144; int len = 32; byte[] salt = new byte[32]; SecureRandom prng = new SecureRandom(); prng.nextBytes(salt); log.info("salt= {}", Hex.toHexString(salt)); byte[] pbkdf2Password = HashUtil.pbkdf2(password.getBytes(), salt, iterations, len, "SHA-256"); log.info(Hex.toHexString(pbkdf2Password)); assertNotNull(pbkdf2Password); }
static ContractId of(byte[] contractBytes) { return new ContractId(HashUtil.sha1(contractBytes)); }
@Test public void keyRecovery() { ECKey key = new ECKey(); String message = "Hello World!"; byte[] hash = HashUtil.sha256(message.getBytes()); ECKey.ECDSASignature sig = key.doSign(hash); key = ECKey.fromPublicOnly(key.getPubKeyPoint()); boolean found = false; for (int i = 0; i < 4; i++) { ECKey key2 = ECKey.recoverFromSignature(i, sig, hash); checkNotNull(key2); if (key.equals(key2)) { found = true; break; } } assertTrue(found); }
@Test public void PBKDF2_SHA3512_Test() { String password = "testpassword"; byte[] salt = Hex.decode("ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"); int iterations = 262144; int len = 32; byte[] pbkdf2Password = HashUtil.pbkdf2(password.getBytes(), salt, iterations, len, "SHA3-512"); log.info(Hex.toHexString(pbkdf2Password)); assertArrayEquals(Hex.decode("2cf80c8e19136485eb79eba7349c8f334582f3b7f22e48e07124c4b5a85631c7"), pbkdf2Password); }
/** * Calculates RIGTMOST160(SHA3(input)). This is used in address * calculations. * * * @param input - data * @return - 20 right bytes of the hash keccak of the data */ public static byte[] sha3omit12(byte[] input) { byte[] hash = sha3(input); return copyOfRange(hash, 12, hash.length); }
/** * SHA256 Hash Method. * * @param input - data for hashing * @return - sha256 hash of the data */ static byte[] sha256(byte[] input) { return hash(input, HASH_SHA_256_ALGORITHM_NAME); }
@Test public void PBKDF2_SHA512_Test() { String password = "testpassword"; byte[] salt = Hex.decode("ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"); int iterations = 262144; int len = 32; byte[] pbkdf2Password = HashUtil.pbkdf2(password.getBytes(), salt, iterations, len, "SHA-512"); log.info(Hex.toHexString(pbkdf2Password)); assertArrayEquals(Hex.decode("5e005b008b20c7ab157725ed2f72faa4357a51eddac85837ca7d925e8daa0d41"), pbkdf2Password); }
public static byte[] calculateAddress(byte[] publicKey) { return HashUtil.sha3omit12( Arrays.copyOfRange(publicKey, 1, publicKey.length)); }
private byte[] blockIndexKey(long index) { String blockIndexKey = "BLOCK_INDEX_" + Long.toString(index); return HashUtil.sha3(blockIndexKey.getBytes()); }
/** * The hash method for supporting many algorithms. * * @param input data for hashing. * @param algorithm algorithm for hashing. ex) "KECCAK-256", "SHA-256", "SHA3-256", "SHA-1" * @return hashed data. */ public static byte[] hash(byte[] input, String algorithm) { return hash(input, algorithm, false); }
@Test public void PBKDF2_SHA1_Test() { String password = "testpassword"; byte[] salt = Hex.decode("ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"); int iterations = 262144; int len = 32; byte[] pbkdf2Password = HashUtil.pbkdf2(password.getBytes(), salt, iterations, len, "SHA-1"); log.info(Hex.toHexString(pbkdf2Password)); assertArrayEquals(Hex.decode("279af536192ea4f5dac04f96471d94ea174c38d4b9a9a908f03217a215fc01f8"), pbkdf2Password); }
/** * Get the address as binary. * * @return address */ public byte[] getAddress() throws SignatureException { byte[] pubKey = this.getPubKey(); return HashUtil.sha3omit12( Arrays.copyOfRange(pubKey, 1, pubKey.length)); }
/** * Get the headerHash for signing. * * @return hash of header */ public byte[] getHashForSigning() { return HashUtil.sha3(this.toBinary()); }
/** * SHA3(Keccak256) Hash Method. * * @param input data * @return hashed data */ public static byte[] sha3(byte[] input) { return hash(input, HASH_256_ALGORITHM_NAME); }
@Test public void PBKDF2_SHA3256_Test() { String password = "testpassword"; byte[] salt = Hex.decode("ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd"); int iterations = 262144; int len = 32; byte[] pbkdf2Password = HashUtil.pbkdf2(password.getBytes(), salt, iterations, len, "SHA3-256"); log.info(Hex.toHexString(pbkdf2Password)); assertArrayEquals(Hex.decode("2761ad467ba3d88c8c331e93829ec5ce912048037c50c8d5faeb4af87bbeb102"), pbkdf2Password); }