Created
August 26, 2018 23:07
-
-
Save akirattii/da1ad567e9ffaf28a5f4e9ee0139f112 to your computer and use it in GitHub Desktop.
JS: How to generate a random entropy by mouse-moving on browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// const entropy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] | |
const entropy = []; | |
let captureStart = false; | |
/** | |
* Mouse Moving Entropy Generator on browser. | |
* Returns an entropy which is 16 bytes long array of unsigned char integer (0-255). | |
*/ | |
$(document).on("mousemove", "html", function(e) { | |
const MAX_LEN = 16; // size of entropy's array | |
if (entropy.length >= MAX_LEN) return; | |
const now = Date.now(); | |
if (now >= 1 && (now % 10) !== 0) return; | |
if (!captureStart) { | |
return setTimeout(() => { | |
captureStart = true; | |
}, 3000); // capturing starts in 3 seconds to set the mouse cursor at random position... | |
} | |
const iw = window.innerWidth; | |
const ih = window.innerHeight; | |
const iwPlusIh = iw + ih; | |
const px = e.pageX; | |
const py = e.pageY; | |
const pxPlusPy = px + py; | |
const ret = Math.round((pxPlusPy / iwPlusIh) * 255); | |
entropy.push(ret); | |
console.log("0-255:", ret); | |
if (entropy.length >= MAX_LEN) { | |
console.log("entropy:", entropy); | |
shuffle(entropy); | |
console.log("suffledEntropy:", entropy); | |
// const account = WalletUtil.generateAccount({ entropy }); | |
// console.log("account:", JSON.stringify(account, null, 2)); | |
} | |
function shuffle(array) { | |
let currentIndex = array.length, | |
temporaryValue, randomIndex; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} | |
}); |
Just copy and pasted this code in console, but this not working.
$(document).on("mousemove", "html",
this is JQuery-function, and need include JQuery, at first.
So, after replace it to JavaScript-version:
document.addEventListener('mousemove',
this code working in console of browser, too.
After copy-and-paste this code in console, just move cursor on the page, and see array, in console-log.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very good