@@ -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