[英]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草案状态(重点为我):
If array is not of an integer type (ie,
Int8Array
,Uint8Array
,Int16Array
,Uint16Array
,Int32Array
, orUint32Array
), throw a TypeMismatchError and terminate the algorithm. 如果array不是整数类型(即Int8Array
,Uint8Array
,Int16Array
,Uint16Array
,Int32Array
或Uint32Array
),则抛出TypeMismatchError并终止算法。If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm. 如果array的byteLength大于65536,则抛出QuotaExceededError并终止算法。
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.