From 68ce15d0786bbbf9a77fb5639627cf7215f60d59 Mon Sep 17 00:00:00 2001 From: onovy Date: Sun, 5 Jan 2014 12:42:34 +0100 Subject: [PATCH] Use webcrypto for whole seed initializing. --- bitaddress.org.html | 25 +++++++++++++++---------- src/securerandom.js | 25 +++++++++++++++---------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/bitaddress.org.html b/bitaddress.org.html index 585c6d7..c3dde7e 100644 --- a/bitaddress.org.html +++ b/bitaddress.org.html @@ -1613,21 +1613,26 @@ if (typeof Crypto == "undefined" || !Crypto.util) { if (sr.pool == null) { sr.pool = new Array(); sr.pptr = 0; - var t; + + // Use webcrypto if available if (window.crypto && window.crypto.getRandomValues) { try { - // Use webcrypto if available - var ua = new Uint8Array(32); + var ua = new Uint8Array(sr.poolSize - sr.pptr); window.crypto.getRandomValues(ua); - for (t = 0; t < 32; ++t) - sr.pool[sr.pptr++] = ua[t]; + var i = 0; + while (sr.pptr < sr.poolSize) { + sr.pool[sr.pptr++] = ua[i++]; + } } catch (e) { alert(e); } + } else { + var t; + while (sr.pptr < sr.poolSize) { // extract some randomness from Math.random() + t = Math.floor(65536 * Math.random()); + sr.pool[sr.pptr++] = t >>> 8; + sr.pool[sr.pptr++] = t & 255; + } } - while (sr.pptr < sr.poolSize) { // extract some randomness from Math.random() - t = Math.floor(65536 * Math.random()); - sr.pool[sr.pptr++] = t >>> 8; - sr.pool[sr.pptr++] = t & 255; - } + sr.pptr = 0; sr.seedTime(); // entropy diff --git a/src/securerandom.js b/src/securerandom.js index 4d1d800..2d9b3be 100644 --- a/src/securerandom.js +++ b/src/securerandom.js @@ -126,21 +126,26 @@ if (sr.pool == null) { sr.pool = new Array(); sr.pptr = 0; - var t; + + // Use webcrypto if available if (window.crypto && window.crypto.getRandomValues) { try { - // Use webcrypto if available - var ua = new Uint8Array(32); + var ua = new Uint8Array(sr.poolSize - sr.pptr); window.crypto.getRandomValues(ua); - for (t = 0; t < 32; ++t) - sr.pool[sr.pptr++] = ua[t]; + var i = 0; + while (sr.pptr < sr.poolSize) { + sr.pool[sr.pptr++] = ua[i++]; + } } catch (e) { alert(e); } + } else { + var t; + while (sr.pptr < sr.poolSize) { // extract some randomness from Math.random() + t = Math.floor(65536 * Math.random()); + sr.pool[sr.pptr++] = t >>> 8; + sr.pool[sr.pptr++] = t & 255; + } } - while (sr.pptr < sr.poolSize) { // extract some randomness from Math.random() - t = Math.floor(65536 * Math.random()); - sr.pool[sr.pptr++] = t >>> 8; - sr.pool[sr.pptr++] = t & 255; - } + sr.pptr = 0; sr.seedTime(); // entropy