Skip to content

Commit 8662766

Browse files
committed
Sha-1 for Android also
1 parent cd624e7 commit 8662766

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

android/src/main/java/com/reactlibrary/Sha256Module.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@ public String getName() {
2828
return "sha256Lib";
2929
}
3030

31+
String buildHash(final String toHash, final String algo, final Integer length) throws NoSuchAlgorithmException, UnsupportedEncodingException {
32+
MessageDigest md = MessageDigest.getInstance(algo);
33+
md.update(toHash.getBytes("UTF-8"));
34+
byte[] digest = md.digest();
35+
return String.format("%0" + length.toString() + "x", new java.math.BigInteger(1, digest));
36+
}
37+
38+
3139
@ReactMethod
3240
public void sha256(final String toHash, Promise promise) {
33-
MessageDigest md = null;
3441
try {
35-
md = MessageDigest.getInstance("SHA-256");
36-
md.update(toHash.getBytes("UTF-8"));
37-
byte[] digest = md.digest();
38-
String hash = String.format("%064x", new java.math.BigInteger(1, digest));
42+
String hash = buildHash(toHash, "SHA-256", 64);
3943
promise.resolve(hash);
40-
4144
} catch (NoSuchAlgorithmException e) {
4245
e.printStackTrace();
4346
promise.reject("sha256", e.getMessage());
@@ -47,4 +50,18 @@ public void sha256(final String toHash, Promise promise) {
4750
}
4851
}
4952

53+
@ReactMethod
54+
public void sha1(final String toHash, Promise promise) {
55+
try {
56+
String hash = buildHash(toHash, "SHA-1", 40);
57+
promise.resolve(hash);
58+
} catch (NoSuchAlgorithmException e) {
59+
e.printStackTrace();
60+
promise.reject("sha1", e.getMessage());
61+
} catch (UnsupportedEncodingException e) {
62+
e.printStackTrace();
63+
promise.reject("sha1", e.getMessage());
64+
}
65+
}
66+
5067
}

0 commit comments

Comments
 (0)