-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigester.java
More file actions
71 lines (56 loc) · 2.07 KB
/
Copy pathDigester.java
File metadata and controls
71 lines (56 loc) · 2.07 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
62
63
64
65
66
67
68
69
70
71
package scottf;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class Digester {
private static final String DEFAULT_DIGEST_ALGORITHM = "SHA-256";
private static final Charset DEFAULT_STRING_ENCODING = StandardCharsets.UTF_8;
private final Charset stringCharset;
private final Base64.Encoder encoder;
private final MessageDigest digest;
public Digester() throws NoSuchAlgorithmException {
this(null, null, null);
}
public Digester(Base64.Encoder encoder) throws NoSuchAlgorithmException {
this(null, null, encoder);
}
public Digester(String digestAlgorithm) throws NoSuchAlgorithmException {
this(digestAlgorithm, null, null);
}
public Digester(String digestAlgorithm, Charset stringCharset, Base64.Encoder encoder) throws NoSuchAlgorithmException {
this.stringCharset = stringCharset == null ? DEFAULT_STRING_ENCODING : stringCharset;
this.encoder = encoder == null ? Base64.getUrlEncoder() : encoder;
this.digest = MessageDigest.getInstance(
digestAlgorithm == null ? DEFAULT_DIGEST_ALGORITHM : digestAlgorithm);
}
public Digester update(String input) {
digest.update(input.getBytes(stringCharset));
return this;
}
public Digester update(byte[] input) {
digest.update(input);
return this;
}
public Digester update(byte[] input, int offset, int len) {
digest.update(input, offset, len);
return this;
}
public Digester reset() {
digest.reset();
return this;
}
public Digester reset(String input) {
return reset().update(input);
}
public Digester reset(byte[] input) {
return reset().update(input);
}
public Digester reset(byte[] input, int offset, int len) {
return reset().update(input, offset, len);
}
public String getDigestValue() {
return encoder.encodeToString(digest.digest());
}
}