forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigest.java
More file actions
46 lines (38 loc) · 1.06 KB
/
Copy pathDigest.java
File metadata and controls
46 lines (38 loc) · 1.06 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
package DNA.Cryptography;
import java.security.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Digest {
static {
Security.addProvider(new BouncyCastleProvider());
}
public static byte[] hash160(byte[] value) {
return ripemd160(sha256(value));
}
public static byte[] hash256(byte[] value) {
return sha256(sha256(value));
}
public static byte[] ripemd160(byte[] value) {
try {
MessageDigest md = MessageDigest.getInstance("RipeMD160");
return md.digest(value);
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
}
public static byte[] sha256(byte[] value) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
return md.digest(value);
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
}
public static byte[] sha256(byte[] value, int offset, int length) {
if (offset != 0 || length != value.length) {
byte[] array = new byte[length];
System.arraycopy(value, offset, array, 0, length);
value = array;
}
return sha256(value);
}
}