-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
39 lines (37 loc) · 1.13 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
const openpgp = require('openpgp')
const toBinary = (data) => {
data = openpgp.util.encode_utf8(data)
data = openpgp.util.str_to_Uint8Array(data)
return data
}
module.exports.pgp = {
async sign ({ payload, secretKey }) {
// We need to use fromBinary because fromText screws with line endings
const data = toBinary(payload) //openpgp.util.encode_utf8(payload)
const privateKeys = (await openpgp.key.readArmored(secretKey)).keys
let { signature } = await openpgp.sign({
data,
privateKeys,
detached: true,
armor: true
})
return { signature }
},
async verify ({ payload, publicKey, signature }) {
const data = toBinary(payload)
const message = openpgp.message.fromBinary(data)
signature = await openpgp.signature.readArmored(signature)
const publicKeys = (await openpgp.key.readArmored(publicKey)).keys
let { signatures } = await openpgp.verify({
message,
signature,
publicKeys
})
let invalid = []
let valid = []
for (let sig of signatures) {
(sig.valid ? valid : invalid).push(sig.keyid.toHex())
}
return { valid, invalid }
}
}