forked from nayuki/Native-hashes-for-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiger.java
More file actions
61 lines (42 loc) · 1.31 KB
/
Tiger.java
File metadata and controls
61 lines (42 loc) · 1.31 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
60
61
/*
* 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 Tiger extends BlockHasher {
protected long[] state;
protected byte padding;
public Tiger() {
super(64);
state = new long[]{0x0123456789ABCDEFL, 0xFEDCBA9876543210L, 0xF096A5B4C3B2E187L};
padding = 0x01;
}
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] = padding;
blockFilled++;
Arrays.fill(block, blockFilled, block.length, (byte)0);
if (blockFilled + 8 > 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 - 8 + 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] >>> (i % 8 * 8));
return result;
}
private static native boolean compress(long[] state, byte[] msg, int off, int len);
static {
System.loadLibrary("nayuki-native-hashes");
}
}