简体   繁体   English

window.crypto.getRandomValues() 的兼容性

[英]Compatibility of window.crypto.getRandomValues()

I need to generate cryptographically secure pseudorandom numbers, in Javascript.我需要在 Javascript 中生成加密安全的伪随机数。 I know about the window.crypto.getRandomValues API, which does exactly what I want.我知道window.crypto.getRandomValues API,它完全符合我的要求。 However, I also know it was introduced relatively recently (in 2011 or so).但是,我也知道它是最近才推出的(在 2011 年左右)。

Can I safely assume window.crypto.getRandomValues is present, or will using it introduce compatibility problems on some browsers?我可以安全地假设window.crypto.getRandomValues存在,还是会在某些浏览器上使用它引入兼容性问题? Are there any major (widely used) browsers which do not support window.crypto.getRandomValues (including mobile or desktop browsers), and if so, which ones do I need to worry about?是否有任何主要(广泛使用)浏览器不支持window.crypto.getRandomValues (包括移动或桌面浏览器),如果是,我需要担心哪些? I would be delighted to learn that there is enough support that I no longer need to worry about fallback methods , if that is indeed the case.如果确实如此,我会很高兴得知有足够的支持,我不再需要担心回退方法

Can I safely assume window.crypto.getRandomValues is present我可以安全地假设 window.crypto.getRandomValues 存在吗

As always it depends on your target market and will change over time.与往常一样,这取决于您的目标市场,并且会随着时间而改变。 caniuse.com lists which browsers support it and calculates the browser marketshare. caniuse.com列出了哪些浏览器支持它并计算浏览器市场份额。

Here is a summary:这是一个总结:

  • IE 11: w/ prefix IE 11:带前缀
  • IE Mobile 11: w/ prefix IE Mobile 11:带前缀

  • Firefox: 21+火狐:21+

  • Firefox Mobile: 21+火狐手机:21+

  • Safari: from 6.1 Safari:从 6.1

  • Safari Mobile: 7.1 Safari 移动版:7.1

  • Chrome: 11+铬:11+

  • Chrome for Mobile: 23+移动版 Chrome:23+
  • Android browser: 4.4安卓浏览器:4.4

  • Opera: 15+歌剧:15+

  • Opera Mobile: 36+ Opera 手机:36+
  • Opera Mini: no歌剧迷你:没有

For a more complete, up-to-date view, it's probably better to just check caniuse.com :要获得更完整、最新的视图,最好查看caniuse.com

http://caniuse.com/#feat=getrandomvalues http://caniuse.com/#feat=getrandomvalues

As of December 2015, all modern browsers, except for Opera Mini, support it:截至 2015 年 12 月,除 Opera Mini 之外的所有现代浏览器都支持它:

在此处输入图片说明

const crypto = window.crypto ||
  window.msCrypto || {
    getRandomValues: array => {
      for (let i = 0, l = array.length; i < l; i++) {
        array[i] = Math.floor(Math.random() * 256);
      }
      return array;
    }
  };

  if (crypto.getRandomValues === undefined) {
    throw new Error("crypto is not supported on this browser");
  }

Opera is the only one that does not support window.crypto.getRandomValues, but it's math.random() is pretty secure. Opera 是唯一不支持 window.crypto.getRandomValues 的,但它的math.random()非常安全。

What I did to solve this was just to check if the window.crypto is available, if not check is it an opera browser and if not just throw an error saying that the browser can't generate a secure password.我为解决这个问题所做的只是检查 window.crypto 是否可用,如果不可用,则检查它是否是 Opera 浏览器,如果不是,则只是抛出一个错误,指出浏览器无法生成安全密码。

if(window.crypto && window.crypto.getRandomValues)
{
    (use window.crypto.getRandomValues)
}
else if(isOpera)
{
    (use Math.random)
}
else throw new Error("Your browser can not generate a secure Password, please change it to one that can (Google Chrome, IE, Firefox, Safari, Opera) or get the newest update.");

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

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