-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (45 loc) · 1.15 KB
/
index.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
import { NativeModules } from 'react-native'
const { Jose } = NativeModules
function pem2der(key) {
return key
.replace(/(?:\r\n|\r|\n)/g, '')
.replace('-----BEGIN RSA PRIVATE KEY-----', '')
.replace('-----END RSA PRIVATE KEY-----', '')
.replace('-----BEGIN RSA PUBLIC KEY-----', '')
.replace('-----END RSA PUBLIC KEY-----', '')
}
function keys(keys) {
return { jwk: keys.jwk, der: pem2der(keys.pem) }
}
export const sign = async (payload, privateKeys, header) => {
return Jose.sign(payload, keys(privateKeys), header)
}
export const verify = Jose.verify
export const decode = Jose.decode
export const addRecipient = async (
jwe,
ownerKeys,
recipientKeys,
alg = 'RSA-OAEP'
) => {
const { encrypted_key } = jwe.recipients.find(
r => r.header.kid === ownerKeys.jwk.kid
)
if (!encrypted_key) {
throw new Error('no matching recipient for owner key')
}
const newEncryptedKey = await Jose.reEncryptCek(
encrypted_key,
keys(ownerKeys),
keys(recipientKeys),
alg
)
jwe.recipients.push({
header: {
kid: recipientKeys.jwk.kid,
alg
},
encrypted_key: newEncryptedKey
})
return jwe
}