FileDocCategorySizeDatePackage
MD5.javaAPI DocphoneME MR2 API (J2ME)6375Wed May 02 18:00:24 BST 2007com.sun.midp.crypto

MD5

public final class MD5 extends MessageDigest
Implements the MD5 hashing algorithm as described in IETF RFC 1321 (see http://www.ietf.org/rfc/rfc1321.txt)

Fields Summary
private int[]
state
State needed for MD5 hash, four elements correspond to A, B, C, D.
private int[]
num
State needed for MD5 hash.
private int[]
count
Number of bits modulo 2^64 (LSB first)
private int[]
data
Input data.
Constructors Summary
MD5()
Create an MD5 digest object.

    
          
     
        reset();
    
Methods Summary
public java.lang.Objectclone()
Clones the MessageDigest object.

return
a clone of this object

        MD5 cpy = new MD5();
        
        System.arraycopy(this.state, 0, cpy.state, 0, 4);
        System.arraycopy(this.num, 0, cpy.num, 0, 1);
        System.arraycopy(this.data, 0, cpy.data, 0, 16);
        System.arraycopy(this.count, 0, cpy.count, 0, 2);
        return cpy;
    
public intdigest(byte[] buf, int offset, int len)
Completes the hash computation by performing final operations such as padding. The digest is reset after this call is made.

param
buf output buffer for the computed digest
param
offset offset into the output buffer to begin storing the digest
param
len number of bytes within buf allotted for the digest
return
the number of bytes placed into buf
exception
DigestException if an error occurs.

        if (len < getDigestLength()) {
            throw new DigestException("Buffer too short.");
        }

        // check the parameters to prevent a VM crash
        int test = buf[offset] + buf[offset + getDigestLength() - 1];
        
        nativeFinal(null, 0, 0, buf, offset, state, num, count, data);
        return getDigestLength();
    
public java.lang.StringgetAlgorithm()
Gets the message digest algorithm.

return
algorithm implemented by this MessageDigest object

 
        return "MD5";
    
public intgetDigestLength()
Gets the length (in bytes) of the hash.

return
byte-length of the hash produced by this object

        return 16;
    
private static native voidnativeFinal(byte[] inBuf, int inOff, int inLen, byte[] outBuf, int outOff, int[] state, int[] num, int[] count, int[] data)
Generates a hash of all/last input data. Completes and returns the hash compuatation after performing final operations such as padding. The MessageDigest object is reset after this call.

param
inBuf input buffer of data to be hashed
param
inOff offset within inBuf where input data begins
param
inLen length (in bytes) of data to be hashed
param
outBuf output buffer where the hash should be placed
param
outOff offset within outBuf where the resulting hash begins
param
state internal hash state
param
num internal hash state
param
count internal hash state
param
data internal hash state

private static native voidnativeUpdate(byte[] inBuf, int inOff, int inLen, int[] state, int[] num, int[] count, int[] data)
Accumulates a hash of the input data. Continues an MD5 message-digest operation, processing another message block, and updating the internal context.

param
inBuf input buffer of data to be hashed
param
inOff offset within inBuf where input data begins
param
inLen length (in bytes) of data to be hashed
param
state internal hash state
param
num internal hash state
param
count internal hash state
param
data internal hash state

public voidreset()
Resets the MessageDigest to the initial state for further use.

        count[0] = 0;
        count[1] = 0;

        // Load magic initialization constants.
        state[0] = 0x67452301;
        state[1] = 0xefcdab89;
        state[2] = 0x98badcfe;
        state[3] = 0x10325476;
        
        for (int i = 0; i < 16; i++) 
            data[i] = 0;

        num[0] = 0;
    
public voidupdate(byte[] inBuf, int inOff, int inLen)
Accumulates a hash of the input data. Continues an MD5 message-digest operation, processing another message block, and updating the internal context.

param
inBuf input buffer of data to be hashed
param
inOff offset within inBuf where input data begins
param
inLen length (in bytes) of data to be hashed
see
#doFinal(byte[], int, int, byte[], int)

        if (inLen == 0) {
            return;
        }
        
        // check parameters to prevent VM from crashing
        int test = inBuf[inOff] + inBuf[inLen - 1] + inBuf[inOff + inLen - 1];
        
        nativeUpdate(inBuf, inOff, inLen, state, num, count, data);