mirror of
https://github.com/status-im/react-native-securerandom.git
synced 2026-08-27 17:11:14 +00:00
19 lines
692 B
JavaScript
19 lines
692 B
JavaScript
/* @flow */
|
|
|
|
import { NativeModules, Platform } from 'react-native';
|
|
import { toByteArray } from 'base64-js';
|
|
|
|
const { RNSecureRandom } = NativeModules;
|
|
|
|
export function generateSecureRandom(length: number): Promise<Uint8Array> {
|
|
if (Platform.OS !== 'ios' && Platform.OS !== 'android' && Platform.OS !== 'desktop') {
|
|
throw Error('react-native-securerandom is currently only available for iOS, Android and Desktop');
|
|
}
|
|
|
|
if (!RNSecureRandom || !RNSecureRandom.generateSecureRandomAsBase64) {
|
|
throw Error('react-native-securerandom is not properly linked');
|
|
}
|
|
|
|
return RNSecureRandom.generateSecureRandomAsBase64(length).then(base64 => toByteArray(base64));
|
|
}
|