MDN Web Docs

Value

An instance of the Crypto interface, providing access to general-purpose cryptography and a strong random-number generator.

Examples

This example uses the crypto property to access the getRandomValues() method.

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);

Result

Specifications

Specification
Web Cryptography Level 2
# dom-windoworworkerglobalscope-crypto

Browser compatibility

See also

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