1package org.bouncycastle.crypto.digests;
2
3import org.bouncycastle.crypto.util.Pack;
4
5
6/**
7 * FIPS 180-2 implementation of SHA-512.
8 *
9 * <pre>
10 *         block  word  digest
11 * SHA-1   512    32    160
12 * SHA-256 512    32    256
13 * SHA-384 1024   64    384
14 * SHA-512 1024   64    512
15 * </pre>
16 */
17public class SHA512Digest
18    extends LongDigest
19{
20    private static final int    DIGEST_LENGTH = 64;
21
22    /**
23     * Standard constructor
24     */
25    public SHA512Digest()
26    {
27    }
28
29    /**
30     * Copy constructor.  This will copy the state of the provided
31     * message digest.
32     */
33    public SHA512Digest(SHA512Digest t)
34    {
35        super(t);
36    }
37
38    public String getAlgorithmName()
39    {
40        return "SHA-512";
41    }
42
43    public int getDigestSize()
44    {
45        return DIGEST_LENGTH;
46    }
47
48    public int doFinal(
49        byte[]  out,
50        int     outOff)
51    {
52        finish();
53
54        Pack.longToBigEndian(H1, out, outOff);
55        Pack.longToBigEndian(H2, out, outOff + 8);
56        Pack.longToBigEndian(H3, out, outOff + 16);
57        Pack.longToBigEndian(H4, out, outOff + 24);
58        Pack.longToBigEndian(H5, out, outOff + 32);
59        Pack.longToBigEndian(H6, out, outOff + 40);
60        Pack.longToBigEndian(H7, out, outOff + 48);
61        Pack.longToBigEndian(H8, out, outOff + 56);
62
63        reset();
64
65        return DIGEST_LENGTH;
66    }
67
68    /**
69     * reset the chaining variables
70     */
71    public void reset()
72    {
73        super.reset();
74
75        /* SHA-512 initial hash value
76         * The first 64 bits of the fractional parts of the square roots
77         * of the first eight prime numbers
78         */
79        H1 = 0x6a09e667f3bcc908L;
80        H2 = 0xbb67ae8584caa73bL;
81        H3 = 0x3c6ef372fe94f82bL;
82        H4 = 0xa54ff53a5f1d36f1L;
83        H5 = 0x510e527fade682d1L;
84        H6 = 0x9b05688c2b3e6c1fL;
85        H7 = 0x1f83d9abfb41bd6bL;
86        H8 = 0x5be0cd19137e2179L;
87    }
88}
89
90