FileDocCategorySizeDatePackage
MessageDigest.javaAPI DocAndroid 1.5 API14807Wed May 06 22:41:06 BST 2009java.security

MessageDigest

public abstract class MessageDigest extends MessageDigestSpi
{@code MessageDigest} is an engine class which is capable of generating one way hash values for arbitrary input, utilizing the algorithm it was initialized with.
see
MessageDigestSpi
since
Android 1.0

Fields Summary
private static final String
SERVICE
private static org.apache.harmony.security.fortress.Engine
engine
private Provider
provider
private String
algorithm
Constructors Summary
protected MessageDigest(String algorithm)
Constructs a new instance of {@code MessageDigest} with the name of the algorithm to use.

param
algorithm the name of algorithm to use
since
Android 1.0


                                               
       
        this.algorithm = algorithm;
    
Methods Summary
public java.lang.Objectclone()

        if (this instanceof Cloneable) {
            return super.clone();
        } else {
            throw new CloneNotSupportedException();
        }
    
public intdigest(byte[] buf, int offset, int len)
Computes and stores the final hash value for this {@link MessageDigest}. After the digest is computed the receiver is reset.

param
buf the buffer to store the result
param
offset the index of the first byte in {@code buf} to store
param
len the number of bytes allocated for the digest
return
the number of bytes written to {@code buf}
throws
DigestException if an error occures
throws
IllegalArgumentException if {@code offset} or {@code len} are not valid in respect to {@code buf}
see
#reset()
since
Android 1.0

        if (buf == null ||
                // offset < 0 || len < 0 ||
                // checks for negative values are commented out intentionally
                // see HARMONY-1148 for details
                (long) offset + (long) len > buf.length) {
            throw new IllegalArgumentException(Messages
                    .getString("security.05")); //$NON-NLS-1$
        }
        return engineDigest(buf, offset, len);
    
public byte[]digest(byte[] input)
Performs the final update and then computes and returns the final hash value for this {@link MessageDigest}. After the digest is computed the receiver is reset.

param
input the {@code byte} array
return
the computed one way hash value
see
#reset()
since
Android 1.0

        update(input);
        return digest();
    
public byte[]digest()
Computes and returns the final hash value for this {@link MessageDigest}. After the digest is computed the receiver is reset.

return
the computed one way hash value
see
#reset
since
Android 1.0

        return engineDigest();
    
public final java.lang.StringgetAlgorithm()
Returns the name of the algorithm of this {@code MessageDigest}.

return
the name of the algorithm of this {@code MessageDigest}
since
Android 1.0

        return algorithm;
    
public final intgetDigestLength()
Returns the engine digest length in bytes. If the implementation does not implement this function or is not an instance of {@code Cloneable}, {@code 0} is returned.

return
the digest length in bytes, or {@code 0}
since
Android 1.0

        int l = engineGetDigestLength();
        if (l != 0) {
            return l;
        }
        if (!(this instanceof Cloneable)) {
            return 0;
        }
        try {
            MessageDigest md = (MessageDigest) clone();
            return md.digest().length;
        } catch (CloneNotSupportedException e) {
            return 0;
        }
    
public static java.security.MessageDigestgetInstance(java.lang.String algorithm)
Returns a new instance of {@code MessageDigest} that utilizes the specified algorithm.

param
algorithm the name of the algorithm to use
return
a new instance of {@code MessageDigest} that utilizes the specified algorithm
throws
NoSuchAlgorithmException if the specified algorithm is not available
throws
NullPointerException if {@code algorithm} is {@code null}
since
Android 1.0

        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("security.01")); //$NON-NLS-1$
        }
        MessageDigest result;
        synchronized (engine) {
            engine.getInstance(algorithm, null);
            if (engine.spi instanceof MessageDigest) {
                result = (MessageDigest) engine.spi;
                result.algorithm = algorithm;
                result.provider = engine.provider;
                return result;
            } else {
                result = new MessageDigestImpl((MessageDigestSpi) engine.spi,
                        engine.provider, algorithm);
                return result;
            }
        }
    
public static java.security.MessageDigestgetInstance(java.lang.String algorithm, java.lang.String provider)
Returns a new instance of {@code MessageDigest} that utilizes the specified algorithm from the specified provider.

