-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathStringUtil.java
More file actions
42 lines (32 loc) · 1.2 KB
/
StringUtil.java
File metadata and controls
42 lines (32 loc) · 1.2 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
package noobchain;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import com.google.gson.GsonBuilder;
public class StringUtil {
//Applies Sha256 to a string and returns the result.
public static String applySha256(String input){
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Applies sha256 to our input,
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder hexString = new StringBuilder(); // This will contain hash as hexidecimal
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
catch(Exception e) {
throw new RuntimeException(e);
}
}
//Short hand helper to turn Object into a json string
public static String getJson(Object o) {
return new GsonBuilder().setPrettyPrinting().create().toJson(o);
}
//Returns difficulty string target, to compare to hash. eg difficulty of 5 will return "00000"
public static String getDifficultyString(int difficulty) {
return "0".repeat(difficulty);
}
}