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