2323import javax .crypto .SecretKeyFactory ;
2424import javax .crypto .Mac ;
2525
26+ import org .spongycastle .crypto .Digest ;
27+ import org .spongycastle .crypto .digests .SHA1Digest ;
28+ import org .spongycastle .crypto .digests .SHA256Digest ;
2629import org .spongycastle .crypto .digests .SHA512Digest ;
2730import org .spongycastle .crypto .generators .PKCS5S2ParametersGenerator ;
2831import org .spongycastle .crypto .params .KeyParameter ;
4043
4144public class RCTAes extends ReactContextBaseJavaModule {
4245
43- private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding" ;
46+ private static final String CIPHER_CBC_ALGORITHM = "AES/CBC/PKCS7Padding" ;
47+ private static final String CIPHER_CTR_ALGORITHM = "AES/CTR/PKCS5Padding" ;
4448 public static final String HMAC_SHA_256 = "HmacSHA256" ;
4549 public static final String HMAC_SHA_512 = "HmacSHA512" ;
4650 private static final String KEY_ALGORITHM = "AES" ;
@@ -57,7 +61,7 @@ public String getName() {
5761 @ ReactMethod
5862 public void encrypt (String data , String key , String iv , String algorithm , Promise promise ) {
5963 try {
60- String result = encrypt (data , key , iv );
64+ String result = encrypt (data , key , iv , algorithm . toLowerCase (). contains ( "cbc" )? CIPHER_CBC_ALGORITHM : CIPHER_CTR_ALGORITHM );
6165 promise .resolve (result );
6266 } catch (Exception e ) {
6367 promise .reject ("-1" , e .getMessage ());
@@ -67,17 +71,22 @@ public void encrypt(String data, String key, String iv, String algorithm, Promis
6771 @ ReactMethod
6872 public void decrypt (String data , String pwd , String iv , String algorithm , Promise promise ) {
6973 try {
70- String strs = decrypt (data , pwd , iv );
74+ String strs = decrypt (data , pwd , iv , algorithm . toLowerCase (). contains ( "cbc" )? CIPHER_CBC_ALGORITHM : CIPHER_CTR_ALGORITHM );
7175 promise .resolve (strs );
7276 } catch (Exception e ) {
7377 promise .reject ("-1" , e .getMessage ());
7478 }
7579 }
7680
81+ @ ReactMethod (isBlockingSynchronousMethod = true )
82+ public String pbkdf2Sync (String pwd , String salt , Integer cost , Integer length , String algorithm ) throws UnsupportedEncodingException , NoSuchAlgorithmException , InvalidKeySpecException {
83+ return pbkdf2 (pwd , salt , cost , length , algorithm );
84+ }
85+
7786 @ ReactMethod
78- public void pbkdf2 (String pwd , String salt , Integer cost , Integer length , Promise promise ) {
87+ public void pbkdf2 (String pwd , String salt , Integer cost , Integer length , String algorithm , Promise promise ) {
7988 try {
80- String strs = pbkdf2 (pwd , salt , cost , length );
89+ String strs = pbkdf2 (pwd , salt , cost , length , algorithm );
8190 promise .resolve (strs );
8291 } catch (Exception e ) {
8392 promise .reject ("-1" , e .getMessage ());
@@ -174,11 +183,20 @@ public static String bytesToHex(byte[] bytes) {
174183 }
175184 return new String (hexChars );
176185 }
177-
178- private static String pbkdf2 (String pwd , String salt , Integer cost , Integer length )
186+ private static String pbkdf2 (String pwd , String salt , Integer cost , Integer length , String algorithm )
179187 throws NoSuchAlgorithmException , InvalidKeySpecException , UnsupportedEncodingException
180188 {
181- PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator (new SHA512Digest ());
189+ Digest algorithmDigest = new SHA512Digest ();
190+ if (algorithm .equalsIgnoreCase ("sha1" )){
191+ algorithmDigest = new SHA1Digest ();
192+ }
193+ if (algorithm .equalsIgnoreCase ("sha256" )){
194+ algorithmDigest = new SHA256Digest ();
195+ }
196+ if (algorithm .equalsIgnoreCase ("sha512" )){
197+ algorithmDigest = new SHA512Digest ();
198+ }
199+ PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator (algorithmDigest );
182200 gen .init (pwd .getBytes ("UTF_8" ), salt .getBytes ("UTF_8" ), cost );
183201 byte [] key = ((KeyParameter ) gen .generateDerivedParameters (length )).getKey ();
184202 return bytesToHex (key );
0 commit comments