Skip to content

Commit 503cf67

Browse files
committed
feat(rsa-key-generator): passphrase and formats
Fix CorentinTh#281 (rsa with passphrase and comment) Supports many formats: PEM (with or without passphrase), PKCS#1, PKCS#8, OpenSSH Standard (with or without passphrase), OpenSSH (new format) and PuTTY
1 parent 275a148 commit 503cf67

File tree

5 files changed

+104
-22
lines changed

5 files changed

+104
-22
lines changed

components.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ declare module '@vue/runtime-core' {
156156
NH3: typeof import('naive-ui')['NH3']
157157
NIcon: typeof import('naive-ui')['NIcon']
158158
NImage: typeof import('naive-ui')['NImage']
159+
NInput: typeof import('naive-ui')['NInput']
159160
NInputGroup: typeof import('naive-ui')['NInputGroup']
160161
NInputGroupLabel: typeof import('naive-ui')['NInputGroupLabel']
161162
NInputNumber: typeof import('naive-ui')['NInputNumber']
@@ -165,6 +166,7 @@ declare module '@vue/runtime-core' {
165166
NProgress: typeof import('naive-ui')['NProgress']
166167
NScrollbar: typeof import('naive-ui')['NScrollbar']
167168
NSlider: typeof import('naive-ui')['NSlider']
169+
NSpace: typeof import('naive-ui')['NSpace']
168170
NStatistic: typeof import('naive-ui')['NStatistic']
169171
NSwitch: typeof import('naive-ui')['NSwitch']
170172
NTable: typeof import('naive-ui')['NTable']

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "it-tools",
3-
"version": "2023.11.2-7d94e11",
3+
"version": "2023.12.21-5ed3693",
44
"description": "Collection of handy online tools for developers, with great UX. ",
55
"keywords": [
66
"productivity",
@@ -86,7 +86,7 @@
8686
"unplugin-auto-import": "^0.16.4",
8787
"uuid": "^9.0.0",
8888
"vue": "^3.3.4",
89-
"vue-i18n": "^9.2.2",
89+
"vue-i18n": "^9.9.1",
9090
"vue-router": "^4.1.6",
9191
"vue-tsc": "^1.8.1",
9292
"xml-formatter": "^3.3.2",
@@ -95,7 +95,7 @@
9595
"devDependencies": {
9696
"@antfu/eslint-config": "^0.41.0",
9797
"@iconify-json/mdi": "^1.1.50",
98-
"@intlify/unplugin-vue-i18n": "^0.13.0",
98+
"@intlify/unplugin-vue-i18n": "^2.0.0",
9999
"@playwright/test": "^1.32.3",
100100
"@rushstack/eslint-patch": "^1.2.0",
101101
"@tsconfig/node18": "^18.2.0",

src/tools/rsa-key-pair-generator/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { translate } from '@/plugins/i18n.plugin';
55
export const tool = defineTool({
66
name: translate('tools.rsa-key-pair-generator.title'),
77
path: '/rsa-key-pair-generator',
8-
description: translate('tools.rsa-key-pair-generator.description'),
9-
keywords: ['rsa', 'key', 'pair', 'generator', 'public', 'private', 'secret', 'ssh', 'pem'],
8+
description: 'Generate new random RSA private and public keys (with or without passphrase).',
9+
keywords: ['rsa', 'key', 'pair', 'generator', 'public', 'private', 'secret', 'ssh', 'pem', 'passphrase', 'password'],
1010
component: () => import('./rsa-key-pair-generator.vue'),
1111
icon: Certificate,
1212
});

src/tools/rsa-key-pair-generator/rsa-key-pair-generator.service.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pki } from 'node-forge';
22
import workerScript from 'node-forge/dist/prime.worker.min?url';
3+
import sshpk from 'sshpk';
34

45
export { generateKeyPair };
56

@@ -16,11 +17,39 @@ function generateRawPairs({ bits = 2048 }) {
1617
);
1718
}
1819

19-
async function generateKeyPair(config: { bits?: number } = {}) {
20+
async function generateKeyPair(config: {
21+
bits?: number
22+
password?: string
23+
format?: sshpk.PrivateKeyFormatType
24+
comment?: string
25+
} = {}) {
2026
const { privateKey, publicKey } = await generateRawPairs(config);
2127

28+
const privateUnencryptedKeyPem = pki.privateKeyToPem(privateKey);
29+
30+
if (config?.format === 'pem') {
31+
return {
32+
publicKey: pki.publicKeyToPem(publicKey),
33+
privateKey: config?.password
34+
? pki.encryptRsaPrivateKey(privateKey, config?.password)
35+
: privateUnencryptedKeyPem,
36+
};
37+
}
38+
39+
const privKey = sshpk.parsePrivateKey(privateUnencryptedKeyPem);
40+
privKey.comment = config?.comment;
41+
const pubFormat = config.format ?? 'ssh';
42+
let privFormat = config.format ?? 'ssh';
43+
if (privFormat === 'ssh') {
44+
privFormat = 'ssh-private';
45+
}
46+
const pubKey = privKey.toPublic();
2247
return {
23-
publicKeyPem: pki.publicKeyToPem(publicKey),
24-
privateKeyPem: pki.privateKeyToPem(privateKey),
48+
publicKey: pubKey.toString(pubFormat),
49+
privateKey: config?.password
50+
? privKey.toString(privFormat,
51+
{ passphrase: config?.password, comment: config?.comment },
52+
)
53+
: privKey.toString(privFormat, { comment: config?.comment }),
2554
};
2655
}
Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
<script setup lang="ts">
2+
import type sshpk from 'sshpk';
23
import { generateKeyPair } from './rsa-key-pair-generator.service';
34
import TextareaCopyable from '@/components/TextareaCopyable.vue';
45
import { withDefaultOnErrorAsync } from '@/utils/defaults';
56
import { useValidation } from '@/composable/validation';
67
import { computedRefreshableAsync } from '@/composable/computedRefreshable';
78
89
const bits = ref(2048);
9-
const emptyCerts = { publicKeyPem: '', privateKeyPem: '' };
10+
const comment = ref('');
11+
const password = ref('');
12+
const emptyCerts = { publicKey: '', privateKey: '' };
13+
14+
const format = useStorage('rsa-key-pair-generator:format', 'ssh');
15+
const formatOptions = [
16+
{ value: 'pem', label: 'PEM' },
17+
{ value: 'pkcs1', label: 'PKCS#1' },
18+
{ value: 'pkcs8', label: 'PKCS#8' },
19+
{ value: 'ssh', label: 'OpenSSH Standard' },
20+
{ value: 'openssh', label: 'OpenSSH New' },
21+
{ value: 'putty', label: 'PuTTY' },
22+
];
23+
24+
const supportsPassphrase = computed(() => format.value === 'pem' || format.value === 'ssh');
1025
1126
const { attrs: bitsValidationAttrs } = useValidation({
1227
source: bits,
@@ -19,31 +34,67 @@ const { attrs: bitsValidationAttrs } = useValidation({
1934
});
2035
2136
const [certs, refreshCerts] = computedRefreshableAsync(
22-
() => withDefaultOnErrorAsync(() => generateKeyPair({ bits: bits.value }), emptyCerts),
37+
() => withDefaultOnErrorAsync(() => generateKeyPair({
38+
bits: bits.value,
39+
password: password.value,
40+
format: format.value as sshpk.PrivateKeyFormatType,
41+
comment: comment.value,
42+
}), emptyCerts),
2343
emptyCerts,
2444
);
2545
</script>
2646

2747
<template>
28-
<div style="flex: 0 0 100%">
29-
<div item-style="flex: 1 1 0" style="max-width: 600px" mx-auto flex gap-3>
30-
<n-form-item label="Bits :" v-bind="bitsValidationAttrs as any" label-placement="left" label-width="100">
48+
<div>
49+
<n-space justify="space-between" mb-1>
50+
<c-select
51+
v-model:value="format"
52+
label-position="left"
53+
label="Format:"
54+
:options="formatOptions"
55+
placeholder="Select a key format"
56+
/>
57+
58+
<n-form-item label="Bits :" v-bind="bitsValidationAttrs as any" label-placement="left">
3159
<n-input-number v-model:value="bits" min="256" max="16384" step="8" />
3260
</n-form-item>
61+
</n-space>
3362

63+
<div v-if="supportsPassphrase" mb-1>
64+
<n-form-item label="Passphrase :" label-placement="left">
65+
<n-input
66+
v-model:value="password"
67+
type="password"
68+
show-password-on="mousedown"
69+
placeholder="Passphrase"
70+
/>
71+
</n-form-item>
72+
</div>
73+
74+
<div mb-1>
75+
<n-form-item label="Comment :" label-placement="left">
76+
<n-input
77+
v-model:value="comment"
78+
type="text"
79+
placeholder="Comment"
80+
/>
81+
</n-form-item>
82+
</div>
83+
84+
<n-space justify="center" mb-1>
3485
<c-button @click="refreshCerts">
3586
Refresh key-pair
3687
</c-button>
37-
</div>
38-
</div>
88+
</n-space>
3989

40-
<div>
41-
<h3>Public key</h3>
42-
<TextareaCopyable :value="certs.publicKeyPem" />
43-
</div>
90+
<div>
91+
<h3>Public key</h3>
92+
<TextareaCopyable :value="certs.publicKey" :word-wrap="true" />
93+
</div>
4494

45-
<div>
46-
<h3>Private key</h3>
47-
<TextareaCopyable :value="certs.privateKeyPem" />
95+
<div>
96+
<h3>Private key</h3>
97+
<TextareaCopyable :value="certs.privateKey" />
98+
</div>
4899
</div>
49100
</template>

0 commit comments

Comments
 (0)