Skip to content

Commit ddd3f6e

Browse files
committed
fix: refactor and unit test
1 parent c10302b commit ddd3f6e

File tree

4 files changed

+673
-406
lines changed

4 files changed

+673
-406
lines changed
Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
import type {
2+
Certificate,
3+
Fingerprint,
4+
Key,
5+
PrivateKey, Signature,
6+
} from 'sshpk';
7+
import type * as openpgp from 'openpgp';
8+
import * as forge from 'node-forge';
9+
10+
export interface LabelValue {
11+
label: string
12+
value: string
13+
multiline?: boolean
14+
}
15+
16+
function onErrorReturnErrorMessage(func: () => any) {
17+
try {
18+
return func();
19+
}
20+
catch (e: any) {
21+
return e.toString();
22+
}
23+
}
24+
25+
function buf2Hex(buffer: ArrayBuffer) { // buffer is an ArrayBuffer
26+
return [...new Uint8Array(buffer)]
27+
.map(x => x.toString(16).padStart(2, '0'))
28+
.join('');
29+
}
30+
31+
export function getPublicKeyLabelValues(publicKey: Key) {
32+
return [
33+
{
34+
label: 'Type:',
35+
value: 'Public Key',
36+
},
37+
{
38+
label: 'Key Type:',
39+
value: publicKey.type,
40+
},
41+
{
42+
label: 'Size:',
43+
value: publicKey.size,
44+
},
45+
{
46+
label: 'Comment:',
47+
value: publicKey.comment,
48+
multiline: true,
49+
},
50+
{
51+
label: 'Curve:',
52+
value: publicKey.curve ?? 'none',
53+
},
54+
{
55+
label: 'Fingerprint (sha256):',
56+
value: onErrorReturnErrorMessage(() => publicKey.fingerprint('sha256')),
57+
multiline: true,
58+
},
59+
{
60+
label: 'Fingerprint (sha512):',
61+
value: onErrorReturnErrorMessage(() => publicKey.fingerprint('sha512')),
62+
multiline: true,
63+
},
64+
] as LabelValue[];
65+
}
66+
67+
export function getPrivateKeyLabelValues(privateKey: PrivateKey) {
68+
return [
69+
{
70+
label: 'Type:',
71+
value: 'Private Key',
72+
},
73+
{
74+
label: 'Key Type:',
75+
value: privateKey.type,
76+
},
77+
{
78+
label: 'Size:',
79+
value: privateKey.size,
80+
},
81+
{
82+
label: 'Comment:',
83+
value: privateKey.comment,
84+
multiline: true,
85+
},
86+
{
87+
label: 'Curve:',
88+
value: privateKey.curve,
89+
},
90+
{
91+
label: 'Fingerprint (sha256):',
92+
value: onErrorReturnErrorMessage(() => privateKey.fingerprint('sha256')),
93+
multiline: true,
94+
},
95+
{
96+
label: 'Fingerprint (sha512):',
97+
value: onErrorReturnErrorMessage(() => privateKey.fingerprint('sha512')),
98+
multiline: true,
99+
},
100+
] as LabelValue[];
101+
}
102+
103+
export function getCertificateLabelValues(cert: Certificate) {
104+
return [
105+
{
106+
label: 'Type:',
107+
value: 'Certificate',
108+
},
109+
{
110+
label: 'Subjects:',
111+
value: cert.subjects?.map(s => s.toString()).join('\n'),
112+
multiline: true,
113+
},
114+
{
115+
label: 'Issuer:',
116+
value: cert.issuer.toString(),
117+
multiline: true,
118+
},
119+
{
120+
label: 'Subject Key:',
121+
value: onErrorReturnErrorMessage(() => cert.subjectKey?.toString('ssh')),
122+
multiline: true,
123+
},
124+
{
125+
label: 'Subject Key Type:',
126+
value: cert.subjectKey?.type,
127+
},
128+
{
129+
label: 'Subject Size:',
130+
value: cert.subjectKey?.size,
131+
},
132+
{
133+
label: 'Subject Comment:',
134+
value: cert.subjectKey?.comment,
135+
multiline: true,
136+
},
137+
{
138+
label: 'Subject Curve:',
139+
value: cert.subjectKey?.curve ?? 'none',
140+
},
141+
{
142+
label: 'Issuer Key:',
143+
value: onErrorReturnErrorMessage(() => cert.issuerKey?.toString('ssh')),
144+
multiline: true,
145+
},
146+
{
147+
label: 'Serial:',
148+
value: buf2Hex(cert.serial),
149+
},
150+
{
151+
label: 'Purposes:',
152+
value: cert.purposes?.join(', '),
153+
},
154+
{
155+
label: 'Extensions:',
156+
value: JSON.stringify(cert.getExtensions(), null, 2),
157+
multiline: true,
158+
},
159+
{
160+
label: 'Fingerprint (sha256):',
161+
value: onErrorReturnErrorMessage(() => cert.fingerprint('sha256')),
162+
multiline: true,
163+
},
164+
{
165+
label: 'Fingerprint (sha512):',
166+
value: onErrorReturnErrorMessage(() => cert.fingerprint('sha512')),
167+
multiline: true,
168+
},
169+
{
170+
label: 'Certificate (pem):',
171+
value: onErrorReturnErrorMessage(() => cert.toString('pem')),
172+
multiline: true,
173+
},
174+
] as LabelValue[];
175+
}
176+
177+
export async function getPGPPublicKeyLabelValuesAsync(pgpPublicKey: openpgp.Key) {
178+
return [
179+
{
180+
label: 'Type:',
181+
value: 'PGP Public Key',
182+
},
183+
{
184+
label: 'Creation Time:',
185+
value: pgpPublicKey.getCreationTime().toString(),
186+
},
187+
{
188+
label: 'Expiration Time:',
189+
value: (await pgpPublicKey.getExpirationTime())?.toString() || '',
190+
},
191+
{
192+
label: 'Algorithm Info:',
193+
value: JSON.stringify(pgpPublicKey.getAlgorithmInfo()),
194+
},
195+
{
196+
label: 'Fingerprint:',
197+
value: pgpPublicKey.getFingerprint(),
198+
},
199+
{
200+
label: 'User ID(s):',
201+
value: pgpPublicKey.getUserIDs().join(', '),
202+
},
203+
{
204+
label: 'Key ID(s):',
205+
value: pgpPublicKey.getKeyIDs().map(k => k.toHex()).join(' ; '),
206+
},
207+
] as LabelValue[];
208+
}
209+
210+
export async function getPGPPrivateKeyLabelValuesAsync(pgpPrivateKey: openpgp.Key) {
211+
return [
212+
{
213+
label: 'Type:',
214+
value: 'PGP Private Key',
215+
},
216+
{
217+
label: 'Creation Time:',
218+
value: pgpPrivateKey.getCreationTime().toString(),
219+
},
220+
{
221+
label: 'Expiration Time:',
222+
value: (await pgpPrivateKey.getExpirationTime())?.toString() || '',
223+
},
224+
{
225+
label: 'Algorithm Info:',
226+
value: JSON.stringify(pgpPrivateKey.getAlgorithmInfo()),
227+
},
228+
{
229+
label: 'Fingerprint:',
230+
value: pgpPrivateKey.getFingerprint(),
231+
},
232+
{
233+
label: 'User ID(s):',
234+
value: pgpPrivateKey.getUserIDs().join(', '),
235+
},
236+
{
237+
label: 'Key ID(s):',
238+
value: pgpPrivateKey.getKeyIDs().map(k => k.toHex()).join(' ; '),
239+
},
240+
] as LabelValue[];
241+
}
242+
243+
export function getCSRLabelValues(csr: forge.pki.Certificate) {
244+
return [
245+
{
246+
label: 'Type:',
247+
value: 'Certificate Signing Request',
248+
},
249+
{
250+
label: 'Subject:',
251+
value: csr.subject?.attributes?.map(a => JSON.stringify(a, null, 2)).join('\n'),
252+
multiline: true,
253+
},
254+
{
255+
label: 'Issuer:',
256+
value: csr.issuer?.toString(),
257+
multiline: true,
258+
},
259+
{
260+
label: 'Validity:',
261+
value: JSON.stringify(csr.validity, null, 2),
262+
},
263+
{
264+
label: 'Signature:',
265+
value: csr.signature,
266+
},
267+
{
268+
label: 'Signature Oid:',
269+
value: csr.signatureOid?.toString(),
270+
},
271+
{
272+
label: 'Signature parameters:',
273+
value: JSON.stringify(csr.signatureParameters, null, 2),
274+
},
275+
{
276+
label: 'Signing info:',
277+
value: JSON.stringify(csr.siginfo, null, 2),
278+
},
279+
{
280+
label: 'Serial:',
281+
value: csr.serialNumber?.toString(),
282+
},
283+
{
284+
label: 'Extensions:',
285+
value: JSON.stringify(csr.extensions, null, 2),
286+
multiline: true,
287+
},
288+
{
289+
label: 'Public Key:',
290+
value: onErrorReturnErrorMessage(() => forge.pki.publicKeyToPem(csr.publicKey)),
291+
multiline: true,
292+
},
293+
{
294+
label: 'Public Key Fingerprint:',
295+
value: onErrorReturnErrorMessage(() => forge.pki.getPublicKeyFingerprint(csr.publicKey)?.toHex()),
296+
multiline: true,
297+
},
298+
] as LabelValue[];
299+
}
300+
301+
export function getFingerprintLabelValues(fingerprint: Fingerprint) {
302+
return [
303+
{
304+
label: 'Type:',
305+
value: 'Fingerprint',
306+
},
307+
{
308+
label: 'Fingerprint (hex):',
309+
value: fingerprint.toString('hex'),
310+
},
311+
{
312+
label: 'Fingerprint (base64):',
313+
value: fingerprint.toString('base64'),
314+
},
315+
] as LabelValue[];
316+
}
317+
318+
export function getSignatureLabelValues(signature: Signature) {
319+
return [
320+
{
321+
label: 'Type:',
322+
value: 'Signature',
323+
},
324+
{
325+
label: 'Fingerprint (asn1):',
326+
value: signature.toString('asn1'),
327+
},
328+
{
329+
label: 'Fingerprint (ssh):',
330+
value: signature.toString('ssh'),
331+
},
332+
] as LabelValue[];
333+
}

0 commit comments

Comments
 (0)