javascript window.crypto.getRandomValues() 的兼容性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16084766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compatibility of window.crypto.getRandomValues()
提问by D.W.
I need to generate cryptographically secure pseudorandom numbers, in Javascript. I know about the window.crypto.getRandomValues
API, which does exactly what I want. However, I also know it was introduced relatively recently (in 2011 or so).
我需要在 Javascript 中生成加密安全的伪随机数。我了解window.crypto.getRandomValues
API,它完全符合我的要求。但是,我也知道它是最近才推出的(在 2011 年左右)。
Can I safely assume window.crypto.getRandomValues
is present, or will using it introduce compatibility problems on some browsers? 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? 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.
我可以安全地假设window.crypto.getRandomValues
存在,还是会在某些浏览器上使用它引入兼容性问题?是否有不支持的主要(广泛使用的)浏览器window.crypto.getRandomValues
(包括移动或桌面浏览器),如果是,我需要担心哪些?如果确实如此,我会很高兴得知有足够的支持,我不再需要担心回退方法。
回答by bobince
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.comlists which browsers support it and calculates the browser marketshare.
与往常一样,这取决于您的目标市场,并且会随着时间而改变。 caniuse.com列出了哪些浏览器支持它并计算浏览器市场份额。
Here is a summary:
这是一个总结:
- IE 11: w/ prefix
IE Mobile 11: w/ prefix
Firefox: 21+
Firefox Mobile: 21+
Safari: from 6.1
Safari Mobile: 7.1
Chrome: 11+
- Chrome for Mobile: 23+
Android browser: 4.4
Opera: 15+
- Opera Mobile: 36+
- Opera Mini: no
- IE 11:带前缀
IE Mobile 11:带前缀
火狐:21+
火狐手机:21+
Safari:从 6.1
Safari 移动版:7.1
铬:11+
- 移动版 Chrome:23+
安卓浏览器:4.4
歌剧:15+
- Opera 手机:36+
- 歌剧迷你:没有
回答by laurent
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 外的所有现代浏览器都支持它:
回答by Richard Merchant
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");
}
回答by OmarAguinaga
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.");