Checksum.java
1.76 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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.granite.contexthub.impl;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
public class Checksum {
public static final String DEFAULT_ALGORITHM = "SHA-1";
public static final String INPUT_ENCODING = "UTF-8";
MessageDigest messageDigest;
LinkedList<String> errors = new LinkedList();
public Checksum(String algorithm) {
this.messageDigest = this.getInstance(algorithm);
if (this.isInitialized()) {
this.messageDigest.reset();
}
}
public Checksum() {
this("SHA-1");
}
public boolean isInitialized() {
return this.messageDigest != null;
}
private MessageDigest getInstance(String algorithm) {
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance(algorithm);
}
catch (NoSuchAlgorithmException exception) {
messageDigest = null;
}
return messageDigest;
}
public void update(String input) {
if (input == null) {
return;
}
try {
this.messageDigest.update(input.getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e) {
this.errors.add(String.format("Error while updating checksum: %s", e.toString()));
}
}
public String toString() {
StringBuilder checksum = new StringBuilder();
if (!this.isInitialized()) {
return "";
}
for (byte el : this.messageDigest.digest()) {
checksum.append(String.format("%1$02x", Byte.valueOf(el)));
}
return checksum.toString();
}
}