Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
Expand Down Expand Up @@ -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
}
}
26 changes: 26 additions & 0 deletions ios/TurboModules/MxEncryption/MxEncryption.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "MxEncryption.h"
#import "RCTAppDelegate.h"
#import <React/RCTReloadCommand.h>
#import <Security/Security.h>
#import "MendixNative-Swift.h"

@implementation MxEncryption
Expand Down Expand Up @@ -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<const uint8_t *>(data.bytes);
for (NSUInteger i = 0; i < length; i++) {
[hex appendFormat:@"%02x", bytes[i]];
}
return hex;
}

@end
1 change: 1 addition & 0 deletions src/encryption/NativeMxEncryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Spec extends TurboModule {
removeItem(key: string): Promise<void>;
clear(): Promise<void>;
isEncrypted(): boolean;
getRandomHex(byteLength: number): string;
}

export default TurboModuleRegistry.getEnforcing<Spec>('MxEncryption');
9 changes: 9 additions & 0 deletions src/encryption/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};
Loading