-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPgpKeyPair2021.ts
132 lines (120 loc) · 3.58 KB
/
PgpKeyPair2021.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import * as openpgp from 'openpgp';
export class PgpKeyPair2021 {
public id: string;
public type: string = 'PgpVerificationKey2021';
public controller: string;
public privateKey: any; // https://github.com/openpgpjs/openpgpjs/blob/master/src/key/key.js
public publicKey: any; // https://github.com/openpgpjs/openpgpjs/blob/master/src/key/key.js
public passphrase?: string;
static async generate(
{ userIds, curve, rsaBits, passphrase }: any,
options = {}
) {
if (curve) {
const {
privateKeyArmored,
publicKeyArmored,
revocationCertificate,
} = await openpgp.generateKey({
userIds, // you can pass multiple user IDs
curve, // ECC curve name
passphrase, // protects the private key
});
return PgpKeyPair2021.from({
privateKeyPgp: privateKeyArmored,
publicKeyPgp: publicKeyArmored,
revocationCertificate,
...options,
});
} else if (rsaBits) {
const {
privateKeyArmored,
publicKeyArmored,
revocationCertificate,
} = await openpgp.generateKey({
userIds, // you can pass multiple user IDs
rsaBits, // RSA key size
passphrase, // protects the private key
});
return PgpKeyPair2021.from({
privateKeyPgp: privateKeyArmored,
publicKeyPgp: publicKeyArmored,
revocationCertificate,
...options,
});
}
throw new Error('curve or rsaBits required');
}
public static async from(options: any) {
const {
keys: [publicKey],
} = await openpgp.key.readArmored(options.publicKeyPgp);
const {
keys: [privateKey],
} = await openpgp.key.readArmored(options.privateKeyPgp);
if (options.passphrase) {
await privateKey.decrypt(options.passphrase);
}
return new PgpKeyPair2021({
id: options.id,
controller: options.controller,
privateKey,
publicKey,
});
}
constructor(options: any = {}) {
this.id = options.id;
this.controller = options.controller;
this.privateKey = options.privateKey;
this.publicKey = options.publicKey;
this.passphrase = options.passphrase;
}
public async fingerprint() {
return this.publicKey.getFingerprint().toUpperCase();
}
public toKeyPair(exportPrivateKey = false) {
let keypair: any = {
id: this.id,
type: this.type,
controller: this.controller,
publicKeyPgp: this.publicKey.armor(),
};
if (exportPrivateKey) {
keypair.privateKeyPgp = this.privateKey.armor();
}
return keypair;
}
public signer() {
let privateKey = this.privateKey;
return {
async sign({ data }: any) {
if (!privateKey) {
throw new Error('No private key to sign with.');
}
const { signature: detachedSignature } = await openpgp.sign({
message: openpgp.message.fromBinary(Buffer.from(data)),
privateKeys: [privateKey],
detached: true,
});
return detachedSignature;
},
};
}
public verifier() {
let publicKey = this.publicKey;
return {
async verify({ data, signature }: any) {
if (!publicKey) {
throw new Error('No public key to verify with.');
}
const { signatures } = await openpgp.verify({
message: openpgp.message.fromBinary(Buffer.from(data)),
signature: await openpgp.signature.readArmored(signature), // parse detached signature
publicKeys: [publicKey], // for verification
});
const { valid } = signatures[0];
return valid;
},
};
}
}