Added check for crypto into getRandomValue().

This commit is contained in:
Douglas Muth 2017-02-01 21:55:38 -05:00
parent 5cb69f140f
commit 61fb6fa227

11
main.js
View file

@ -29,10 +29,21 @@ Diceware.getRandomValue = function(max) {
return(NaN); return(NaN);
} }
if (Diceware.i_can_has_good_crypto()) {
var a = new Uint32Array(1); var a = new Uint32Array(1);
window.crypto.getRandomValues(a); window.crypto.getRandomValues(a);
retval = (a[0] % max); retval = (a[0] % max);
} else {
//
// Fall back to something way less secure. The user has already
// been warned.
//
retval = Math.floor(Math.random() * max);
}
return(retval); return(retval);
} // End of getRandomValue() } // End of getRandomValue()