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


生成 size 位的伪随机素数。

如果 options.safetrue,素数将是一个安全素数——也就是说,(prime - 1) / 2 也将是素数。

options.addoptions.rem 参数可用于强制执行其他要求,例如,对于 Diffie-Hellman:

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

如果以 ArrayBufferSharedArrayBufferTypedArrayBufferDataView 形式给出,则 options.addoptions.rem 都必须编码为大端序列。

默认情况下,素数被编码为 <ArrayBuffer> 中八位字节的大端序列。 如果 bigint 选项为 true,则提供 <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 and options.rem are both set, the prime will satisfy the condition that prime % add = rem.
  • If only options.add is set and options.safe is not true, the prime will satisfy the condition that prime % add = 1.
  • 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.rem is ignored if options.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.