--- title: Window.crypto slug: Web/API/Window/crypto translation_of: Web/API/Window/crypto ---
{{APIRef}}

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

语法

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

范例

使用 {{domxref("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>

结果

{{ EmbedLiveSample('Example') }}

规范

规范 状态 批注
{{SpecName("Web Crypto API", "#dfn-GlobalCrypto", "Window.crypto")}} {{Spec2("Web Crypto API")}} Initial definition

浏览器支持

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support 44 {{CompatVersionUnknown}} {{CompatVersionUnknown}} 11 {{property_prefix("ms")}} 20 19 {{CompatVersionUnknown}}
Feature Chrome for Android Firefox Mobile Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

另见