Crypto.getRandomValues() method lets you get cryptographically strong random values. The array given as the parameter is filled with random numbers (random in its cryptographic meaning).

To guarantee enough performance, implementations are not using a truly random number generator, but they are using a pseudo-random number generator seeded with a value with enough entropy. The pseudo-random number generator algorithm (PRNG) may vary across user agents , but is suitable for cryptographic purposes. Implementations are required to use a seed with enough entropy, like a system-level entropy source.

getRandomValues() is the only member of the Crypto interface which can be used from an insecure context.

句法

typedArray = cryptoObj.getRandomValues(typedArray);
					

参数

typedArray
An integer-based TypedArray , that is an Int8Array Uint8Array Int16Array Uint16Array Int32Array ,或 Uint32Array . All elements in the array are overwritten with random numbers.

返回值

The same array passed as typedArray but with its contents replaced with the newly generated random numbers. Note that typedArray is modified in-place, and no copy is made.

异常

This method can throw an exception under error conditions.

DOMException (name: QuotaExceededError )

The requested length exceeds 65,536 bytes.

用法注意事项

不使用 getRandomValues() to generate encryption keys. Instead, use the generateKey() method. There are a few reasons for this; for example, getRandomValues() is not guaranteed to be running in a secure context.

There is no minimum degree of entropy mandated by the Web Cryptography specification. User agents are instead urged to provide the best entropy they can when generating random numbers, using a well-defined, efficient pseudorandom number generator built into the user agent itself, but seeded with values taken from an external source of pseudorandom numbers, such as a platform-specific random number function, the Unix /dev/urandom device, or other source of random or pseudorandom data.

范例

/* 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]);
}
					

规范

规范 状态 注释
Web Cryptography API 推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
getRandomValues() Chrome 11 Edge 12 Firefox 26 IE 11 Opera 15 Safari 6.1 WebView Android ≤37 Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS 6.1 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. Web 加密 API
  2. Crypto
  3. 特性
    1. subtle
  4. 方法
    1. getRandomValues()
  5. Related pages for Web Crypto API
    1. CryptoKey
    2. CryptoKeyPair
    3. RandomSource
    4. SubtleCrypto
    5. Window.crypto