Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sha1_hex and sha256_hex #15

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion gjavac-core/src/main/kotlin/gjavac/lib/UvmCoreLibs.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gjavac.lib;

import com.google.gson.Gson;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class UvmCoreLibs {
Expand All @@ -27,6 +29,31 @@ public static String tojsonstring(Object obj) {
}
}

public static String sha_hex(String s, String sha_encoding_method) {
try {
MessageDigest md = MessageDigest.getInstance(sha_encoding_method);

byte[] sha_result = md.digest(s.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < sha_result.length; i++) {
sb.append(Integer.toString((sha_result[i] & 0xff) + 0x100, 16).substring(1));
}

return sb.toString();
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

public static String sha1_hex(String s) {
return sha_hex(s, "SHA-1");
}

public static String sha256_hex(String s) {
return sha_hex(s, "SHA-256");
}

public static void pprint(Object obj) {
System.out.println(tojsonstring(obj));
}
Expand Down