@react-native-module/get-random-values
Version:
implementation of getRandomValues for React Native
27 lines (24 loc) • 1.23 kB
JavaScript
import fastBase64Decode from '../node_modules/fast-base64-decode/index.js';
import { TypeMismatchError, QuotaExceededError } from './errors.js';
import { getRandomBase64 } from './lib.js';
// Web API Same
function getRandomValues(array) {
// For web
// If you're running react-native debug mode (Chrome debug)
// calling request is replaced globalThis.crypto.getRandomValues
// If you ignore this, you may get error on getRandomBase64
if (globalThis.crypto) {
if (globalThis.crypto.getRandomValues) {
return globalThis.crypto.getRandomValues(array);
}
}
if (!(array instanceof Int8Array || array instanceof Uint8Array || array instanceof Int16Array || array instanceof Uint16Array || array instanceof Int32Array || array instanceof Uint32Array || array instanceof Uint8ClampedArray)) {
throw new TypeMismatchError('Expected an integer array');
}
if (array.byteLength > 65536) {
throw new QuotaExceededError('Can only request a maximum of 65536 bytes');
}
fastBase64Decode(getRandomBase64(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength));
return array;
}
export { getRandomValues };