2020-05-09 09:21:45 +02:00
|
|
|
/**
|
|
|
|
* @fileOverview creates an rng module that will bring all calls to 'crypto'
|
|
|
|
* into one place to try and prevent mistakes when touching the crypto code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import crypto from 'crypto';
|
2020-05-11 12:25:37 +02:00
|
|
|
// uses `crypto` module under nodejs/cli and shim under RN
|
|
|
|
// @see blue_modules/crypto.js
|
|
|
|
|
2020-05-09 09:21:45 +02:00
|
|
|
/**
|
|
|
|
* Generate cryptographically secure random bytes using native api.
|
|
|
|
* @param {number} size The number of bytes of randomness
|
|
|
|
* @return {Promise.<Buffer>} The random bytes
|
|
|
|
*/
|
2020-05-09 10:30:39 +02:00
|
|
|
export async function randomBytes(size) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-05-09 09:21:45 +02:00
|
|
|
crypto.randomBytes(size, (err, data) => {
|
2020-05-09 10:30:39 +02:00
|
|
|
if (err) reject(err);
|
|
|
|
else resolve(data);
|
2020-05-09 09:21:45 +02:00
|
|
|
});
|
|
|
|
});
|
2020-05-09 10:30:39 +02:00
|
|
|
}
|