@react-native-module/randombytes
Version:
implementation of randomBytes for React Native
23 lines (20 loc) • 753 B
JavaScript
import { Buffer } from 'buffer';
import { MAX_BYTES } from './constants.js';
import { Random } from 'random-js';
function randomBytesWithoutNativeModule(size, callback) {
const random = new Random();
const randomHex = random.hex(size * 2);
let randomBytes = Buffer.from(randomHex, 'hex');
// this is the max bytes crypto.getRandomValues
// https://github.com/crypto-browserify/randombytes/blob/f18ded32b209f0d4c637608a11ae042ae96b4c2e/browser.js#L31
if (size > MAX_BYTES) {
randomBytes = Buffer.from(randomBytes.subarray(0, MAX_BYTES));
}
if (callback != null) {
callback(null, randomBytes);
}
else {
return randomBytes;
}
}
export { randomBytesWithoutNativeModule };