From 61fb6fa227909aa4105a8a97ee918a36ccbb24c7 Mon Sep 17 00:00:00 2001 From: Douglas Muth Date: Wed, 1 Feb 2017 21:55:38 -0500 Subject: [PATCH] Added check for crypto into getRandomValue(). --- main.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 2251282..6dd3520 100644 --- a/main.js +++ b/main.js @@ -29,9 +29,20 @@ Diceware.getRandomValue = function(max) { return(NaN); } - var a = new Uint32Array(1); - window.crypto.getRandomValues(a); - retval = (a[0] % max); + if (Diceware.i_can_has_good_crypto()) { + var a = new Uint32Array(1); + window.crypto.getRandomValues(a); + 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);