public final class Hash

  1. Object
  2. 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)

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.

Fields

public static final String MD5 = "MD5"MD5 algorithm identifier (128-bit digest).
public static final String SHA1 = "SHA-1"SHA-1 algorithm identifier (160-bit digest).
public static final String SHA224 = "SHA-224"SHA-224 algorithm identifier (224-bit digest, SHA-2 family).
public static final String SHA256 = "SHA-256"SHA-256 algorithm identifier (256-bit digest, SHA-2 family).
public static final String SHA384 = "SHA-384"SHA-384 algorithm identifier (384-bit digest, SHA-2 family).
public static final String SHA512 = "SHA-512"SHA-512 algorithm identifier (512-bit digest, SHA-2 family).

Methods

public static Hash create(String algorithm)Creates a streaming hash for the given algorithm.
public void update(byte[] data)Feeds the entire array into the running hash.
public void update(byte[] data, int offset, int length)Feeds a slice of the array into the running hash.
public void update(byte b)Feeds a single byte into the running hash.
public byte[] digest()Finalises the running hash and returns the digest.
public byte[] digest(byte[] data)Convenience: feed data then return the digest.
public int digestLength()Number of bytes in the digest produced by this hash.
public void reset()Resets the running digest so the instance can be reused.
public static byte[] md5(byte[] data)One-shot MD5 hash.
public static byte[] sha1(byte[] data)One-shot SHA-1 hash.
public static byte[] sha224(byte[] data)One-shot SHA-224 hash.
public static byte[] sha256(byte[] data)One-shot SHA-256 hash (recommended general-purpose hash).
public static byte[] sha384(byte[] data)One-shot SHA-384 hash.
public static byte[] sha512(byte[] data)One-shot SHA-512 hash.
public static String toHex(byte[] data)Encodes the bytes as a lowercase hex string (two characters per byte).
public static byte[] fromHex(String hex)Decodes a hex string back into bytes.

Inherited methods

Field details

MD5

public static final String MD5 = "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.

SHA1

public static final String SHA1 = "SHA-1"
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.

SHA224

public static final String SHA224 = "SHA-224"
SHA-224 algorithm identifier (224-bit digest, SHA-2 family).

SHA256

public static final String SHA256 = "SHA-256"
SHA-256 algorithm identifier (256-bit digest, SHA-2 family). Recommended default for general-purpose hashing.

SHA384

public static final String SHA384 = "SHA-384"
SHA-384 algorithm identifier (384-bit digest, SHA-2 family).

SHA512

public static final String SHA512 = "SHA-512"
SHA-512 algorithm identifier (512-bit digest, SHA-2 family).

Method details

create

public static Hash create(String algorithm)
Creates a streaming hash for the given algorithm.

Parameters

algorithm String
one of MD5, SHA1, SHA224, SHA256, SHA384, SHA512 – case insensitive, with or without the dash

Returns

a new Hash instance with zero bytes consumed

Throws

CryptoException
if the algorithm is not recognised

update

public void update(byte[] data)
Feeds the entire array into the running hash.

Parameters

data byte[]
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 byte[]
bytes to append to the running digest
offset int
index of the first byte to read
length int
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)

digest

public byte[] digest(byte[] data)
Convenience: feed data then 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.

toHex

public static String toHex(byte[] data)
Encodes the bytes as a lowercase hex string (two characters per byte).

fromHex

public static byte[] fromHex(String hex)
Decodes a hex string back into bytes. The string must contain an even number of hex characters (whitespace and the 0x prefix are not stripped – pass cleaned input).