iaik.me.security
Class MessageDigest

java.lang.Object
  |
  +--iaik.me.security.MessageDigest
Direct Known Subclasses:
MD2, MD5, SHA, SHA256

public abstract class MessageDigest
extends Object

This is the main class for digest computation. A message digest is a one-way function that maps an arbitrarily long input to a fixed length output. It can be used in various ways including digital signatures or for MAC functions.

This class defines the API, forms the base class, and is a factory for MessageDigest implementations.

The API closely resembles the API used within the JCE in JDK 1.1 and later, that documentation may provide additional insight.

Note that MessageDigests should not be created directly via the constructor but preferably via the getInstance(String) method defined in this class.
For example:

 MessageDigest digest = MessageDigest.getInstance("SHA1");
 digest.update("MY TEST DATA".getBytes());	
 byte[] result = digest.digest();		
 
Supported algorithm aliases are:

Since:
3.0
Version:
3.02

Field Summary
protected  byte[] buffer
          Buffer, for use by implementations.
protected  long count
          Count of the number of bytes processed so far, for use by implementations.
protected  int digestLength
           
protected static byte[] padding
          Data suitable for padding of MD4 style digests. padding[0] == 0x80, all other bytes are zero.
 
Constructor Summary
protected MessageDigest(int digestLength)
          Constructor for implementations that do not which to use the buffering features of this class.
protected MessageDigest(int digestLength, int blockSize)
          Constructor for implementations.
 
Method Summary
abstract  Object clone()
          Returns a clone of this object.
protected  void compress(byte[] input, int offset)
          Compression function, to be implemented by subclasses.
 byte[] digest()
          Computes the digest.
 byte[] digest(byte[] input)
          Computes the digest.
abstract  int digest(byte[] output, int offset)
          Performs the final padding operations and computes the digest.
 String getAlgorithm()
          Returns the name of the used digest algorithm.
 int getDigestLength()
          Returns the length of the digestalgorithm output in bytes.
abstract  byte[][] getEncodedDigestInfo()
          Returns the pregenerated ASN1 encoding of the DigestInfo structure for the corresponding digest algorithm, see .
static MessageDigest getInstance(String algorithm)
          Returns an instance of the MessageDigest with the given name.
static void register(String name, String clazz)
          This method is used to register new (or self implemented) signature algorithms.
For example: MessageDigest.register("SHA1", "iaik.me.security.md.SHA"); Once registered, the algorithm can be used via the getInstance(String) method.
abstract  void reset()
          Resets the internal values of digest instance.
 void update(byte b)
          Adds a byte to the digest data.
 void update(byte[] input)
          Adds a byte array to the digest data.
 void update(byte[] input, int offset, int len)
          Adds a byte array to the digest data.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

padding

protected static final byte[] padding
Data suitable for padding of MD4 style digests. padding[0] == 0x80, all other bytes are zero.

digestLength

protected int digestLength

buffer

protected byte[] buffer
Buffer, for use by implementations.

count

protected long count
Count of the number of bytes processed so far, for use by implementations.
Constructor Detail

MessageDigest

protected MessageDigest(int digestLength)
Constructor for implementations that do not which to use the buffering features of this class.

MessageDigest

protected MessageDigest(int digestLength,
                        int blockSize)
Constructor for implementations. Length of digest and block (compression function) are in bytes.
Method Detail

getAlgorithm

public final String getAlgorithm()
Returns the name of the used digest algorithm.
Returns:
the algorithm name
Since:
3.0

getDigestLength

public final int getDigestLength()
Returns the length of the digestalgorithm output in bytes. For e.g. SHA1 has a digestlength of 160 bits, so the resulting digestlength is 20 bytes.
Returns:
the number of bytes of the output
Since:
3.0

update

public void update(byte b)
Adds a byte to the digest data.
Parameters:
b - digest data
Since:
3.0

update

public void update(byte[] input)
Adds a byte array to the digest data.
Since:
3.0

update

public void update(byte[] input,
                   int offset,
                   int len)
Adds a byte array to the digest data.
Parameters:
input - the data to be hashed.
offset - startindex
len - number of bytes to be hashed beginnig from the startindex
Since:
3.0

compress

protected void compress(byte[] input,
                        int offset)
Compression function, to be implemented by subclasses.

digest

public abstract int digest(byte[] output,
                           int offset)
Performs the final padding operations and computes the digest. The digest is stored in output, the method itself returns the length of the digest output. The digest instance is automatically reset after the digest was computed.
Parameters:
output - the hash value
offset - starting index of the data to hash
Returns:
the length of the digest output in bytes. see getDigestLength()
Since:
3.0

digest

public final byte[] digest(byte[] input)
Computes the digest. Equivalent to update(byte[]) followed by digest(). This method needs to allocate a new byte array and hence may be less efficient than the digest(byte[] , int ) method if used frequently. The digest instance is automatically reset after the digest was computed.
Parameters:
input - the data to be hashed
Returns:
the computed hashvalue
Since:
3.0

digest

public final byte[] digest()
Computes the digest. This method needs to allocate a new byte array and hence may be less efficient than the digest(byte[] , int ) method if used frequently. The digest instance is automatically reset after the digest was computed.
Returns:
the computed digest
Since:
3.0

reset

public abstract void reset()
Resets the internal values of digest instance. This action is automatically performed after the digest was computed.
Since:
3.0

clone

public abstract Object clone()
Returns a clone of this object. The clone has the exact same state as this object.
Overrides:
clone in class Object
Returns:
the cloned digest object
Since:
3.0

getInstance

public static MessageDigest getInstance(String algorithm)
                                 throws CryptoException
Returns an instance of the MessageDigest with the given name. The name may be an algorithm name (e.g. "MD5") or an object identifier mapped to such a name in the ASN1 class (e.g. ASN.OID_MD5, which is "1.2.840.113549.2.5" and maps to "MD5"). If an implementation is not available (unknown algorithm, implementation not included in JAR file) a CryptoException is thrown. The digest instance is automatically reset after the digest was computed.

Algorithm names supported are MD2, MD5, SHA, SHA1, SHA-1 (alias for SHA), SHA-224, SHA-256 and the object ids registed in the ASN1 class.

Parameters:
algorithm - the name of the digestalgorithm
Returns:
the messagedigest instance
Since:
3.0

getEncodedDigestInfo

public abstract byte[][] getEncodedDigestInfo()
Returns the pregenerated ASN1 encoding of the DigestInfo structure for the corresponding digest algorithm, see . The digest value is of course NOT included. The two dimensional array cotains the enconding with (index = 0) and without the ASN1 parameter NULL (index = 1).
Returns:
a two dimensional byte array containing the digestinfo encodings
Since:
3.04

register

public static void register(String name,
                            String clazz)
This method is used to register new (or self implemented) signature algorithms.
For example:
 MessageDigest.register("SHA1", "iaik.me.security.md.SHA");    
 
Once registered, the algorithm can be used via the getInstance(String) method.
Parameters:
name - the name of the algorithm
clazz - the name of the class which implements the algorithm (with full package name)
Since:
3.0

This Javadoc may contain text parts from IETF Internet Standard specifications, see copyright note) and RSA Data Security Public-Key Cryptography Standards (see copyright note).

IAIK-JCE ME 3.04, (c) 2002 IAIK, (c) 2003 to 2006 Stiftung SIC