BlueWallet/blue_modules/crypto.js
2020-05-11 11:21:28 +01:00

24 lines
719 B
JavaScript

/**
* @fileOverview wraps a secure native random number generator.
* This module mimics the node crypto api and is intended to work in RN environment.
*/
const { NativeModules } = require('react-native');
const { RNRandomBytes } = NativeModules;
/**
* Generate cryptographically secure random bytes using native api.
* @param {number} size The number of bytes of randomness
* @param {function} callback The callback to return the bytes
* @return {Buffer} The random bytes
*/
exports.randomBytes = (size, callback) => {
RNRandomBytes.randomBytes(size, (err, bytes) => {
if (err) {
callback(err);
} else {
callback(null, Buffer.from(bytes, 'base64'));
}
});
};