1 |
|
2 |
|
3 |
|
4 |
|
5 | import * as webcrypto from 'lib0/webcrypto'
|
6 | export { exportKeyJwk } from './common.js'
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const defaultUsages = ['encrypt', 'decrypt']
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export const encrypt = (key, data) =>
|
25 | webcrypto.subtle.encrypt(
|
26 | {
|
27 | name: 'RSA-OAEP'
|
28 | },
|
29 | key,
|
30 | data
|
31 | ).then(buf => new Uint8Array(buf))
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | export const decrypt = (key, data) =>
|
43 | webcrypto.subtle.decrypt(
|
44 | {
|
45 | name: 'RSA-OAEP'
|
46 | },
|
47 | key,
|
48 | data
|
49 | ).then(data => new Uint8Array(data))
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | export const generateKeyPair = ({ extractable = false, usages = defaultUsages } = {}) =>
|
58 | webcrypto.subtle.generateKey(
|
59 | {
|
60 | name: 'RSA-OAEP',
|
61 | modulusLength: 4096,
|
62 | publicExponent: new Uint8Array([1, 0, 1]),
|
63 | hash: 'SHA-256'
|
64 | },
|
65 | extractable,
|
66 | usages
|
67 | )
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | export const importKeyJwk = (jwk, { extractable = false, usages } = {}) => {
|
76 | if (usages == null) {
|
77 |
|
78 | usages = jwk.key_ops || defaultUsages
|
79 | }
|
80 | return webcrypto.subtle.importKey('jwk', jwk, { name: 'RSA-OAEP', hash: 'SHA-256' }, extractable, (usages))
|
81 | }
|