2020-05-06 10:55:09 +08:00
|
|
|
/**
|
2020-05-06 11:57:45 +08:00
|
|
|
* @fileOverview wraps a secure native random number generator.
|
2020-05-11 11:21:28 +01:00
|
|
|
* This module mimics the node crypto api and is intended to work in RN environment.
|
2020-05-06 10:55:09 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
const { NativeModules } = require('react-native');
|
|
|
|
const { RNRandomBytes } = NativeModules;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate cryptographically secure random bytes using native api.
|
2020-05-06 11:57:45 +08:00
|
|
|
* @param {number} size The number of bytes of randomness
|
|
|
|
* @param {function} callback The callback to return the bytes
|
|
|
|
* @return {Buffer} The random bytes
|
2020-05-06 10:55:09 +08:00
|
|
|
*/
|
|
|
|
exports.randomBytes = (size, callback) => {
|
|
|
|
RNRandomBytes.randomBytes(size, (err, bytes) => {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
callback(null, Buffer.from(bytes, 'base64'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|