Change internal representation of random data from Uint32Array to Uint8Array
This commit is contained in:
parent
37213e1654
commit
abf0545208
|
@ -60,30 +60,37 @@ module.exports = {
|
||||||
* @return {Integer} A secure random number
|
* @return {Integer} A secure random number
|
||||||
*/
|
*/
|
||||||
getSecureRandom: function(from, to) {
|
getSecureRandom: function(from, to) {
|
||||||
var buf = new Uint32Array(1);
|
var randUint = this.getSecureRandomUint();
|
||||||
this.getRandomValues(buf);
|
|
||||||
var bits = ((to - from)).toString(2).length;
|
var bits = ((to - from)).toString(2).length;
|
||||||
while ((buf[0] & (Math.pow(2, bits) - 1)) > (to - from))
|
while ((randUint & (Math.pow(2, bits) - 1)) > (to - from)) {
|
||||||
this.getRandomValues(buf);
|
randUint = this.getSecureRandomUint();
|
||||||
return from + (Math.abs(buf[0] & (Math.pow(2, bits) - 1)));
|
}
|
||||||
|
return from + (Math.abs(randUint & (Math.pow(2, bits) - 1)));
|
||||||
},
|
},
|
||||||
|
|
||||||
getSecureRandomOctet: function() {
|
getSecureRandomOctet: function() {
|
||||||
var buf = new Uint32Array(1);
|
var buf = new Uint8Array(1);
|
||||||
this.getRandomValues(buf);
|
this.getRandomValues(buf);
|
||||||
return buf[0] & 0xFF;
|
return buf[0];
|
||||||
|
},
|
||||||
|
|
||||||
|
getSecureRandomUint: function() {
|
||||||
|
var buf = new Uint8Array(4);
|
||||||
|
var dv = new DataView(buf.buffer);
|
||||||
|
this.getRandomValues(buf);
|
||||||
|
return dv.getUint32(0);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper routine which calls platform specific crypto random generator
|
* Helper routine which calls platform specific crypto random generator
|
||||||
* @param {Uint32Array} buf
|
* @param {Uint8Array} buf
|
||||||
*/
|
*/
|
||||||
getRandomValues: function(buf) {
|
getRandomValues: function(buf) {
|
||||||
if (typeof window !== 'undefined' && window.crypto) {
|
if (typeof window !== 'undefined' && window.crypto) {
|
||||||
window.crypto.getRandomValues(buf);
|
window.crypto.getRandomValues(buf);
|
||||||
} else if (nodeCrypto) {
|
} else if (nodeCrypto) {
|
||||||
var bytes = nodeCrypto.randomBytes(4);
|
var bytes = nodeCrypto.randomBytes(buf.length);
|
||||||
buf[0] = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
|
buf.set(bytes);
|
||||||
} else if (this.randomBuffer.buffer) {
|
} else if (this.randomBuffer.buffer) {
|
||||||
this.randomBuffer.get(buf);
|
this.randomBuffer.get(buf);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -76,13 +76,13 @@ AsyncProxy.prototype.seedRandom = function(size) {
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Uint32Array with random numbers
|
* Get Uint8Array with random numbers
|
||||||
* @param {Integer} size Length of buffer
|
* @param {Integer} size Length of buffer
|
||||||
* @return {Uint32Array}
|
* @return {Uint8Array}
|
||||||
*/
|
*/
|
||||||
AsyncProxy.prototype.getRandomBuffer = function(size) {
|
AsyncProxy.prototype.getRandomBuffer = function(size) {
|
||||||
if (!size) return null;
|
if (!size) return null;
|
||||||
var buf = new Uint32Array(size);
|
var buf = new Uint8Array(size);
|
||||||
crypto.random.getRandomValues(buf);
|
crypto.random.getRandomValues(buf);
|
||||||
return buf;
|
return buf;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user