spki
Class Hash

java.lang.Object
  |
  +--spki.BaseObj
        |
        +--spki.Hash

public class Hash
extends BaseObj
implements LocObj, Principal, SeqDefName

spki Hash class. A Hash object gives the hash of some other object.

Formal definition:

 <alg-id>:: <byte-string> | <uri> ;
 <hash-alg-id>:: <alg-id> ;
 <hash-value>:: <byte-string> ;
 <hash>:: "(" "hash" <hash-alg-id> <hash-value> <uris>? ")" ;
 

NOTE: For now, only algorithm name is supported for the alg-id.

NOTE: Due to bugs in Cryptix 3.0.3, the MD5 and SHA1 hashing algorithms produce non-comformant output, so we have to use the hashing algorithms provided by Sun. We do not recommend use of other algorithms than MD5 or SHA1.

TODO: support using URI as the alg-id?

Version:
$Id: Hash.java,v 1.29 2002/08/05 18:55:10 tep Exp $
Author:
Tep Narula <tep@win.trlabs.ca>, adapted from SPKI.SPKIHash by Per Harald Myrvang <perm@pasta.cs.uit.no>

Field Summary
private static String __ME
          This class's full name.
private  String alg_id
          The hash algorithm ID.
private  MessageDigest md
          The digest object used for hashing input values.
static String SEXP_NAME
          This class S-Expression type name, used by toSExpression() and other methods.
private  URIs uris
          The hashed-object's URI, maybe null.
private  byte[] value
          The hash value.
 
Fields inherited from class spki.BaseObj
__ME, cache_sexp, cached_sexp, DEFAULT_HASH_ALG, locked, SEXP_NAME
 
Constructor Summary
protected Hash()
          Don't use me!
  Hash(byte[] value, String alg_id, URIs uris)
          Create an immutable hash object with the specified hashing algorithm and pre-fab hash value.
  Hash(SExpression sexp)
          Create an immutable Hash object from an S-Expression.
  Hash(String alg_id, byte[] input, URIs uris)
          Create immutable Hash object from input with the specified hashing algorithm.
 
Method Summary
 boolean equals(Hash that)
          Compare this object against the given object.
 boolean equals(Principal that)
          Compare this object with another principal.
 String getAlgId()
          Get this object's hashing algorithm ID.
 String getB64Value()
          Return the hash value as a Base64-encoded string.
private  MessageDigest getMD(String alg_id)
          Try to get a MessageDigest provider.
 byte[] getValue()
          Get this object's hash value.
private  void hash(byte[] input)
          Hash the specified data, store the result, and make this object immutable.
 Principal toHashPrincipal()
          Convert this principal into a hash representation.
 SExpression toSExpression()
          Build an SExpression representing this object.
 
Methods inherited from class spki.BaseObj
disableSExpressionCaching, enableSExpressionCaching, extractHashAlgId, getCachedSExpression, isLocked, isSListOfType, lock, providerToSPKI, setCachedSExpression, spkiToProvider, toString
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

__ME

private static final String __ME
This class's full name.

SEXP_NAME

public static final String SEXP_NAME
This class S-Expression type name, used by toSExpression() and other methods.
See Also:
toSExpression()

md

private MessageDigest md
The digest object used for hashing input values.

alg_id

private String alg_id
The hash algorithm ID.

value

private byte[] value
The hash value.

uris

private URIs uris
The hashed-object's URI, maybe null.
Constructor Detail

Hash

protected Hash()
Don't use me!
See Also:
Hash(SExpression), Hash(String,byte[],URIs), Hash(byte[],String,URIs)

Hash

public Hash(SExpression sexp)
     throws SPKIException,
            ParseException
Create an immutable Hash object from an S-Expression.
See Also:
BaseObj.BaseObj(SExpression)

Hash

public Hash(String alg_id,
            byte[] input,
            URIs uris)
     throws SPKIException
Create immutable Hash object from input with the specified hashing algorithm.
Parameters:
alg_id - String containing the desired hashing algorithm.
input - Byte array of input data to calculate hash value of.
uris - the hased-object's URI, maybe null.
Throws:
SPKIException - if something went wrong.

Hash

public Hash(byte[] value,
            String alg_id,
            URIs uris)
     throws SPKIException
Create an immutable hash object with the specified hashing algorithm and pre-fab hash value.
Parameters:
alg_id - String containing the desired hashing algorithm.
value - byte array containing the hash-value.
uris - the hased-object's URI, maybe null.
Throws:
SPKIException - if something went wrong.
Method Detail

toSExpression

public SExpression toSExpression()
                          throws SPKIException
Description copied from interface: SPKIObject
Build an SExpression representing this object.
Overrides:
toSExpression in class BaseObj
Tags copied from interface: SPKIObject
Returns:
SExpression object representing this object, or null.
Throws:
SPKIException - if something went wrong during conversion.

equals

public boolean equals(Principal that)
Description copied from interface: Principal
Compare this object with another principal.
Specified by:
equals in interface Principal
Tags copied from interface: Principal
Parameters:
that - a principal to compare with.
Returns:
true if the two principals are equal, false otherwise.

toHashPrincipal

public Principal toHashPrincipal()
                          throws SPKIException
Description copied from interface: Principal
Convert this principal into a hash representation.
Specified by:
toHashPrincipal in interface Principal
Tags copied from interface: Principal
Returns:
the hash representation of this principal.
Throws:
SPKIException - if something went wrong during conversion.

getAlgId

public String getAlgId()
Get this object's hashing algorithm ID.
Returns:
String containing the ID of the hashing hash-alg-id for this object.

getValue

public byte[] getValue()
Get this object's hash value. It makes a copy of the hash value and returns the copy to prevent external tampering.
Returns:
A fresh byte array containing a copy of this object's hash value, or null.

getB64Value

public String getB64Value()
Return the hash value as a Base64-encoded string. TODO: Cache the value?
Returns:
String containing Base64-encoded hash value, or null.

equals

public boolean equals(Hash that)
Compare this object against the given object. NOTE: we don't compare the optional URIs field, though.
Parameters:
that - the Hash object to compare this object against.
Returns:
true if they contain the same hashing algorithm and value, false otherwise.

getMD

private MessageDigest getMD(String alg_id)
                     throws SPKIException
Try to get a MessageDigest provider.
Parameters:
alg_id - String containing the desired algorithm ID.
Returns:
MessageDigest object with specified algorithm.
Throws:
SPKIException - if there were no suitable algorithm

hash

private void hash(byte[] input)
           throws IllegalStateException
Hash the specified data, store the result, and make this object immutable. The input data cannot be chained (i.e. the next call to hash() will throw an exception).
Parameters:
input - Byte array of input data to calculate hash value of.
Throws:
IllegalStateException - if this method is already immutable.
See Also:
getValue(), BaseObj.lock()