crypto/random: minor. comments.

This commit is contained in:
Christopher Jeffrey 2017-07-17 22:22:52 -07:00
parent 76679a0650
commit 76eb6b354b
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 10 additions and 7 deletions

View File

@ -14,21 +14,21 @@
const crypto = global.crypto || global.msCrypto || {};
/**
* Generate some random bytes.
* Generate pseudo-random bytes.
* @param {Number} size
* @returns {Buffer}
*/
exports.randomBytes = function randomBytes(n) {
let data = new Uint8Array(n);
exports.randomBytes = function randomBytes(size) {
let data = new Uint8Array(size);
crypto.getRandomValues(data);
return Buffer.from(data.buffer);
};
if (!crypto.getRandomValues) {
// Out of luck here. Use bad randomness for now.
exports.randomBytes = function randomBytes(n) {
let data = Buffer.allocUnsafe(n);
exports.randomBytes = function randomBytes(size) {
let data = Buffer.allocUnsafe(size);
for (let i = 0; i < data.length; i++)
data[i] = Math.floor(Math.random() * 256);

View File

@ -12,8 +12,11 @@
const crypto = require('crypto');
/*
* Misc
/**
* Generate pseudo-random bytes.
* @function
* @param {Number} size
* @returns {Buffer}
*/
exports.randomBytes = crypto.randomBytes;