This random generator uses cryptographically secure numbers/chars instead of Math.random()
. The Javascript code with jQuery works well but I affect clean code ;) It would be great if you could help me to optimize the code (e.g. in speed).
(function () {
var $length, $result, $new, $chars;
$(document).ready(function () {
$length = $('#pw-length');
$result = $('#pw-result');
$new = $('#get-new-pw');
$chars = $('#pw-chars');
// display the result on page load
getRandomString();
// generate new password
$new.click(function () {
getRandomString();
return false;
});
// allow only numbers in the length-input-field
$length.on('keyup focusout', function () {
$length.val($length.val().replace(/[^0-9]/g, ''));
});
});
function randomString(length) {
var charset = $chars.val(),
length = parseInt($length.val()),
i,
result = "";
// First we're going to try to use a built-in CSPRNG
if (window.crypto && window.crypto.getRandomValues) {
values = new Uint32Array(length);
window.crypto.getRandomValues(values);
for (i = 0; i < length; i++) {
result += charset[values[i] % charset.length];
}
}
// IE calls it msCrypto
else if (window.msCrypto && window.msCrypto.getRandomValues) {
values = new Uint32Array(length);
window.msCrypto.getRandomValues(values);
for (i = 0; i < length; i++) {
result += charset[values[i] % charset.length];
}
}
// -> No built-in functionality -> use the function Math.random()
else {
for (i = 0; i < length; i++) {
result += charset[Math.floor(Math.random() * charset.length)];
}
}
return result;
}
// Display the result
function getRandomString() {
$result.html(randomString(10));
}
}());
I also uploaded it here for testing. And of course the corresponding HTML:
<input type="text" id="pw-chars" value="AaBbCcDdEeFfGgHhiJjKkLMmNnoPpQqRrSsTtUuVvWwXxYyZz23456789!?$%#&@^+-~*=_.,:;()" />
<input type="text" id="pw-length" value="10" />
<button id="get-new-pw">New</button>
<div id="pw-result"></div>