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