crypto.generatePrime(size[, options[, callback]])


生成 size 位的伪随机素数。

¥Generates a pseudorandom prime of size bits.

如果 options.safetrue,素数将是一个安全素数 - 也就是说,(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.addoptions.rem 参数可用于强制执行其他要求,例如,对于 Diffie-Hellman:

¥The options.add and options.rem parameters can be used to enforce additional requirements, e.g., for Diffie-Hellman:

  • 如果 options.addoptions.rem 都设置,素数将满足条件 prime % add = rem

    ¥If options.add and options.rem are both set, the prime will satisfy the condition that prime % add = rem.

  • 如果只设置了 options.addoptions.safe 不是 true,素数将满足条件 prime % add = 1

    ¥If only options.add is set and options.safe is not true, the prime will satisfy the condition that prime % add = 1.

  • 如果只设置了 options.add,而将 options.safe 设置为 true,则素数将满足条件 prime % add = 3。这是必要的,因为 options.add > 2prime % add = 1 会与 options.safe 强制执行的条件相矛盾。

    ¥If only options.add is set and options.safe is set to true, the prime will instead satisfy the condition that prime % add = 3. This is necessary because prime % add = 1 for options.add > 2 would contradict the condition enforced by options.safe.

  • 如果未给出 options.add,则忽略 options.rem

    ¥options.rem is ignored if options.add is not given.

如果以 ArrayBufferSharedArrayBufferTypedArrayBufferDataView 形式给出,则 options.addoptions.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.