简体   繁体   English

从数学随机更改为 window.crypto

[英]Change from math random to window.crypto

I use a random word generator, which I wanted to change from math.random to the more secure window.crypto .我使用了一个随机单词生成器,我想将其从math.random更改为更安全的window.crypto

I tried for hours to get this working and I'm sure there is a error in the code.我尝试了几个小时才能使其正常工作,但我确定代码中存在错误。 How must I change my code to get this code to using the window.crypto method?我必须如何更改我的代码才能使此代码使用window.crypto方法?

var wordings = ['X',
  'I',
  'II'
];

function getRandom(randArray) {
  return Math.floor(Math.random() * randArray.length);
}

function showrandom() {
  document.getElementById('random').innerHTML = wordings[getRandom(wordings)] + ' ' + wordings[getRandom(wordings)];
}

showrandom();

What I tried so far:到目前为止我尝试过的:

var wordings = ['X',
  'I',
  'II'
];

function getRandom(randArray) {
  var array = new Uint32Array(10);
  window.crypto.getRandomValues(array);
}


function showrandom() {
  document.getElementById('random').innerHTML = wordings[getRandom(wordings)] + ' ' + wordings[getRandom(wordings)];
}

The basic problem is that the Math.random returns values from 0 ( inclusive ) to 1 ( exclusive ), while the window.crypto.getRandomValues returns integers from 0 to the max 32 bit integer ( or whatever the max of the type of the array you pass in ).基本问题是Math.random返回从 0(包含)到 1(包含)的值,而window.crypto.getRandomValues返回从 0 到最大 32 位整数(或数组类型的最大值)的整数你通过)。

So you need to to scale down the range of the window.crypto to the one in the Math.random因此,您需要将window.crypto的范围缩小到Math.random

Something like就像是

function cryptoRandom(){
  // return a crypto generated number
  // between 0 and 1 (0 inclusive, 1 exclusive);
  // Mimics the Math.random function in range of results
  var array = new Uint32Array(1),
    max = Math.pow(2, 32), // normally the max is 2^32 -1 but we remove the -1
                           //  so that the max is exclusive
    randomValue = window.crypto.getRandomValues(array)[0] / max;

    return randomValue;
}

function getRandom(randArray) {
    return Math.floor(cryptoRandom() * randArray.length);
}

See https://news.ycombinator.com/item?id=9976493 for why using modulo % decreases the entropy of the random number请参阅https://news.ycombinator.com/item?id=9976493了解为什么使用 modulo %降低随机数的熵

You are missing ) It should be document.getElementById('random').innerHTML = wordings[getRandom(wordings)] + ' ' + wordings[getRandom(wordings)];你错过了)它应该是document.getElementById('random').innerHTML = wordings[getRandom(wordings)] + ' ' + wordings[getRandom(wordings)]; Does this solve your problem?这能解决您的问题吗?

Also as documentation shows crypto this method generates random integer values.同样作为文档显示 加密,此方法生成随机整数值。 As i understand from your code you are trying to derive index of the words inside wordings array from random number.正如我从您的代码中了解到的,您正试图从随机数中导出wordings数组中单词的索引。 To get indexes from random integers you could use modulo operation by wordings.length for each random number, but this approach needs to be investigated in terms of security issues.要从随机整数中获取索引,您可以通过wordings.length对每个随机数使用模运算,但需要根据安全问题对这种方法进行调查。

the code would look like this:代码如下所示:

var wordings = ['X',
    'I',
    'II'
];

function getRandom(randArray){
    var cryptoObj = window.crypto || window.msCrypto; // for IE 11
    var array = new Uint32Array(1);
    cryptoObj.getRandomValues(array);

    return array[0]%randArray.length;
}

function showrandom() {
    document.getElementById('random').innerHTML = wordings[getRandom(wordings)] + ' ' + wordings[getRandom(wordings)];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM