@onekeyfe/react-native-pbkdf2 3.0.7 → 3.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -74,4 +74,7 @@ def kotlin_version = getExtOrDefault("kotlinVersion")
74
74
  dependencies {
75
75
  implementation "com.facebook.react:react-android"
76
76
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
77
+ implementation "com.madgag.spongycastle:core:1.58.0.0"
78
+ implementation "com.madgag.spongycastle:prov:1.54.0.0"
79
+ implementation "com.madgag.spongycastle:pg:1.54.0.0"
77
80
  }
@@ -4,9 +4,17 @@ import android.util.Base64
4
4
  import com.facebook.react.bridge.Promise
5
5
  import com.facebook.react.bridge.ReactApplicationContext
6
6
  import com.facebook.react.module.annotations.ReactModule
7
- import javax.crypto.SecretKeyFactory
8
- import javax.crypto.spec.PBEKeySpec
7
+ import org.spongycastle.crypto.Digest
8
+ import org.spongycastle.crypto.digests.SHA1Digest
9
+ import org.spongycastle.crypto.digests.SHA256Digest
10
+ import org.spongycastle.crypto.digests.SHA512Digest
11
+ import org.spongycastle.crypto.generators.PKCS5S2ParametersGenerator
12
+ import org.spongycastle.crypto.params.KeyParameter
9
13
 
14
+ /**
15
+ * Ported from upstream react-native-fast-pbkdf2 Pbkdf2Module.java.
16
+ * Adapted to extend NativePbkdf2Spec (TurboModule).
17
+ */
10
18
  @ReactModule(name = Pbkdf2Module.NAME)
11
19
  class Pbkdf2Module(reactContext: ReactApplicationContext) :
12
20
  NativePbkdf2Spec(reactContext) {
@@ -25,31 +33,23 @@ class Pbkdf2Module(reactContext: ReactApplicationContext) :
25
33
  hash: String,
26
34
  promise: Promise
27
35
  ) {
28
- Thread {
29
- try {
30
- val passwordBytes = Base64.decode(password, Base64.DEFAULT)
31
- val saltBytes = Base64.decode(salt, Base64.DEFAULT)
32
- val iterationCount = rounds.toInt()
33
- val keyLengthBits = keyLength.toInt() * 8
36
+ try {
37
+ val decodedPassword = Base64.decode(password, Base64.DEFAULT)
38
+ val decodedSalt = Base64.decode(salt, Base64.DEFAULT)
34
39
 
35
- val algorithm = when (hash.uppercase()) {
36
- "SHA256", "SHA-256" -> "PBKDF2WithHmacSHA256"
37
- "SHA512", "SHA-512" -> "PBKDF2WithHmacSHA512"
38
- else -> "PBKDF2WithHmacSHA256"
39
- }
40
-
41
- val spec = PBEKeySpec(
42
- String(passwordBytes, Charsets.UTF_8).toCharArray(),
43
- saltBytes,
44
- iterationCount,
45
- keyLengthBits
46
- )
47
- val factory = SecretKeyFactory.getInstance(algorithm)
48
- val derivedKey = factory.generateSecret(spec).encoded
49
- promise.resolve(Base64.encodeToString(derivedKey, Base64.NO_WRAP))
50
- } catch (e: Exception) {
51
- promise.reject("PBKDF2_ERROR", e.message, e)
40
+ // Default to SHA1 (matching upstream original)
41
+ val digest: Digest = when (hash) {
42
+ "sha-256" -> SHA256Digest()
43
+ "sha-512" -> SHA512Digest()
44
+ else -> SHA1Digest()
52
45
  }
53
- }.start()
46
+
47
+ val gen = PKCS5S2ParametersGenerator(digest)
48
+ gen.init(decodedPassword, decodedSalt, rounds.toInt())
49
+ val key = (gen.generateDerivedParameters(keyLength.toInt() * 8) as KeyParameter).key
50
+ promise.resolve(Base64.encodeToString(key, Base64.DEFAULT))
51
+ } catch (e: Exception) {
52
+ promise.reject("PBKDF2_ERROR", e.message, e)
53
+ }
54
54
  }
55
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-pbkdf2",
3
- "version": "3.0.7",
3
+ "version": "3.0.8",
4
4
  "description": "react-native-pbkdf2",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",