简体   繁体   English

crypto.getRandomValues是否会返回负值?

[英]Will crypto.getRandomValues return negative values?

Below is code from https://www.google-analytics.com/analytics.js 以下是来自https://www.google-analytics.com/analytics.js的代码

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

As far as I can imagine, a positive value & 2147483647 will always be itself. 据我所能想象, a positive value & 2147483647永远都是其本身。 If it's necessary, can I deduce that the crypto.getRandomValues will return negative values? 如果有必要,我是否可以推断crypto.getRandomValues将返回负值? I cannot find that from MDN. 我在MDN中找不到。

The Web Cryptography API draft states (emphasis mine): Web密码学API草案状态(重点为我):

  1. If array is not of an integer type (ie, Int8Array , Uint8Array , Int16Array , Uint16Array , Int32Array , or Uint32Array ), throw a TypeMismatchError and terminate the algorithm. 如果array不是整数类型(即Int8ArrayUint8ArrayInt16ArrayUint16ArrayInt32ArrayUint32Array ),则抛出TypeMismatchError并终止算法。

  2. If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm. 如果array的byteLength大于65536,则抛出QuotaExceededError并终止算法。

  3. Overwrite all elements of array with cryptographically random values of the appropriate type . 适当类型的加密随机值覆盖数组的所有元素。

In other words: if you pass an Int* array as argument, you can expect negative values. 换句话说:如果将Int*数组作为参数传递,则可以期望为负值。 If you don't want those, use Uint* (which you're already doing, so you can expect a[0] to be positive). 如果您不想要这些,请使用Uint* (您已经在做,因此可以期望a[0]为正)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 crypto.getRandomValues(new Uint32Array(1))[0] / lim 可以为负数吗? - Can crypto.getRandomValues(new Uint32Array(1))[0] / lim ever be negative? IE9和Firefox的Crypto.getRandomValues实现 - Crypto.getRandomValues Implementation for IE9 and Firefox Crypto.getRandomValues的熵源是什么? - What is the source of entropy for Crypto.getRandomValues? Javascript:使用 crypto.getRandomValues 生成一个范围内的随机数 - Javascript: Generate a random number within a range using crypto.getRandomValues 如何使用crypto.getRandomValues中的随机数对数组进行随机化? - How to randomize an array with random numbers from crypto.getRandomValues? Inte.net Explorer 11 中的 crypto.getRandomValues 有什么问题? - What is wrong with crypto.getRandomValues in Internet Explorer 11? 如何用 crypto.getRandomValues() 替换 Math.random() 并保持相同的结果? - How to replace Math.random() with crypto.getRandomValues() and keep same result? window.crypto.getRandomValues() 的兼容性 - Compatibility of window.crypto.getRandomValues() 如何使用window.crypto.getRandomValues获取特定范围内的随机值 - How to use window.crypto.getRandomValues to get random values in a specific range 开玩笑地模拟 window.crypto.getRandomValues - Mock window.crypto.getRandomValues in jest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM