我需要在用户浏览器中生成一个安全的 50 个字符的随机字符串。
查看sjcl.prng到目前为止我已经知道了:
$(document).ready(function () {
sjcl.random = new sjcl.prng(8);
sjcl.random.startCollectors();
$("body").on('mousemove', function() {
console.log(sjcl.random.getProgress(8));
if(sjcl.random.isReady(8) === 2) {
sjcl.random.stopCollectors();
console.log(sjcl.random.randomWords(5,8));
}
});
});
移动鼠标一段时间后,我得到一个字节数组,如下所示:[-579285364, 1099191484, 94979086, -1572161987, -570940948]
。
但我要查找的是 50 个字符的字母数字字符串。我对这个主题的了解有限,我在这里寻求一些帮助。
最佳答案
我是这样解决的:
function createRandomString (callback, length) {
var randomBase64String = '',
checkReadyness;
checkReadyness = setInterval(function () {
console.log(length);
if(sjcl.random.isReady(10)) {
while(randomBase64String.length < length) {
randomInt = sjcl.random.randomWords(1, 10)[0];
randomBase64String += btoa(randomInt);
}
randomBase64String = randomBase64String.substring(0, length);
callback(randomBase64String);
clearInterval(checkReadyness);
}
}, 1);
}
This doesn't work in a few older browsers though. Because I used window.btoa().
关于javascript - 如何使用 Stanford PRNG 生成随机字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24211586/