-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
generate_key_pair.ts
77 lines (70 loc) · 2.08 KB
/
generate_key_pair.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { generateKeyPair as generate } from '../runtime/generate.ts'
import type { KeyLike } from '../types.d.ts'
export interface GenerateKeyPairResult {
/**
* The generated Private Key.
*/
privateKey: KeyLike
/**
* Public Key corresponding to the generated Private Key.
*/
publicKey: KeyLike
}
export interface GenerateKeyPairOptions {
/**
* The EC "crv" (Curve) or OKP "crv" (Subtype of Key Pair) value to generate.
* The curve must be both supported on the runtime as well as applicable for
* the given JWA algorithm identifier.
*/
crv?: string
/**
* A hint for RSA algorithms to generate an RSA key of a given `modulusLength`
* (Key size in bits). JOSE requires 2048 bits or larger. Default is 2048.
*/
modulusLength?: number
/**
* (Web Cryptography API specific) The value to use as
* [SubtleCrypto.generateKey()](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey)
* `extractable` argument. Default is false.
*/
extractable?: boolean
}
/**
* Generates a private and a public key for a given JWA algorithm identifier.
* This can only generate asymmetric key pairs. For symmetric secrets use the
* `generateSecret` function.
*
* Note: Under Web Cryptography API runtime the `privateKey` is generated with
* `extractable` set to `false` by default.
*
* @example Usage
* ```js
* const { publicKey, privateKey } = await generateKeyPair('PS256')
* console.log(publicKey)
* console.log(privateKey)
* ```
*
* @example ESM import
* ```js
* import { generateKeyPair } from 'jose'
* ```
*
* @example CJS import
* ```js
* const { generateKeyPair } = require('jose')
* ```
*
* @example Deno import
* ```js
* import { generateKeyPair } from 'https://deno.land/x/jose@VERSION/index.ts'
* ```
*
* @param alg JWA Algorithm Identifier to be used with the generated key pair.
* @param options Additional options passed down to the key pair generation.
*/
export async function generateKeyPair(
alg: string,
options?: GenerateKeyPairOptions,
): Promise<GenerateKeyPairResult> {
return generate(alg, options)
}