From d4ab05badabb6b874f84b599b6fca0db3ee66608 Mon Sep 17 00:00:00 2001 From: Jonathan Underwood Date: Thu, 16 Dec 2021 02:04:16 +0900 Subject: [PATCH] Use CSPRNG for private keys --- js/coin.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/js/coin.js b/js/coin.js index 84a1149..c3ce337 100644 --- a/js/coin.js +++ b/js/coin.js @@ -42,9 +42,24 @@ }; } + /* generate crypto secure random numbers (should exist on most browsers) */ + coinjs.randomBytesSafe = function(count){ + var cry = window.crypto || window.msCrypto; + if (!cry) return ''; + var buf = new Uint8Array(count); + cry.getRandomValues(buf); + var s = ''; + for (var i = 0; i < buf.length; i++) { + var h = buf[i].toString(16); + s += h.length === 1 ? '0' + h : h; + } + return s; + } + /* generate a new random private key */ coinjs.newPrivkey = function(){ var x = window.location; + x += coinjs.randomBytesSafe(32); x += (window.screen.height * window.screen.width * window.screen.colorDepth); x += coinjs.random(64); x += (window.screen.availHeight * window.screen.availWidth * window.screen.pixelDepth);