From 17b91b9368d682b8650a5c53ddfb8bc54fb1f71e Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 12 Dec 2019 01:01:12 +0100 Subject: [PATCH] :tada: Add fill method to rng-impl ns. --- frontend/src/uxbox/util/rng_impl.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/frontend/src/uxbox/util/rng_impl.js b/frontend/src/uxbox/util/rng_impl.js index d124b728d..371cc21eb 100644 --- a/frontend/src/uxbox/util/rng_impl.js +++ b/frontend/src/uxbox/util/rng_impl.js @@ -19,6 +19,11 @@ goog.scope(function() { if (cljs.core._STAR_target_STAR_ === "nodejs") { const crypto = require("crypto"); + self.fill = function(buf) { + crypto.randomFillSync(buf); + return buf; + }; + self.getBytes = function(n) { return crypto.randomBytes(n); }; @@ -27,6 +32,12 @@ goog.scope(function() { // Check if whatwg rng is available (high quality); else if (global.crypto !== undefined && global.crypto.getRandomValues !== undefined) { + + self.fill = function(buf) { + global.crypto.getRandomValues(buf); + return buf; + }; + self.getBytes = function(n) { const buf = new Uint8Array(16); global.crypto.getRandomValues(buf); @@ -37,6 +48,16 @@ goog.scope(function() { // Switch Back to the Math.random (low quality); else { console.warn("No high quality RNG available, switching back to Math.random."); + + self.fill = function(buf) { + for (let i = 0, r; i < buf.length; i++) { + if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; } + buf[i] = r >>> ((i & 0x03) << 3) & 0xff; + } + + return buf; + }; + self.getBytes = function(n) { const buf = new Array(n); for (let i = 0, r; i < n; i++) {