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

Usage in Deno

import { generateKeySync } from "node:crypto";
generateKeySync(
type: "hmac" | "aes",
options: { length: number; },
): KeyObject

Synchronously generates a new random secret key of the given length. The type will determine which validations will be performed on the length.

const {
  generateKeySync,
} = await import('node:crypto');

const key = generateKeySync('hmac', { length: 512 });
console.log(key.export().toString('hex'));  // e89..........41e

The size of a generated HMAC key should not exceed the block size of the underlying hash function. See createHmac for more information.

Parameters

type: "hmac" | "aes"

The intended use of the generated secret key. Currently accepted values are 'hmac' and 'aes'.

options: { length: number; }

Return Type