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
  • 只有在设置了 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.addoptions.rem 是以 ArrayBufferSharedArrayBufferTypedArrayBufferDataView 提供的,则必须以大端序列进行编码。

🌐 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.