forked from nayuki/Native-hashes-for-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRipemd256.java
More file actions
59 lines (40 loc) · 1.31 KB
/
Ripemd256.java
File metadata and controls
59 lines (40 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
/*
* 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 Ripemd256 extends BlockHasher {
protected int[] state;
public Ripemd256() {
super(64);
state = new int[]{0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0x76543210, 0xFEDCBA98, 0x89ABCDEF, 0x01234567};
}
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 + 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 * 4];
for (int i = 0; i < result.length; i++)
result[i] = (byte)(state[i / 4] >>> (i % 4 * 8));
return result;
}
private static native boolean compress(int[] state, byte[] msg, int off, int len);
static {
System.loadLibrary("nayuki-native-hashes");
}
}