crypto.generatePrime(size[, options[, callback]])
-
size
<number> 要生成的素数的大小(以位为单位)。¥
size
<number> The size (in bits) of the prime to generate. -
options
<Object>-
add
<ArrayBuffer> | <SharedArrayBuffer> | <TypedArray> | <Buffer> | <DataView> | <bigint> -
rem
<ArrayBuffer> | <SharedArrayBuffer> | <TypedArray> | <Buffer> | <DataView> | <bigint> -
safe
<boolean> 默认值:false
。¥
safe
<boolean> Default:false
. -
bigint
<boolean> 当true
时,生成的素数作为bigint
返回。¥
bigint
<boolean> Whentrue
, the generated prime is returned as abigint
.
-
-
callback
<Function>-
err
<Error> -
prime
<ArrayBuffer> | <bigint>
-
生成 size
位的伪随机素数。
¥Generates a pseudorandom prime of size
bits.
如果 options.safe
是 true
,素数将是一个安全素数 - 也就是说,(prime - 1) / 2
也将是素数。
¥If options.safe
is true
, the prime will be a safe prime -- that is,
(prime - 1) / 2
will also be a prime.
options.add
和 options.rem
参数可用于强制执行其他要求,例如,对于 Diffie-Hellman:
¥The options.add
and options.rem
parameters can be used to enforce additional
requirements, e.g., for Diffie-Hellman:
-
如果
options.add
和options.rem
都设置,素数将满足条件prime % add = rem
。¥If
options.add
andoptions.rem
are both set, the prime will satisfy the condition thatprime % add = rem
. -
如果只设置了
options.add
而options.safe
不是true
,素数将满足条件prime % add = 1
。¥If only
options.add
is set andoptions.safe
is nottrue
, the prime will satisfy the condition thatprime % add = 1
. -
如果只设置了
options.add
,而将options.safe
设置为true
,则素数将满足条件prime % add = 3
。这是必要的,因为options.add > 2
的prime % add = 1
会与options.safe
强制执行的条件相矛盾。¥If only
options.add
is set andoptions.safe
is set totrue
, the prime will instead satisfy the condition thatprime % add = 3
. This is necessary becauseprime % add = 1
foroptions.add > 2
would contradict the condition enforced byoptions.safe
. -
如果未给出
options.add
,则忽略options.rem
。¥
options.rem
is ignored ifoptions.add
is not given.
如果以 ArrayBuffer
、SharedArrayBuffer
、TypedArray
、Buffer
或 DataView
形式给出,则 options.add
和 options.rem
都必须编码为大端序列。
¥Both options.add
and options.rem
must be encoded as big-endian sequences
if given as an ArrayBuffer
, SharedArrayBuffer
, TypedArray
, Buffer
, or
DataView
.
默认情况下,素数被编码为 <ArrayBuffer> 中八位字节的大端序列。如果 bigint
选项为 true
,则提供 <bigint>。
¥By default, the prime is encoded as a big-endian sequence of octets
in an <ArrayBuffer>. If the bigint
option is true
, then a <bigint>
is provided.
素数的 size
将直接影响生成素数所需的时间。尺寸越大,所需时间越长。因为我们使用 OpenSSL 的 BN_generate_prime_ex
函数,它只提供对我们中断生成过程的能力的最小控制,所以不建议生成过大的素数,因为这样做可能会使进程无响应。
¥The size
of the prime will have a direct impact on how long it takes to
generate the prime. The larger the size, the longer it will take. Because
we use OpenSSL's BN_generate_prime_ex
function, which provides only
minimal control over our ability to interrupt the generation process,
it is not recommended to generate overly large primes, as doing so may make
the process unresponsive.