Move random.js module to blue_modules/crypto.js

This commit is contained in:
Tankred Hase 2020-05-06 10:55:09 +08:00
parent 9c5321b622
commit 71d8e329b8
No known key found for this signature in database
GPG key ID: 8A9F1C42BFB264F6
2 changed files with 22 additions and 24 deletions

22
blue_modules/crypto.js Normal file
View file

@ -0,0 +1,22 @@
/**
* @fileOverview wrap a secure native random number generator
*/
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 number of bytes of randomness
* @return {undefined}
*/
exports.randomBytes = (size, callback) => {
RNRandomBytes.randomBytes(size, (err, bytes) => {
if (err) {
callback(err);
} else {
callback(null, Buffer.from(bytes, 'base64'));
}
});
};

View file

@ -1,24 +0,0 @@
/**
* @fileOverview wrap a secure native random number generator
*/
import { NativeModules } from 'react-native';
const { RNRandomBytes } = NativeModules;
/**
* Generate cryptographically secure random bytes using native api.
* @param {number} size The number of bytes of randomness
* @return {Promise<Buffer>} The random bytes
*/
export async function randomBytes(size) {
return new Promise((resolve, reject) => {
RNRandomBytes.randomBytes(size, (err, bytes) => {
if (err) {
reject(err);
} else {
resolve(Buffer.from(bytes, 'base64'));
}
});
});
}