-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrOpr.java
48 lines (35 loc) · 1.11 KB
/
StrOpr.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
package healthchain;
import java.security.MessageDigest;
import com.google.gson.GsonBuilder;
import java.security.MessageDigest;
public class StrOpr {
public static String applySha256(String input)
{
//Apply POW algo i.e. sha256
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Applies sha256 to input,
byte[] hash = digest.digest(input.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer(); //hash as hexidecimal
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
catch(Exception e) {
throw new RuntimeException(e);
}
}
//Convert object to Json string
public static String getJson(Object o)
{
return new GsonBuilder().setPrettyPrinting().create().toJson(o);
}
public static String getDificultyString(int difficulty)
{
//Return the Difficulty level in Blockchain
return new String(new char[difficulty]).replace('\0', '0');
}
}