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