Window.crypto

Window.crypto只读属性返回与全局对象关联的 Crypto对象。 此对象允许网页访问某些加密相关服务。

语法

var cryptoObj = window.crypto || window.msCrypto; // for IE 11

范例

使用 Window.crypto 来访问getRandomValues() 方法.

JavaScript

genRandomNumbers = function getRandomNumbers() {
  var array = new Uint32Array(10);
  window.crypto.getRandomValues(array);

  var randText = document.getElementById("myRandText");
  randText.innerHTML = "The random numbers are: "
  for (var i = 0; i < array.length; i++) {
    randText.innerHTML += array[i] + " ";
  }
}

HTML

<p id="myRandText">随机数字: </p>
<button type="button" onClick='genRandomNumbers()'>生成10个随机数字</button>

结果

规范

规范 状态 批注
Web Cryptography API
Window.crypto
Recommendation Initial definition

浏览器支持

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help! (en-US)

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support 44 (Yes) (Yes) 11 ms (en-US) 20 19 (Yes)
Feature Chrome for Android Firefox Mobile Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

另见