FileDocCategorySizeDatePackage
MD2Digest.javaAPI DocAzureus 3.0.3.47066Tue Jun 08 05:12:58 BST 2004org.bouncycastle.crypto.digests

MD2Digest

public class MD2Digest extends Object implements org.bouncycastle.crypto.Digest
implementation of MD2 as outlined in RFC1319 by B.Kaliski from RSA Laboratories April 1992

Fields Summary
private static final int
DIGEST_LENGTH
private byte[]
X
private int
xOff
private byte[]
M
private int
mOff
private byte[]
C
private int
COff
private static final byte[]
S
Constructors Summary
public MD2Digest()


     
    
        reset();
    
public MD2Digest(MD2Digest t)

		System.arraycopy(t.X, 0, X, 0, t.X.length);
		xOff = t.xOff;
		System.arraycopy(t.M, 0, M, 0, t.M.length);
		mOff = t.mOff;
		System.arraycopy(t.C, 0, C, 0, t.C.length);
		COff = t.COff;
	
Methods Summary
public intdoFinal(byte[] out, int outOff)
close the digest, producing the final digest value. The doFinal call leaves the digest reset.

param
out the array the digest is to be copied into.
param
outOff the offset into the out array the digest is to start at.

        // add padding
        byte paddingByte = (byte)(M.length-mOff);
        for (int i=mOff;i<M.length;i++)
        {
            M[i] = paddingByte;
        }
        //do final check sum
        processCheckSum(M);
        // do final block process
        processBlock(M);

        processBlock(C);

        System.arraycopy(X,xOff,out,outOff,16);

        reset();

        return DIGEST_LENGTH;
    
public java.lang.StringgetAlgorithmName()
return the algorithm name

return
the algorithm name

        return "MD2";
    
public intgetDigestSize()
return the size, in bytes, of the digest produced by this message digest.

return
the size, in bytes, of the digest produced by this message digest.

        return DIGEST_LENGTH;
    
protected voidprocessBlock(byte[] m)

        for (int i=0;i<16;i++)
        {
            X[i+16] = m[i];
            X[i+32] = (byte)(m[i] ^ X[i]);
        }
        // encrypt block
        int t = 0;

        for (int j=0;j<18;j++)
        {
            for (int k=0;k<48;k++)
            {
                t = X[k] ^= S[t];
                t = t & 0xff;
            }
            t = (t + j)%256;
        }
     
protected voidprocessCheckSum(byte[] m)

        int L = C[15];
        for (int i=0;i<16;i++)
        {
            C[i] ^= S[(m[i] ^ L) & 0xff];
            L = C[i];
        }
    
public voidreset()
reset the digest back to it's initial state.

        xOff = 0;
        for (int i = 0; i != X.length; i++)
        {
            X[i] = 0;
        }
        mOff = 0;
        for (int i = 0; i != M.length; i++)
        {
            M[i] = 0;
        }
        COff = 0;
        for (int i = 0; i != C.length; i++)
        {
            C[i] = 0;
        }
    
public voidupdate(byte in)
update the message digest with a single byte.

param
in the input byte to be entered.

        M[mOff++] = in;

        if (mOff == 16)
        {
            processCheckSum(M);
            processBlock(M);
            mOff = 0;
        }
    
public voidupdate(byte[] in, int inOff, int len)
update the message digest with a block of bytes.

param
in the byte array containing the data.
param
inOff the offset into the byte array where the data starts.
param
len the length of the data.

        //
        // fill the current word
        //
        while ((mOff != 0) && (len > 0))
        {
            update(in[inOff]);
            inOff++;
            len--;
        }

        //
        // process whole words.
        //
        while (len > 16)
        {
            System.arraycopy(in,inOff,M,0,16);
            processCheckSum(M);
            processBlock(M);
            len -= 16;
            inOff += 16;
        }

        //
        // load in the remainder.
        //
        while (len > 0)
        {
            update(in[inOff]);
            inOff++;
            len--;
        }