Window:crypto 属性
Window 接口的只读属性 crypto 返回此窗口范围的 Crypto 对象。此对象使网页能够访问某些与加密相关的服务。
尽管该属性本身是只读的,但其所有方法(以及其子对象 SubtleCrypto 的方法)都不是只读的,因此容易受到 polyfill 攻击。
尽管 crypto 在所有窗口上都可用,但在不安全的上下文中,返回的 Crypto 对象只有一个可用的功能:getRandomValues() 方法。一般来说,您应该只在安全上下文中使用此 API。
值
Crypto 接口的一个实例,提供对通用加密和强大的随机数生成器的访问。
示例
此示例使用 crypto 属性访问 getRandomValues() 方法。
HTML
html
<p id="myRandText">The random numbers are:</p>
<button type="button">Generate 10 random numbers</button>
JavaScript
js
function genRandomNumbers() {
const array = new Uint32Array(10);
globalThis.crypto.getRandomValues(array);
const randText = document.getElementById("myRandText");
randText.textContent = `The random numbers are: ${array.join(" ")}`;
}
document.querySelector("button").addEventListener("click", genRandomNumbers);
结果
规范
| 规范 |
|---|
| Web 加密级别 2 # dom-windoworworkerglobalscope-crypto |
浏览器兼容性
加载中…
另见
Crypto接口WorkerGlobalScope.crypto