IAIK PKCS#11 Provider Micro Edition
version 1.0

iaik.pkcs.pkcs11.me
Class MessageDigest

java.lang.Object
  extended byiaik.pkcs.pkcs11.me.MessageDigest

public class MessageDigest
extends java.lang.Object

Objects of this class calculate a hash values using the associated token.

A typical piece of code using this class may look like this.

  Token token = ...
  long algorithm = MessageDigest.ALGORITHM_SHA_1;
  if (!token.supportsAlgorithm(algorithm)) {
    ... // token does not support this hash algorithm
  }
  MessageDigest tokenMessageDigest = token.getMessageDigest(algorithm);
 
  // feed in data, e.g. from a stream
  InputStream dataStream = ...
  byte[] buffer = new byte[1024];
  int bytesRead;
  while ((bytesRead = dataStream.read(buffer)) >= 0) {
    tokenMessageDigest.update(buffer, 0, bytesRead);
  }
  byte[] digestValue = tokenMessageDigest.digest();
 
The variable digestValue will hold the calculated hash value; e.g. 20 bytes (160 bit) for a SHA-1 hash value.
Please note that this implementation operates on bytes. It it impossible to hash data which's length in bits is not a multiple of 8.

This class is not thread safe.

See Also:
Token, Token.getMessageDigest(long), Token.supportsAlgorithm(long, int)

Field Summary
static long ALGORITHM_MD2
          The algorithm code constant for the MD2 hash algorithm as specified in RFC 1319.
static long ALGORITHM_MD5
          The algorithm code constant for the MD5 hash algorithm as specified in RFC 1321.
static long ALGORITHM_RIPEMD128
          The algorithm code constant for the RIPEMD-128 hash algorithm as specified in ISO/IEC 10118-3.
static long ALGORITHM_RIPEMD160
          The algorithm code constant for the RIPEMD-160 hash algorithm as specified in ISO/IEC 10118-3.
static long ALGORITHM_SHA_1
          The algorithm code constant for NIST's SHA-1 hash algorithm as specified in FIPS PUB 180-1.
static long ALGORITHM_SHA_256
          The algorithm code constant for NIST's SHA-256 hash algorithm as specified in FIPS PUB 180-2.
static long ALGORITHM_SHA_384
          The algorithm code constant for NIST's SHA-384 hash algorithm as specified in FIPS PUB 180-2.
static long ALGORITHM_SHA_512
          The algorithm code constant for NIST's SHA-512 hash algorithm as specified in FIPS PUB 180-2.
 
Method Summary
 byte[] digest()
          Finish the hash calculation of the previously fed in data.
static byte[] makeDigestInfo(byte[] hashValue, long digestAlgorithm)
          Create an encoded digest info structure for the specified hash algorithm and hash value.
 void update(byte[] data)
          Feed a data piece of arbitrary length into the hash calculation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALGORITHM_SHA_1

public static final long ALGORITHM_SHA_1
The algorithm code constant for NIST's SHA-1 hash algorithm as specified in FIPS PUB 180-1.

See Also:
Constant Field Values

ALGORITHM_MD2

public static final long ALGORITHM_MD2
The algorithm code constant for the MD2 hash algorithm as specified in RFC 1319.

See Also:
Constant Field Values

ALGORITHM_MD5

public static final long ALGORITHM_MD5
The algorithm code constant for the MD5 hash algorithm as specified in RFC 1321.

See Also:
Constant Field Values

ALGORITHM_RIPEMD128

public static final long ALGORITHM_RIPEMD128
The algorithm code constant for the RIPEMD-128 hash algorithm as specified in ISO/IEC 10118-3.

See Also:
Constant Field Values

ALGORITHM_RIPEMD160

public static final long ALGORITHM_RIPEMD160
The algorithm code constant for the RIPEMD-160 hash algorithm as specified in ISO/IEC 10118-3.

See Also:
Constant Field Values

ALGORITHM_SHA_256

public static final long ALGORITHM_SHA_256
The algorithm code constant for NIST's SHA-256 hash algorithm as specified in FIPS PUB 180-2.

See Also:
Constant Field Values

ALGORITHM_SHA_384

public static final long ALGORITHM_SHA_384
The algorithm code constant for NIST's SHA-384 hash algorithm as specified in FIPS PUB 180-2.

See Also:
Constant Field Values

ALGORITHM_SHA_512

public static final long ALGORITHM_SHA_512
The algorithm code constant for NIST's SHA-512 hash algorithm as specified in FIPS PUB 180-2.

See Also:
Constant Field Values
Method Detail

makeDigestInfo

public static byte[] makeDigestInfo(byte[] hashValue,
                                    long digestAlgorithm)
                             throws PKCS11Exception
Create an encoded digest info structure for the specified hash algorithm and hash value. The hash algorithm can be any constant of this class beginning with ALGORITHM_; e.g. ALGORITHM_SHA_1.

This method is especially useful in for creating a RSA signature where the hash value is calculated separately to the RSA operation; e.g. hashing in software, RSA operation with padding on the token.

Parameters:
hashValue - The raw hash value; e.g. 20 bytes for a SHA-1 hash value.
digestAlgorithm - The algorithm code of the hash algorithm; e.g. ALGORITHM_SHA_1.
Returns:
The encoded digest info structure.
Throws:
PKCS11Exception

update

public void update(byte[] data)
            throws PKCS11RuntimeException
Feed a data piece of arbitrary length into the hash calculation. All bytes of the provided array are taken.

Parameters:
data - The data to feed into the hash calculation.
Throws:
PKCS11RuntimeException
See Also:
digest()

digest

public byte[] digest()
              throws PKCS11RuntimeException
Finish the hash calculation of the previously fed in data. This includes any final padding. After this method returned, this object is reset to its initial state. It is ready for a new hash calculation.

Returns:
The hash value; e.g. 20 bytes (160 bit) for a SHA-1 hash value.
Throws:
PKCS11RuntimeException
See Also:
update(byte[])

IAIK PKCS#11 Provider Micro Edition
version 1.0

IAIK JavaSecurity Website http://jce.iaik.tugraz.at/

IAIK at Graz University of Technology, Austria, Europe
Copyright 2001-2005, IAIK, Graz University of Technology, Inffeldgasse 16a, 8010 Graz, Austria. All Rights Reserved.
version 1.0