|
@@ -1,43 +1,17 @@
|
|
|
-package pbkdf2;
|
|
|
+package rnpbkdf2;
|
|
|
|
|
|
-import android.widget.Toast;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
-import java.security.SecureRandom;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import java.util.UUID;
|
|
|
-
|
|
|
-import java.security.MessageDigest;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
-import java.security.spec.InvalidKeySpecException;
|
|
|
-import java.security.InvalidKeyException;
|
|
|
-
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-
|
|
|
-import javax.crypto.Cipher;
|
|
|
-import javax.crypto.SecretKey;
|
|
|
-import javax.crypto.spec.SecretKeySpec;
|
|
|
-import javax.crypto.spec.IvParameterSpec;
|
|
|
-import javax.crypto.spec.PBEKeySpec;
|
|
|
-import javax.crypto.SecretKeyFactory;
|
|
|
-import javax.crypto.Mac;
|
|
|
+import com.facebook.react.bridge.Promise;
|
|
|
+import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
|
+import com.facebook.react.bridge.ReactMethod;
|
|
|
|
|
|
import org.spongycastle.crypto.digests.SHA512Digest;
|
|
|
import org.spongycastle.crypto.generators.PKCS5S2ParametersGenerator;
|
|
|
import org.spongycastle.crypto.params.KeyParameter;
|
|
|
-import org.spongycastle.util.encoders.Hex;
|
|
|
|
|
|
-import android.util.Base64;
|
|
|
-
|
|
|
-import com.facebook.react.bridge.NativeModule;
|
|
|
-import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
-import com.facebook.react.bridge.Promise;
|
|
|
-import com.facebook.react.bridge.ReactContext;
|
|
|
-import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
|
-import com.facebook.react.bridge.ReactMethod;
|
|
|
-import com.facebook.react.bridge.Callback;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.spec.InvalidKeySpecException;
|
|
|
|
|
|
public class PBKDF2 extends ReactContextBaseJavaModule {
|
|
|
|
|
@@ -47,25 +21,10 @@ public class PBKDF2 extends ReactContextBaseJavaModule {
|
|
|
super(reactContext);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public String getName() {
|
|
|
- return "PBKDF2";
|
|
|
- }
|
|
|
-
|
|
|
- @ReactMethod
|
|
|
- public void derivationKey(String pwd, String salt, Integer iterations, Promise promise) {
|
|
|
- try {
|
|
|
- String strs = pbkdf2(pwd, salt, iterations, SHA512_DIGEST_LENGTH);
|
|
|
- promise.resolve(strs);
|
|
|
- } catch (Exception e) {
|
|
|
- promise.reject("-1", e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public static String bytesToHex(byte[] bytes) {
|
|
|
final char[] hexArray = "0123456789abcdef".toCharArray();
|
|
|
char[] hexChars = new char[bytes.length * 2];
|
|
|
- for ( int j = 0; j < bytes.length; j++ ) {
|
|
|
+ for (int j = 0; j < bytes.length; j++) {
|
|
|
int v = bytes[j] & 0xFF;
|
|
|
hexChars[j * 2] = hexArray[v >>> 4];
|
|
|
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
|
@@ -74,11 +33,25 @@ public class PBKDF2 extends ReactContextBaseJavaModule {
|
|
|
}
|
|
|
|
|
|
private static String derivationKey(String pwd, String salt, Integer cost, Integer length)
|
|
|
- throws NoSuchAlgorithmException, InvalidKeySpecException
|
|
|
- {
|
|
|
+ throws NoSuchAlgorithmException, InvalidKeySpecException {
|
|
|
PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA512Digest());
|
|
|
gen.init(pwd.getBytes(StandardCharsets.UTF_8), salt.getBytes(StandardCharsets.UTF_8), cost);
|
|
|
byte[] key = ((KeyParameter) gen.generateDerivedParameters(length)).getKey();
|
|
|
return bytesToHex(key);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getName() {
|
|
|
+ return "PBKDF2";
|
|
|
+ }
|
|
|
+
|
|
|
+ @ReactMethod
|
|
|
+ public void derivationKey(String pwd, String salt, Integer iterations, Promise promise) {
|
|
|
+ try {
|
|
|
+ String strs = derivationKey(pwd, salt, iterations, SHA512_DIGEST_LENGTH);
|
|
|
+ promise.resolve(strs);
|
|
|
+ } catch (Exception e) {
|
|
|
+ promise.reject("-1", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|