Mar*_*n19 5 javascript random prng sjcl
我需要在用户浏览器中生成一个安全的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));
}
});
});
Run Code Online (Sandbox Code Playgroud)
将鼠标移动一段时间后,我得到一个像这样的字节数组:[-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.substr(0, length);
callback(randomBase64String);
clearInterval(checkReadyness);
}
}, 1);
}
Run Code Online (Sandbox Code Playgroud)
但这在旧版浏览器中不起作用.因为我使用了window.btoa().