5
Hello, I’m new to javascript and am using the following function to shuffle characters from a string in a password generator that I’m trying to develop:
function shuffle(string) {
"use strict";
var parts = string.split('');
for (var i = parts.length; i > 0;) {
var random = parseInt(Math.random() * i);
var temp = parts[--i];
parts[i] = parts[random];
parts[random] = temp;
}
return parts.join('');
}
But as I was told, use Math.random()
is not very safe for that purpose. I would like to know how I can adapt my function then using window.crypto.getRandomValues()
who informed me to be safer.
Grateful for any help!