• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.bouncycastle.crypto.digests;
2 
3 import org.bouncycastle.util.Memoable;
4 import org.bouncycastle.util.Pack;
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  */
18 public class SHA512Digest
19     extends LongDigest
20 {
21     private static final int    DIGEST_LENGTH = 64;
22 
23     /**
24      * Standard constructor
25      */
SHA512Digest()26     public SHA512Digest()
27     {
28     }
29 
30     /**
31      * Copy constructor.  This will copy the state of the provided
32      * message digest.
33      */
SHA512Digest(SHA512Digest t)34     public SHA512Digest(SHA512Digest t)
35     {
36         super(t);
37     }
38 
SHA512Digest(byte[] encodedState)39     public SHA512Digest(byte[] encodedState)
40     {
41         restoreState(encodedState);
42     }
43 
getAlgorithmName()44     public String getAlgorithmName()
45     {
46         return "SHA-512";
47     }
48 
getDigestSize()49     public int getDigestSize()
50     {
51         return DIGEST_LENGTH;
52     }
53 
doFinal( byte[] out, int outOff)54     public int doFinal(
55         byte[]  out,
56         int     outOff)
57     {
58         finish();
59 
60         Pack.longToBigEndian(H1, out, outOff);
61         Pack.longToBigEndian(H2, out, outOff + 8);
62         Pack.longToBigEndian(H3, out, outOff + 16);
63         Pack.longToBigEndian(H4, out, outOff + 24);
64         Pack.longToBigEndian(H5, out, outOff + 32);
65         Pack.longToBigEndian(H6, out, outOff + 40);
66         Pack.longToBigEndian(H7, out, outOff + 48);
67         Pack.longToBigEndian(H8, out, outOff + 56);
68 
69         reset();
70 
71         return DIGEST_LENGTH;
72     }
73 
74     /**
75      * reset the chaining variables
76      */
reset()77     public void reset()
78     {
79         super.reset();
80 
81         /* SHA-512 initial hash value
82          * The first 64 bits of the fractional parts of the square roots
83          * of the first eight prime numbers
84          */
85         H1 = 0x6a09e667f3bcc908L;
86         H2 = 0xbb67ae8584caa73bL;
87         H3 = 0x3c6ef372fe94f82bL;
88         H4 = 0xa54ff53a5f1d36f1L;
89         H5 = 0x510e527fade682d1L;
90         H6 = 0x9b05688c2b3e6c1fL;
91         H7 = 0x1f83d9abfb41bd6bL;
92         H8 = 0x5be0cd19137e2179L;
93     }
94 
copy()95     public Memoable copy()
96     {
97         return new SHA512Digest(this);
98     }
99 
reset(Memoable other)100     public void reset(Memoable other)
101     {
102         SHA512Digest d = (SHA512Digest)other;
103 
104         copyIn(d);
105     }
106 
getEncodedState()107     public byte[] getEncodedState()
108     {
109         byte[] encoded = new byte[getEncodedStateSize()];
110         super.populateState(encoded);
111         return encoded;
112     }
113 }
114 
115