diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a51d4a..33c2989 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- We added MxEncryption.getRandomHex(byteLength) to the encryption module, which returns secure random values. + ## [v1.0.0] - 2026-11-04 - We upgraded the react-native to version 0.88.0 and bumped related dependencies. diff --git a/android/src/main/java/com/mendixnative/encryption/MxEncryptionModule.kt b/android/src/main/java/com/mendixnative/encryption/MxEncryptionModule.kt index 9c4f9e7..bec52a8 100644 --- a/android/src/main/java/com/mendixnative/encryption/MxEncryptionModule.kt +++ b/android/src/main/java/com/mendixnative/encryption/MxEncryptionModule.kt @@ -5,6 +5,7 @@ import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.module.annotations.ReactModule import com.mendix.mendixnative.encryption.MendixEncryptedStorageModule import com.mendixnative.NativeMxEncryptionSpec +import java.security.SecureRandom @ReactModule(name = MxEncryptionModule.NAME) class MxEncryptionModule(reactContext: ReactApplicationContext) : @@ -34,7 +35,18 @@ class MxEncryptionModule(reactContext: ReactApplicationContext) : return encryptedStorage.isEncrypted } + override fun getRandomHex(byteLength: Double): String { + val length = byteLength.toInt() + require(byteLength == length.toDouble() && length in 1..MAX_RANDOM_BYTES) { + "byteLength must be an integer between 1 and $MAX_RANDOM_BYTES" + } + val bytes = ByteArray(length) + SecureRandom().nextBytes(bytes) + return bytes.joinToString("") { "%02x".format(it) } + } + companion object { const val NAME = "MxEncryption" + private const val MAX_RANDOM_BYTES = 1024 } } diff --git a/ios/TurboModules/MxEncryption/MxEncryption.mm b/ios/TurboModules/MxEncryption/MxEncryption.mm index 6f029e6..bf07e88 100644 --- a/ios/TurboModules/MxEncryption/MxEncryption.mm +++ b/ios/TurboModules/MxEncryption/MxEncryption.mm @@ -1,6 +1,7 @@ #import "MxEncryption.h" #import "RCTAppDelegate.h" #import +#import #import "MendixNative-Swift.h" @implementation MxEncryption @@ -47,4 +48,29 @@ - (nonnull NSNumber *)isEncrypted { return [NSNumber numberWithBool: [EncryptedStorage isEncrypted]]; } +- (NSString *)getRandomHex:(double)byteLength { + static const NSUInteger maxRandomBytes = 1024; + NSUInteger length = (NSUInteger)byteLength; + if (byteLength != (double)length || length < 1 || length > maxRandomBytes) { + @throw [NSException exceptionWithName:NSInvalidArgumentException + reason:@"byteLength must be an integer between 1 and 1024" + userInfo:nil]; + } + + NSMutableData *data = [NSMutableData dataWithLength:length]; + int status = SecRandomCopyBytes(kSecRandomDefault, length, data.mutableBytes); + if (status != errSecSuccess) { + @throw [NSException exceptionWithName:@"MxEncryptionRandomError" + reason:[NSString stringWithFormat:@"SecRandomCopyBytes failed with status %d", status] + userInfo:nil]; + } + + NSMutableString *hex = [NSMutableString stringWithCapacity:length * 2]; + const uint8_t *bytes = static_cast(data.bytes); + for (NSUInteger i = 0; i < length; i++) { + [hex appendFormat:@"%02x", bytes[i]]; + } + return hex; +} + @end diff --git a/src/encryption/NativeMxEncryption.ts b/src/encryption/NativeMxEncryption.ts index 02975af..e1aca3c 100644 --- a/src/encryption/NativeMxEncryption.ts +++ b/src/encryption/NativeMxEncryption.ts @@ -6,6 +6,7 @@ export interface Spec extends TurboModule { removeItem(key: string): Promise; clear(): Promise; isEncrypted(): boolean; + getRandomHex(byteLength: number): string; } export default TurboModuleRegistry.getEnforcing('MxEncryption'); diff --git a/src/encryption/index.ts b/src/encryption/index.ts index 2186d79..7cefbed 100644 --- a/src/encryption/index.ts +++ b/src/encryption/index.ts @@ -16,4 +16,13 @@ export const MxEncryption = { isEncrypted(): boolean { return NativeMxEncryption.isEncrypted(); }, + /** + * Returns `byteLength` bytes from the platform's cryptographically secure random generator + * (SecRandomCopyBytes on iOS, SecureRandom on Android), lowercase hex encoded + * (2 characters per byte). Throws if `byteLength` is not an integer between 1 and 1024, + * or if the secure generator fails. It never falls back to a weaker source. + */ + getRandomHex(byteLength: number): string { + return NativeMxEncryption.getRandomHex(byteLength); + }, };