# Class Hash

java.lang.Object
com.codename1.security.Hash

public final class Hash extends java.lang.Object

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.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `MD5`
  MD5 algorithm identifier (128-bit digest).
  `static final String`
  `SHA1`
  SHA-1 algorithm identifier (160-bit digest).
  `static final String`
  `SHA224`
  SHA-224 algorithm identifier (224-bit digest, SHA-2 family).
  `static final String`
  `SHA256`
  SHA-256 algorithm identifier (256-bit digest, SHA-2 family).
  `static final String`
  `SHA384`
  SHA-384 algorithm identifier (384-bit digest, SHA-2 family).
  `static final String`
  `SHA512`
  SHA-512 algorithm identifier (512-bit digest, SHA-2 family).

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Hash`
  `create(String algorithm)`
  Creates a streaming hash for the given algorithm.
  `byte[]`
  `digest()`
  Finalises the running hash and returns the digest.
  `byte[]`
  `digest(byte[] data)`
  Convenience: feed `data` then return the digest.
  `int`
  `digestLength()`
  Number of bytes in the digest produced by this hash.
  `static byte[]`
  `fromHex(String hex)`
  Decodes a hex string back into bytes.
  `static byte[]`
  `md5(byte[] data)`
  One-shot MD5 hash.
  `void`
  `reset()`
  Resets the running digest so the instance can be reused.
  `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 String`
  `toHex(byte[] data)`
  Encodes the bytes as a lowercase hex string (two characters per byte).
  `void`
  `update(byte b)`
  Feeds a single byte into the running hash.
  `void`
  `update(byte[] data)`
  Feeds the entire array into the running hash.
  `void`
  `update(byte[] data, int offset, int length)`
  Feeds a slice of the array into the running hash.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### MD5

    public static final String 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:
      - Constant Field Values

  - ### SHA1

    public static final String 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:
      - Constant Field Values

  - ### SHA224

    public static final String SHA224
    SHA-224 algorithm identifier (224-bit digest, SHA-2 family).
    See Also:
      - Constant Field Values

  - ### SHA256

    public static final String SHA256
    SHA-256 algorithm identifier (256-bit digest, SHA-2 family). Recommended default for general-purpose hashing.
    See Also:
      - Constant Field Values

  - ### SHA384

    public static final String SHA384
    SHA-384 algorithm identifier (384-bit digest, SHA-2 family).
    See Also:
      - Constant Field Values

  - ### SHA512

    public static final String SHA512
    SHA-512 algorithm identifier (512-bit digest, SHA-2 family).
    See Also:
      - Constant Field Values

- ## Method Details

  - ### create

    public static Hash create(String algorithm)

    Creates a streaming hash for the given algorithm.

    ###### Parameters

    - `algorithm`: 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`: 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)

  - ### 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).