Window: crypto property
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thecrypto
read-only property of theWindow
interface returns theCrypto
object for this window's scope. This object gives web pages access to certain cryptographic related services.
Although the property itself is read-only, all of its methods (and the methods of itschild object,SubtleCrypto
) are not read-only, and therefore vulnerableto attack bypolyfill.
Althoughcrypto
is available on all windows, the returnedCrypto
object only has one usable feature in insecure contexts: thegetRandomValues()
method. In general, you should use this API only in secure contexts.
Value
An instance of theCrypto
interface, providing access to general-purpose cryptography and a strong random-number generator.
Examples
This example uses thecrypto
property to access thegetRandomValues()
method.
HTML
<p>The random numbers are:</p><button type="button">Generate 10 random numbers</button>
JavaScript
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);
Result
Specifications
Specification |
---|
Web Cryptography Level 2 # dom-windoworworkerglobalscope-crypto |
Browser compatibility
See also
- The
Crypto
interface WorkerGlobalScope.crypto