JDK 1.4 supports the following message-digest algorithms:
- MD2 and MD5, which are 128-bit algorithms
- SHA-1, which is a 160-bit algorithm
- SHA-256, SHA-383, and SHA-512, which offer longer fingerprint sizes of 256, 383, and 512 bits, respectively
MD5 and SHA-1 are the most used algorithms.
The MessageDigest class manipulates message digests. The following methods are used in the Message digest code example:
- MessageDigest.getInstance("MD5"): Creates the message digest.
- .update(plaintext): Calculates the message digest with a plaintext string.
- .digest(): Reads the message digest.
If a key is used as part of the message-digest generation, the algorithm is known as a message-authentication code. JDK 1.4 supports the HMAC/SHA-1 and HMAC/MD5 message-authentication code algorithms.
The Mac class manipulates message-authentication codes using a key produced by the KeyGenerator class. The following methods are used in the Message authentication code example:
- KeyGenerator.getInstance("HmacMD5") and .generateKey(): Generates the key.
- Mac.getInstance("HmacMD5"): Creates a MAC object.
- .init(MD5key): Intializes the MAC object.
- .update(plaintext) and .doFinal(): Calculates the MAC object with a plaintext string.