-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwk.js
52 lines (47 loc) · 1.26 KB
/
jwk.js
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
import { encodeBase64UrlAsString } from '../utils/base64.js';
import { digest } from '../utils/crypto.js';
/**
* @param {Object} object
* @return {Promise<BASE64URL<any>>}
*/
export async function thumbprintObject(object) {
const signingInput = JSON.stringify(object);
const result = await digest('SHA-256', signingInput);
return encodeBase64UrlAsString(result);
}
/**
* @param {JWK} jwk
* @return {Promise<BASE64URL<any>>}
*/
export async function thumbprintEC(jwk) {
const { crv, kty, x, y } = jwk;
return await thumbprintObject({ crv, kty, x, y });
}
/**
* @param {JWK} jwk
* @return {Promise<BASE64URL<any>>}
*/
export async function thumbprintRSA(jwk) {
const { e, kty, n } = jwk;
return await thumbprintObject({ e, kty, n });
}
/**
* @param {JWK} jwk
* @return {Promise<BASE64URL<any>>}
*/
export async function thumbprintSymmetrical(jwk) {
const { k, kty } = jwk;
return await thumbprintObject({ k, kty });
}
/**
* @param {JWK} jwk
* @return {Promise<BASE64URL<any>>}
*/
export async function thumbprintJWK(jwk) {
switch (jwk.kty) {
case 'RSA': return await thumbprintRSA(jwk);
case 'EC': return await thumbprintEC(jwk);
case 'oct': return await thumbprintSymmetrical(jwk);
default: throw new Error('Unknown key type');
}
}