forked from nayuki/Native-hashes-for-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSha512.java
More file actions
59 lines (40 loc) · 1.38 KB
/
Sha512.java
File metadata and controls
59 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Native hash functions for Java
*
* Copyright (c) Project Nayuki. (MIT License)
* https://www.nayuki.io/page/native-hash-functions-for-java
*/
package nayuki.nativehash;
import java.util.Arrays;
public class Sha512 extends BlockHasher {
protected long[] state;
public Sha512() {
super(128);
state = new long[]{0x6A09E667F3BCC908L, 0xBB67AE8584CAA73BL, 0x3C6EF372FE94F82BL, 0xA54FF53A5F1D36F1L, 0x510E527FADE682D1L, 0x9B05688C2B3E6C1FL, 0x1F83D9ABFB41BD6BL, 0x5BE0CD19137E2179L};
}
protected void compress(byte[] msg, int off, int len) {
if (!compress(state, msg, off, len))
throw new RuntimeException("Native call failed");
}
protected byte[] getHashDestructively() {
block[blockFilled] = (byte)0x80;
blockFilled++;
Arrays.fill(block, blockFilled, block.length, (byte)0);
if (blockFilled + 16 > block.length) {
compress(block, 0, block.length);
Arrays.fill(block, (byte)0);
}
length = length << 3;
for (int i = 0; i < 8; i++)
block[block.length - 1 - i] = (byte)(length >>> (i * 8));
compress(block, 0, block.length);
byte[] result = new byte[state.length * 8];
for (int i = 0; i < result.length; i++)
result[i] = (byte)(state[i / 8] >>> (56 - i % 8 * 8));
return result;
}
private static native boolean compress(long[] state, byte[] msg, int off, int len);
static {
System.loadLibrary("nayuki-native-hashes");
}
}