-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashFunction.java
73 lines (60 loc) · 1.65 KB
/
HashFunction.java
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
62
63
64
65
66
67
68
69
70
71
72
73
// This class implements a cryptographic hash function.
// To use it, you must first create a HashFunction object:
//
// HashFunction hf = new HashFunction();
//
// Having created it, you feed it the data you want to hash. You can feed
// the data in in chunks. To feed in a byte, use
//
// byte b = ...
// hf.update(b);
//
// To feed in an entire array of bytes, use
//
// byte[] barr = ...
// hf.update(barr);
//
// To feed in part of an array of bytes, use
//
// byte[] barr = ...
// hf.update(barr, int offset, int len);
//
// This feeds in the subarray from barr[offset] to barr[offset+len-1].
// When you've fed in all of the bytes you want to hash, you can get the
// result by calling
//
// byte[] hashResult = hf.digest();
//
// After calling digest(), the HashFunction object is reset, so you can reuse
// it as if it were a newly created HashFunction object.
// You can also call reset() on a HashFunction if you want to reset it.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashFunction {
private MessageDigest dig;
public HashFunction() {
try{
dig = MessageDigest.getInstance("SHA");
}catch(NoSuchAlgorithmException x) {
x.printStackTrace();
dig = null;
}
}
public void update(byte b) {
dig.update(b);
}
public void update(byte[] arr) {
dig.update(arr);
}
public void update(byte[] arr, int offset, int len) {
dig.update(arr, offset, len);
}
public byte[] digest() {
byte[] ret = dig.digest();
dig.reset();
return ret;
}
public void reset() {
dig.reset();
}
}