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

SHA256Digest

public class SHA256Digest extends GeneralDigest
Draft FIPS 180-2 implementation of SHA-256. Note: As this is based on a draft this implementation is subject to change.
block word digest
SHA-1 512 32 160
SHA-256 512 32 256
SHA-384 1024 64 384
SHA-512 1024 64 512

Fields Summary
private static final int
DIGEST_LENGTH
private int
H1
private int
H2
private int
H3
private int
H4
private int
H5
private int
H6
private int
H7
private int
H8
private int[]
X
private int
xOff
static final int[]
K
Constructors Summary
public SHA256Digest()
Standard constructor


	  	 
     
    
        reset();
    
public SHA256Digest(SHA256Digest t)
Copy constructor. This will copy the state of the provided message digest.

		super(t);

		H1 = t.H1;
		H2 = t.H2;
		H3 = t.H3;
		H4 = t.H4;
		H5 = t.H5;
		H6 = t.H6;
		H7 = t.H7;
		H8 = t.H8;

		System.arraycopy(t.X, 0, X, 0, t.X.length);
		xOff = t.xOff;
	
Methods Summary
private intCh(int x, int y, int z)

        return ((x & y) ^ ((~x) & z));
    
private intMaj(int x, int y, int z)

        return ((x & y) ^ (x & z) ^ (y & z));
    
private intSum0(int x)

        return rotateRight(x, 2) ^ rotateRight(x, 13) ^ rotateRight(x, 22);
    
private intSum1(int x)

        return rotateRight(x, 6) ^ rotateRight(x, 11) ^ rotateRight(x, 25);
    
private intTheta0(int x)

        return rotateRight(x, 7) ^ rotateRight(x, 18) ^ (x >>> 3);
    
private intTheta1(int x)

        return rotateRight(x, 17) ^ rotateRight(x, 19) ^ (x >>> 10);
    
public intdoFinal(byte[] out, int outOff)

        finish();

        unpackWord(H1, out, outOff);
        unpackWord(H2, out, outOff + 4);
        unpackWord(H3, out, outOff + 8);
        unpackWord(H4, out, outOff + 12);
        unpackWord(H5, out, outOff + 16);
        unpackWord(H6, out, outOff + 20);
        unpackWord(H7, out, outOff + 24);
        unpackWord(H8, out, outOff + 28);

        reset();

        return DIGEST_LENGTH;
    
public java.lang.StringgetAlgorithmName()

        return "SHA-256";
    
public intgetDigestSize()

        return DIGEST_LENGTH;
    
protected voidprocessBlock()

        //
        // expand 16 word block into 64 word blocks.
        //
        for (int t = 16; t <= 63; t++)
        {
            X[t] = Theta1(X[t - 2]) + X[t - 7] + Theta0(X[t - 15]) + X[t - 16];
        }

        //
        // set up working variables.
        //
        int     a = H1;
        int     b = H2;
        int     c = H3;
        int     d = H4;
        int     e = H5;
        int     f = H6;
        int     g = H7;
        int     h = H8;

		for (int t = 0; t <= 63; t++)
		{
			int		T1, T2;

			T1 = h + Sum1(e) + Ch(e, f, g) + K[t] + X[t];
			T2 = Sum0(a) + Maj(a, b, c);
			h = g;
			g = f;
			f = e;
			e = d + T1;
			d = c;
			c = b;
			b = a;
			a = T1 + T2;
		}

        H1 += a;
        H2 += b;
        H3 += c;
        H4 += d;
        H5 += e;
        H6 += f;
        H7 += g;
        H8 += h;

        //
        // reset the offset and clean out the word buffer.
        //
        xOff = 0;
        for (int i = 0; i != X.length; i++)
        {
            X[i] = 0;
        }
    
protected voidprocessLength(long bitLength)

        if (xOff > 14)
        {
            processBlock();
        }

        X[14] = (int)(bitLength >>> 32);
        X[15] = (int)(bitLength & 0xffffffff);
    
protected voidprocessWord(byte[] in, int inOff)

        X[xOff++] = ((in[inOff] & 0xff) << 24) | ((in[inOff + 1] & 0xff) << 16)
                    | ((in[inOff + 2] & 0xff) << 8) | ((in[inOff + 3] & 0xff)); 

        if (xOff == 16)
        {
            processBlock();
        }
    
public voidreset()
reset the chaining variables

        super.reset();

		/* SHA-256 initial hash value
		 * The first 32 bits of the fractional parts of the square roots
		 * of the first eight prime numbers
		 */

		H1 = 0x6a09e667;
		H2 = 0xbb67ae85;
		H3 = 0x3c6ef372;
		H4 = 0xa54ff53a;
		H5 = 0x510e527f;
		H6 = 0x9b05688c;
		H7 = 0x1f83d9ab;
		H8 = 0x5be0cd19;

        xOff = 0;
        for (int i = 0; i != X.length; i++)
        {
            X[i] = 0;
        }
    
private introtateRight(int x, int n)

        return (x >>> n) | (x << (32 - n));
    
private voidunpackWord(int word, byte[] out, int outOff)

        out[outOff]     = (byte)(word >>> 24);
        out[outOff + 1] = (byte)(word >>> 16);
        out[outOff + 2] = (byte)(word >>> 8);
        out[outOff + 3] = (byte)word;