gpt4 book ai didi

javascript - crypto.getRandomValues 会返回负值吗?

转载 作者:行者123 更新时间:2023-12-03 06:53:36 26 4
gpt4 key购买 nike

以下代码来自 https://www.google-analytics.com/analytics.js

var a = new Uint32Array(1);
window.crypto.getRandomValues(a);
return a[0] & 2147483647;

据我想象,正值 & 2147483647 永远是它本身。如果有必要,我可以推断 crypto.getRandomValues 将返回负值吗?我无法从 MDN 中找到该信息。

最佳答案

Web Cryptography API draft状态(强调我的):

  1. If array is not of an integer type (i.e., Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, or Uint32Array), throw a TypeMismatchError and terminate the algorithm.

  2. If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm.

  3. Overwrite all elements of array with cryptographically random values of the appropriate type.

换句话说:如果您传递 Int* 数组作为参数,则可能会出现负值。如果您不想要这些,请使用 Uint* (您已经在这样做,因此您可以预期 a[0] 为正)。

关于javascript - crypto.getRandomValues 会返回负值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37387935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com