Skip to main content
generatePrime - crypto - Node documentation
function generatePrime

Usage in Deno

import { generatePrime } from "node:crypto";
generatePrime(
size: number,
callback: (
err: Error | null,
prime: ArrayBuffer,
) => void
,
): void
<div class="alert alert-warning"><div><svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M12 9v4" /> <path d="M10.363 3.591l-8.106 13.534a1.914 1.914 0 0 0 1.636 2.871h16.214a1.914 1.914 0 0 0 1.636 -2.87l-8.106 -13.536a1.914 1.914 0 0 0 -3.274 0z" /> <path d="M12 16h.01" /> </svg> Deno compatibility</div><div><p> The <code>safe</code>, <code>add</code> and <code>rem</code> option is not supported.</p> </div></div>

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.

Parameters

size: number

The size (in bits) of the prime to generate.

callback: (
err: Error | null,
prime: ArrayBuffer,
) => void

Return Type

void
generatePrime(
size: number,
callback: (
err: Error | null,
prime: bigint,
) => void
,
): void
<div class="alert alert-warning"><div><svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M12 9v4" /> <path d="M10.363 3.591l-8.106 13.534a1.914 1.914 0 0 0 1.636 2.871h16.214a1.914 1.914 0 0 0 1.636 -2.87l-8.106 -13.536a1.914 1.914 0 0 0 -3.274 0z" /> <path d="M12 16h.01" /> </svg> Deno compatibility</div><div><p> The <code>safe</code>, <code>add</code> and <code>rem</code> option is not supported.</p> </div></div>

Parameters

size: number
callback: (
err: Error | null,
prime: bigint,
) => void

Return Type

void
generatePrime(
size: number,
callback: (
err: Error | null,
prime: ArrayBuffer,
) => void
,
): void
<div class="alert alert-warning"><div><svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M12 9v4" /> <path d="M10.363 3.591l-8.106 13.534a1.914 1.914 0 0 0 1.636 2.871h16.214a1.914 1.914 0 0 0 1.636 -2.87l-8.106 -13.536a1.914 1.914 0 0 0 -3.274 0z" /> <path d="M12 16h.01" /> </svg> Deno compatibility</div><div><p> The <code>safe</code>, <code>add</code> and <code>rem</code> option is not supported.</p> </div></div>

Parameters

size: number
callback: (
err: Error | null,
prime: ArrayBuffer,
) => void

Return Type

void
generatePrime(
size: number,
callback: (
err: Error | null,
prime: ArrayBuffer | bigint,
) => void
,
): void
<div class="alert alert-warning"><div><svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M12 9v4" /> <path d="M10.363 3.591l-8.106 13.534a1.914 1.914 0 0 0 1.636 2.871h16.214a1.914 1.914 0 0 0 1.636 -2.87l-8.106 -13.536a1.914 1.914 0 0 0 -3.274 0z" /> <path d="M12 16h.01" /> </svg> Deno compatibility</div><div><p> The <code>safe</code>, <code>add</code> and <code>rem</code> option is not supported.</p> </div></div>

Parameters

size: number
callback: (
err: Error | null,
prime: ArrayBuffer | bigint,
) => void

Return Type

void