window.crypto.getRandomValues

  • Revision slug: Web/API/window.crypto.getRandomValues
  • Revision title: window.crypto.getRandomValues
  • Revision id: 440707
  • Created:
  • Creator: SaschaNaz
  • Is current revision? No
  • Comment

Revision Content

{{ DOMRef() }}

{{ SeeCompatTable() }}

This methods lets you get cryptographically random values.

Syntax

window.crypto.getRandomValues(typedArray);

Parameters

Parameter Description
typedArray Integer-based TypedArray. All elements in the array are going to be overridden with random numbers.

Description

If you provide an integer-based TypedArray (i.e. Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, or Uint32Array), the function is going fill the array with cryptographically random numbers. The browser is supposed to be using a strong (pseudo) random number generator. Because the browser probably only has a limited amount of entropy, the method is free to throw the QuotaExceededError, if too much entropy is drained.

Example

/* assuming that window.crypto.getRandomValues is available */

var array = new Uint32Array(10);
window.crypto.getRandomValues(array);

console.log("Your lucky numbers:");
for (var i = 0; i < array.length; i++) {
    console.log(array[i]);
}

Browser Compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 11.0 {{ webkitbug("22049") }} 21.0 11.0 {{ property_prefix("ms") }} 15.0 3.1
Feature Android Browser Chrome (as App) Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari
Basic support {{ CompatNo() }} 23 21.0 {{ CompatNo() }} {{ CompatNo() }} iOS 6

Specification

See also

Revision Source

<p>{{ DOMRef() }}</p>
<p>{{ SeeCompatTable() }}</p>
<p>This methods lets you get cryptographically random values.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
window.crypto.getRandomValues(typedArray);</pre>
<h2 id="Parameters">Parameters</h2>
<table class="standard-table" style="">
  <thead>
    <tr>
      <th scope="col">Parameter</th>
      <th scope="col">Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>typedArray</code></td>
      <td>Integer-based <a href="/en/JavaScript_typed_arrays" title="JavaScript typed arrays">TypedArray</a>. All elements in the array are going to be overridden with random numbers.</td>
    </tr>
  </tbody>
</table>
<h2 id="Description">Description</h2>
<p>If you provide an integer-based <a href="/en/JavaScript_typed_arrays" title="JavaScript typed arrays">TypedArray</a> (i.e. <a href="/en/JavaScript_typed_arrays/Int8Array" title="Int8Array"><code>Int8Array</code></a>, <a href="/en/JavaScript_typed_arrays/Uint8Array" title="Uint8Array"><code>Uint8Array</code></a>, <a href="/en/JavaScript_typed_arrays/Int16Array" title="Int16Array"><code>Int16Array</code></a>, <a href="/en/JavaScript_typed_arrays/Uint16Array" title="Uint16Array"><code>Uint16Array</code></a>, <a href="/en/JavaScript_typed_arrays/Int32Array" title="Int32Array"><code>Int32Array</code></a>, or <a href="/en/JavaScript_typed_arrays/Uint32Array" title="Uint32Array"><code>Uint32Array</code></a>), the function is going fill the array with cryptographically random numbers. The browser is supposed to be using a strong (pseudo) random number generator. Because the browser probably only has a limited amount of entropy, the method is free to throw the <code>QuotaExceededError</code>, if too much entropy is drained.</p>
<h2 id="Example">Example</h2>
<pre class="brush: js">
/* assuming that window.crypto.getRandomValues is available */

var array = new Uint32Array(10);
window.crypto.getRandomValues(array);

console.log("Your lucky numbers:");
for (var i = 0; i &lt; array.length; i++) {
&nbsp;&nbsp;&nbsp; console.log(array[i]);
}
</pre>
<h2 id="Browser_Compatibility">Browser Compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>11.0 {{ webkitbug("22049") }}</td>
        <td>21.0</td>
        <td>11.0 {{ property_prefix("ms") }}</td>
        <td>15.0</td>
        <td>3.1</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android Browser</th>
        <th>Chrome (as App)</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatNo() }}</td>
        <td>23</td>
        <td>21.0</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>iOS 6</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a class="external" href="http://wiki.whatwg.org/wiki/Crypto" title="http://wiki.whatwg.org/wiki/Crypto">WHATWG window.crypto proposal</a></li>
  <li><a href="http://www.w3.org/TR/WebCryptoAPI/" title="http://www.w3.org/TR/WebCryptoAPI/">Web Cryptography API</a></li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
  <li>{{ domxref("Window.crypto") }}</li>
  <li><a href="/en/JavaScript_crypto" title="JavaScript crypto">JavaScript crypto</a></li>
  <li><a href="/en/JavaScript/Reference/Global_Objects/Math/random" title="random">Math.random</a></li>
</ul>
Revert to this revision