Class Hash
Streaming and one-shot cryptographic hash (message digest) functions. The supported algorithms are exposed as constants on this class:
MD5-- 128 bit, legacy interop only (broken collision resistance)SHA1-- 160 bit, legacy interop only (broken collision resistance)SHA224-- 224 bit (SHA-2 family)SHA256-- 256 bit (SHA-2 family, recommended general-purpose hash)SHA384-- 384 bit (SHA-2 family)SHA512-- 512 bit (SHA-2 family)SHA3_256-- 256 bit (SHA-3, FIPS 202)KECCAK256-- 256 bit original Keccak, the hash Ethereum uses for addresses, transaction hashes and ABI selectors. It is NOT SHA3-256: the two differ in the padding byte and give different digests.
Quick example
byte[] digest = Hash.sha256("hello".getBytes("UTF-8"));
String hex = Hash.toHex(digest);
// streaming
Hash h = Hash.create(Hash.SHA256);
h.update("hello".getBytes("UTF-8"));
h.update(" world".getBytes("UTF-8"));
byte[] out = h.digest();
The implementations are written entirely in portable Java so they are available on every supported platform. They produce identical output to the equivalent algorithm in the standard JDK.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringKeccak-256 algorithm identifier (256-bit digest).static final StringMD5 algorithm identifier (128-bit digest).static final StringRIPEMD-160 algorithm identifier (160-bit digest).static final StringSHA-1 algorithm identifier (160-bit digest).static final StringSHA-224 algorithm identifier (224-bit digest, SHA-2 family).static final StringSHA-256 algorithm identifier (256-bit digest, SHA-2 family).static final StringSHA3-256 algorithm identifier (256-bit digest, FIPS 202).static final StringSHA-384 algorithm identifier (384-bit digest, SHA-2 family).static final StringSHA-512 algorithm identifier (512-bit digest, SHA-2 family). -
Method Summary
Modifier and TypeMethodDescriptionstatic Hashblake2b(int digestSize, byte[] key, byte[] salt, byte[] person) A BLAKE2b hash (RFC 7693), optionally keyed, salted and personalised.static Hashblake2s(int digestSize, byte[] key, byte[] salt, byte[] person) A BLAKE2s hash (RFC 7693), the 32-bit variant ofblake2b(int, byte[], byte[], byte[]).copy()An independent hash in the same state as this one: a shared prefix is hashed once and each copy finished on its own.static HashCreates a streaming hash for the given algorithm.byte[]digest()Finalises the running hash and returns the digest.byte[]digest(byte[] data) Convenience: feeddatathen return the digest.intNumber of bytes in the digest produced by this hash.static byte[]Decodes a hex string back into bytes.static byte[]hash160(byte[] data) RIPEMD-160 of the SHA-256 ofdata: Bitcoin'sHASH160, the hash inside P2PKH and P2WPKH addresses.static byte[]keccak256(byte[] data) One-shot Keccak-256 hash (Ethereum's hash, seeKECCAK256).static byte[]md5(byte[] data) One-shot MD5 hash.byte[]The digest of everything fed so far, leaving the hash running: more input may follow anddigest()still answers for all of it.voidreset()Resets the running digest so the instance can be reused.static byte[]ripemd160(byte[] data) One-shot RIPEMD-160 hash.static byte[]sha1(byte[] data) One-shot SHA-1 hash.static byte[]sha224(byte[] data) One-shot SHA-224 hash.static byte[]sha256(byte[] data) One-shot SHA-256 hash (recommended general-purpose hash).static byte[]sha384(byte[] data) One-shot SHA-384 hash.static byte[]sha512(byte[] data) One-shot SHA-512 hash.static StringtoHex(byte[] data) Encodes the bytes as a lowercase hex string (two characters per byte).voidupdate(byte b) Feeds a single byte into the running hash.voidupdate(byte[] data) Feeds the entire array into the running hash.voidupdate(byte[] data, int offset, int length) Feeds a slice of the array into the running hash.
-
Field Details
-
MD5
MD5 algorithm identifier (128-bit digest). Provided for legacy interop -- MD5 is no longer considered collision resistant and should not be used for new security-sensitive code.- See Also:
-
SHA1
SHA-1 algorithm identifier (160-bit digest). Provided for legacy interop -- SHA-1 is no longer considered collision resistant and should not be used for new security-sensitive code.- See Also:
-
SHA224
SHA-224 algorithm identifier (224-bit digest, SHA-2 family).- See Also:
-
SHA256
SHA-256 algorithm identifier (256-bit digest, SHA-2 family). Recommended default for general-purpose hashing.- See Also:
-
SHA384
SHA-384 algorithm identifier (384-bit digest, SHA-2 family).- See Also:
-
SHA512
SHA-512 algorithm identifier (512-bit digest, SHA-2 family).- See Also:
-
SHA3_256
SHA3-256 algorithm identifier (256-bit digest, FIPS 202).- See Also:
-
KECCAK256
-
RIPEMD160
RIPEMD-160 algorithm identifier (160-bit digest). Bitcoin's address hash is RIPEMD-160 over SHA-256; seehash160(byte[]).- See Also:
-
-
Method Details
-
create
Creates a streaming hash for the given algorithm.
Parameters
algorithm: one ofMD5,SHA1,SHA224,SHA256,SHA384,SHA512,SHA3_256,KECCAK256,RIPEMD160-- case insensitive, with or without the dash
Returns
a new Hash instance with zero bytes consumed
Throws
CryptoException: if the algorithm is not recognised
-
blake2b
A BLAKE2b hash (RFC 7693), optionally keyed, salted and personalised.
Parameters
-
digestSize: bytes of output, 1 to 64 -
key: the MAC key, at most 64 bytes; empty for a plain hash -
salt: at most 16 bytes, zero padded; may be empty -
person: the personalisation string, at most 16 bytes, zero padded; may be empty
Throws
CryptoException: if a size is out of range
-
-
blake2s
A BLAKE2s hash (RFC 7693), the 32-bit variant of
blake2b(int, byte[], byte[], byte[]).Parameters
-
digestSize: bytes of output, 1 to 32 -
key: the MAC key, at most 32 bytes; empty for a plain hash -
salt: at most 8 bytes, zero padded; may be empty -
person: the personalisation string, at most 8 bytes, zero padded; may be empty
Throws
CryptoException: if a size is out of range
-
-
copy
An independent hash in the same state as this one: a shared prefix is hashed once and each copy finished on its own.
Returns
the copy; updating either leaves the other unchanged
-
update
public void update(byte[] data) Feeds the entire array into the running hash.
Parameters
data: bytes to append to the running digest
-
update
public void update(byte[] data, int offset, int length) Feeds a slice of the array into the running hash.
Parameters
-
data: bytes to append to the running digest -
offset: index of the first byte to read -
length: number of bytes to read
-
-
update
public void update(byte b) Feeds a single byte into the running hash. -
digest
public byte[] digest()Finalises the running hash and returns the digest. The hash is reset after this call so the same instance may be reused for another message.
Returns
the raw digest bytes (algorithm specific length)
-
peekDigest
public byte[] peekDigest()The digest of everything fed so far, leaving the hash running: more input may follow and
digest()still answers for all of it. This iscopy().digest()without the copy where the algorithm can finish over its own state and put it back, as BLAKE2 does.Returns
the raw digest bytes (algorithm specific length)
-
digest
public byte[] digest(byte[] data) Convenience: feeddatathen return the digest. -
digestLength
public int digestLength()Number of bytes in the digest produced by this hash. -
reset
public void reset()Resets the running digest so the instance can be reused. -
md5
public static byte[] md5(byte[] data) One-shot MD5 hash. -
sha1
public static byte[] sha1(byte[] data) One-shot SHA-1 hash. -
sha224
public static byte[] sha224(byte[] data) One-shot SHA-224 hash. -
sha256
public static byte[] sha256(byte[] data) One-shot SHA-256 hash (recommended general-purpose hash). -
sha384
public static byte[] sha384(byte[] data) One-shot SHA-384 hash. -
sha512
public static byte[] sha512(byte[] data) One-shot SHA-512 hash. -
keccak256
public static byte[] keccak256(byte[] data) One-shot Keccak-256 hash (Ethereum's hash, seeKECCAK256). -
ripemd160
public static byte[] ripemd160(byte[] data) One-shot RIPEMD-160 hash. -
hash160
public static byte[] hash160(byte[] data) RIPEMD-160 of the SHA-256 ofdata: Bitcoin'sHASH160, the hash inside P2PKH and P2WPKH addresses. -
toHex
Encodes the bytes as a lowercase hex string (two characters per byte). -
fromHex
Decodes a hex string back into bytes. Either case is accepted, as is a leading0xor0X, the form Ethereum writes hex in; the rest must be an even number of hex characters (whitespace is not stripped).
-