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 位的伪随机质数。
【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。 - 只有在设置了
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。
如果 options.add 和 options.rem 是以 ArrayBuffer、SharedArrayBuffer、TypedArray、Buffer 或 DataView 提供的,则必须以大端序列进行编码。
【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.】