crypto.generatePrimeSync(size[, options])
size
<number> 要生成的素数的大小(以位为单位)。options
<Object>add
<ArrayBuffer> | <SharedArrayBuffer> | <TypedArray> | <Buffer> | <DataView> | <bigint>rem
<ArrayBuffer> | <SharedArrayBuffer> | <TypedArray> | <Buffer> | <DataView> | <bigint>safe
<boolean> 默认值:false
。bigint
<boolean> 当true
时,生成的素数作为bigint
返回。
- 返回: <ArrayBuffer> | <bigint>
生成 size
位的伪随机素数。
如果 options.safe
是 true
,素数将是一个安全素数——也就是说,(prime - 1) / 2
也将是素数。
options.add
和 options.rem
参数可用于强制执行其他要求,例如,对于 Diffie-Hellman:
- 如果
options.add
和options.rem
都设置,素数将满足条件prime % add = rem
。 - 如果只设置了
options.add
而options.safe
不是true
,素数将满足条件prime % add = 1
。 - 如果只设置了
options.add
,而将options.safe
设置为true
,则素数将满足条件prime % add = 3
。 这是必要的,因为options.add > 2
的prime % add = 1
会与options.safe
强制执行的条件相矛盾。 - 如果未给出
options.add
,则忽略options.rem
。
如果以 ArrayBuffer
、SharedArrayBuffer
、TypedArray
、Buffer
或 DataView
形式给出,则 options.add
和 options.rem
都必须编码为大端序列。
默认情况下,素数被编码为 <ArrayBuffer> 中八位字节的大端序列。
如果 bigint
选项为 true
,则提供 <bigint>。
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> Default:false
.bigint
<boolean> Whentrue
, the generated prime is returned as abigint
.
- Returns: <ArrayBuffer> | <bigint>
Generates a pseudorandom prime of size
bits.
If options.safe
is true
, the prime will be a safe prime -- that is,
(prime - 1) / 2
will also be a prime.
The options.add
and options.rem
parameters can be used to enforce additional
requirements, e.g., for Diffie-Hellman:
- If
options.add
andoptions.rem
are both set, the prime will satisfy the condition thatprime % add = rem
. - If only
options.add
is set andoptions.safe
is nottrue
, the prime will satisfy the condition thatprime % add = 1
. - 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.rem
is ignored ifoptions.add
is not given.
Both options.add
and options.rem
must be encoded as big-endian sequences
if given as an ArrayBuffer
, SharedArrayBuffer
, TypedArray
, Buffer
, or
DataView
.
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.