param
algorithm the name of the algorithm to use
param
provider the name of the provider
return
a new instance of {@code MessageDigest} that utilizes the specified algorithm from the specified provider
throws
NoSuchAlgorithmException if the specified algorithm is not available
throws
NoSuchProviderException if the specified provider is not available
throws
NullPointerException if {@code algorithm} is {@code null}
since
Android 1.0

        if ((provider == null) || (provider.length() == 0)) {
            throw new IllegalArgumentException(Messages.getString("security.02")); //$NON-NLS-1$
        }
        Provider p = Security.getProvider(provider);
        if (p == null) {
            throw new NoSuchProviderException(Messages.getString("security.03", provider)); //$NON-NLS-1$
        }
        return getInstance(algorithm, p);
    
public static java.security.MessageDigestgetInstance(java.lang.String algorithm, java.security.Provider provider)
Returns a new instance of {@code MessageDigest} that utilizes the specified algorithm from the specified provider.

param
algorithm the name of the algorithm to use
param
provider the provider
return
a new instance of {@code MessageDigest} that utilizes the specified algorithm from the specified provider
throws
NoSuchAlgorithmException if the specified algorithm is not available
throws
NullPointerException if {@code algorithm} is {@code null}
since
Android 1.0

        if (provider == null) {
            throw new IllegalArgumentException(Messages.getString("security.04")); //$NON-NLS-1$
        }
        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("security.01")); //$NON-NLS-1$
        }
        MessageDigest result;
        synchronized (engine) {
            engine.getInstance(algorithm, provider, null);
            if (engine.spi instanceof MessageDigest) {
                result = (MessageDigest) engine.spi;
                result.algorithm = algorithm;
                result.provider = provider;
                return result;
            } else {
                result = new MessageDigestImpl((MessageDigestSpi) engine.spi,
                        provider, algorithm);
                return result;
            }
        }
    
public final java.security.ProvidergetProvider()
Returns the provider associated with this {@code MessageDigest}.

return
the provider associated with this {@code MessageDigest}
since
Android 1.0

        return provider;
    
public static booleanisEqual(byte[] digesta, byte[] digestb)
Indicates whether to digest are equal by performing a simply byte-per-byte compare of the two digests.

param
digesta the first digest to be compared
param
digestb the second digest to be compared
return
{@code true} if the two hashes are equal, {@code false} otherwise
since
Android 1.0

        if (digesta.length != digestb.length) {
            return false;
        }
        for (int i = 0; i < digesta.length; i++) {
            if (digesta[i] != digestb[i]) {
                return false;
            }
        }
        return true;
    
public voidreset()
Puts this {@code MessageDigest} back in an initial state, such that it is ready to compute a one way hash value.

since
Android 1.0

        engineReset();
    
public java.lang.StringtoString()
Returns a string containing a concise, human-readable description of this {@code MessageDigest} including the name of its algorithm.

return
a printable representation for this {@code MessageDigest}
since
Android 1.0

        return "MESSAGE DIGEST " + algorithm; //$NON-NLS-1$
    
public final voidupdate(java.nio.ByteBuffer input)
Updates this {@code MessageDigest} using the given {@code input}.

param
input the {@code ByteBuffer}
since
Android 1.0

        engineUpdate(input);
    
public voidupdate(byte arg0)
Updates this {@code MessageDigest} using the given {@code byte}.

param
arg0 the {@code byte} to update this {@code MessageDigest} with
see
#reset()
since
Android 1.0

        engineUpdate(arg0);
    
public voidupdate(byte[] input, int offset, int len)
Updates this {@code MessageDigest} using the given {@code byte[]}.

param
input the {@code byte} array
param
offset the index of the first byte in {@code input} to update from
param
len the number of bytes in {@code input} to update from
throws
IllegalArgumentException if {@code offset} or {@code len} are not valid in respect to {@code input}
since
Android 1.0

        if (input == null ||
                // offset < 0 || len < 0 ||
                // checks for negative values are commented out intentionally
                // see HARMONY-1120 for details
                (long) offset + (long) len > input.length) {
            throw new IllegalArgumentException(Messages
                    .getString("security.05")); //$NON-NLS-1$
        }
        engineUpdate(input, offset, len);
    
public voidupdate(byte[] input)
Updates this {@code MessageDigest} using the given {@code byte[]}.

param
input the {@code byte} array
throws
NullPointerException if {@code input} is {@code null}
since
Android 1.0

        if (input == null) {
            throw new NullPointerException(Messages.getString("security.06")); //$NON-NLS-1$
        }
        engineUpdate(input, 0, input.length);