Skip to content

Commit 5732ce7

Browse files
committed
feat: add pgp parsing
1 parent 6445c80 commit 5732ce7

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"naive-ui": "^2.35.0",
7777
"netmask": "^2.0.2",
7878
"node-forge": "^1.3.1",
79+
"openpgp": "^5.11.1",
7980
"oui-data": "^1.0.10",
8081
"pdf-signature-reader": "^1.4.2",
8182
"pinia": "^2.0.34",

pnpm-lock.yaml

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools/certificate-key-parser/certificate-key-parser.vue

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
PrivateKey, Signature, SignatureFormatType,
1616
} from 'sshpk';
1717
import { Base64 } from 'js-base64';
18+
import * as openpgp from 'openpgp';
1819
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
1920
2021
function buf2Hex(buffer: ArrayBuffer) { // buffer is an ArrayBuffer
@@ -59,7 +60,7 @@ interface LabelValue {
5960
value: string
6061
multiline?: boolean
6162
}
62-
const parsedSections = computed<LabelValue[]>(() => {
63+
const parsedSections = computedAsync<LabelValue[]>(async () => {
6364
try {
6465
certificateX509DER.value = '';
6566
const onErrorReturnErrorMessage = (func: () => any) => {
@@ -78,6 +79,14 @@ const parsedSections = computed<LabelValue[]>(() => {
7879
return null;
7980
}
8081
};
82+
const canParseAsync = async (value: string | Buffer, parseFunction: (value: string | Buffer) => Promise<any>) => {
83+
try {
84+
return await parseFunction(value);
85+
}
86+
catch {
87+
return null;
88+
}
89+
};
8190
const inputKeyOrCertificateValue
8291
= inputKeyOrCertificate.value !== ''
8392
? inputKeyOrCertificate.value
@@ -263,6 +272,74 @@ const parsedSections = computed<LabelValue[]>(() => {
263272
] as LabelValue[];
264273
}
265274
275+
const pgpPrivateKey = await canParseAsync(inputKeyOrCertificateValue, value => openpgp.readPrivateKey({ armoredKey: value.toString() })) as openpgp.Key;
276+
if (pgpPrivateKey) {
277+
return [
278+
{
279+
label: 'Type: ',
280+
value: 'PGP Private Key',
281+
},
282+
{
283+
label: ': ',
284+
value: pgpPrivateKey.getCreationTime().toString(),
285+
},
286+
{
287+
label: ': ',
288+
value: (await pgpPrivateKey.getExpirationTime())?.toString() || '',
289+
},
290+
{
291+
label: 'Algorithm Info: ',
292+
value: JSON.stringify(pgpPrivateKey.getAlgorithmInfo()),
293+
},
294+
{
295+
label: 'Fingerprint: ',
296+
value: pgpPrivateKey.getFingerprint(),
297+
},
298+
{
299+
label: 'User ID(s): ',
300+
value: pgpPrivateKey.getUserIDs().join(', '),
301+
},
302+
{
303+
label: 'Key ID(s): ',
304+
value: pgpPrivateKey.getKeyIDs().map(k => k.toString()).join(' ; '),
305+
},
306+
] as LabelValue[];
307+
}
308+
309+
const pgpPublicKey = await canParseAsync(inputKeyOrCertificateValue, value => openpgp.readKey({ armoredKey: value.toString() })) as openpgp.Key;
310+
if (pgpPublicKey) {
311+
return [
312+
{
313+
label: 'Type: ',
314+
value: 'PGP Public Key',
315+
},
316+
{
317+
label: ': ',
318+
value: pgpPublicKey.getCreationTime().toString(),
319+
},
320+
{
321+
label: ': ',
322+
value: (await pgpPublicKey.getExpirationTime())?.toString() || '',
323+
},
324+
{
325+
label: 'Algorithm Info: ',
326+
value: JSON.stringify(pgpPublicKey.getAlgorithmInfo()),
327+
},
328+
{
329+
label: 'Fingerprint: ',
330+
value: pgpPublicKey.getFingerprint(),
331+
},
332+
{
333+
label: 'User ID(s): ',
334+
value: pgpPublicKey.getUserIDs().join(', '),
335+
},
336+
{
337+
label: 'Key ID(s): ',
338+
value: pgpPublicKey.getKeyIDs().map(k => k.toString()).join(' ; '),
339+
},
340+
] as LabelValue[];
341+
}
342+
266343
const signature = canParse(inputKeyOrCertificateValue, (value) => {
267344
//
268345
for (const algo of ['dsa', 'rsa', 'ecdsa', 'ed25519']) {

0 commit comments

Comments
 (0)