Skip to content

Instantly share code, notes, and snippets.

@akirattii
Created August 26, 2018 23:07
Show Gist options
  • Save akirattii/da1ad567e9ffaf28a5f4e9ee0139f112 to your computer and use it in GitHub Desktop.
Save akirattii/da1ad567e9ffaf28a5f4e9ee0139f112 to your computer and use it in GitHub Desktop.
JS: How to generate a random entropy by mouse-moving on browser
// 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;
}
});
@salif
Copy link

salif commented Nov 25, 2019

very good

@username1565
Copy link

username1565 commented Sep 22, 2020

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