@onekeyfe/react-native-get-random-values 1.1.20 → 1.1.22
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.
package/android/build.gradle
CHANGED
|
@@ -126,5 +126,5 @@ dependencies {
|
|
|
126
126
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
127
127
|
implementation project(":react-native-nitro-modules")
|
|
128
128
|
|
|
129
|
-
implementation project(":
|
|
129
|
+
implementation project(":onekeyfe_react-native-native-logger")
|
|
130
130
|
}
|
|
@@ -5,16 +5,22 @@ import com.margelo.nitro.nativelogger.OneKeyLog
|
|
|
5
5
|
|
|
6
6
|
@DoNotStrip
|
|
7
7
|
class ReactNativeGetRandomValues : HybridReactNativeGetRandomValuesSpec() {
|
|
8
|
+
companion object {
|
|
9
|
+
private const val MAX_BYTE_LENGTH = 65536 // 64 KB upper bound
|
|
10
|
+
}
|
|
11
|
+
|
|
8
12
|
override fun getRandomBase64(byteLength: Double): String {
|
|
13
|
+
val length = byteLength.toInt()
|
|
14
|
+
if (length <= 0 || length > MAX_BYTE_LENGTH) {
|
|
15
|
+
OneKeyLog.warn("RandomValues", "Invalid byteLength: $byteLength, must be 1..$MAX_BYTE_LENGTH")
|
|
16
|
+
throw IllegalArgumentException("Invalid byteLength: must be 1...$MAX_BYTE_LENGTH")
|
|
17
|
+
}
|
|
9
18
|
try {
|
|
10
|
-
val data = ByteArray(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
random.nextBytes(data)
|
|
14
|
-
|
|
19
|
+
val data = ByteArray(length)
|
|
20
|
+
java.security.SecureRandom().nextBytes(data)
|
|
15
21
|
return android.util.Base64.encodeToString(data, android.util.Base64.NO_WRAP)
|
|
16
22
|
} catch (e: Exception) {
|
|
17
|
-
OneKeyLog.error("RandomValues", "SecureRandom failed
|
|
23
|
+
OneKeyLog.error("RandomValues", "SecureRandom failed")
|
|
18
24
|
throw e
|
|
19
25
|
}
|
|
20
26
|
}
|
|
@@ -2,17 +2,21 @@ import NitroModules
|
|
|
2
2
|
import ReactNativeNativeLogger
|
|
3
3
|
|
|
4
4
|
class ReactNativeGetRandomValues: HybridReactNativeGetRandomValuesSpec {
|
|
5
|
+
private static let maxByteLength = 65536 // 64 KB upper bound
|
|
6
|
+
|
|
5
7
|
func getRandomBase64(byteLength: Double) throws -> String {
|
|
6
8
|
let length = Int(byteLength)
|
|
7
|
-
guard length > 0,
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
guard length > 0, length <= ReactNativeGetRandomValues.maxByteLength,
|
|
10
|
+
let data = NSMutableData(length: length) else {
|
|
11
|
+
OneKeyLog.warn("RandomValues", "Invalid byteLength: \(byteLength), must be 1...\(ReactNativeGetRandomValues.maxByteLength)")
|
|
12
|
+
throw NSError(domain: "ReactNativeGetRandomValues", code: -1,
|
|
13
|
+
userInfo: [NSLocalizedDescriptionKey: "Invalid byteLength: must be 1...\(ReactNativeGetRandomValues.maxByteLength)"])
|
|
10
14
|
}
|
|
11
15
|
let result = SecRandomCopyBytes(kSecRandomDefault, length, data.mutableBytes)
|
|
12
16
|
if result != errSecSuccess {
|
|
13
17
|
OneKeyLog.error("RandomValues", "SecRandomCopyBytes failed with status: \(result)")
|
|
14
18
|
throw NSError(domain: "ReactNativeGetRandomValues", code: Int(result), userInfo: nil)
|
|
15
19
|
}
|
|
16
|
-
return data.base64EncodedString(options:
|
|
20
|
+
return data.base64EncodedString(options: [])
|
|
17
21
|
}
|
|
18
22
|
}
|