Skip to main content
generateKey - node__crypto.d.ts - Node documentation
function generateKey

Usage in Deno

```typescript import { generateKey } from "node:node__crypto.d.ts"; ```
generateKey(
type: "hmac" | "aes",
options: { length: number; },
callback: (
err: Error | null,
key: KeyObject,
) => void
,
): void
Asynchronously generates a new random secret key of the given `length`. The `type` will determine which validations will be performed on the `length`. ```js const { generateKey, } = await import('node:crypto'); generateKey('hmac', { length: 512 }, (err, key) => { if (err) throw err; console.log(key.export().toString('hex')); // 46e..........620 }); ``` The size of a generated HMAC key should not exceed the block size of the underlying hash function. See [createHmac](../.././node__crypto.d.ts/~/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; }
callback: (
err: Error | null,
key: KeyObject,
) => void

Return Type

void