feat(rsa-key-generator): passphrase and formats

Fix #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
This commit is contained in:
sharevb 2024-02-17 17:28:58 +01:00 committed by ShareVB
parent 275a148be2
commit 503cf6715c
5 changed files with 105 additions and 23 deletions

2
components.d.ts vendored
View file

@ -156,6 +156,7 @@ declare module '@vue/runtime-core' {
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NImage: typeof import('naive-ui')['NImage']
NInput: typeof import('naive-ui')['NInput']
NInputGroup: typeof import('naive-ui')['NInputGroup']
NInputGroupLabel: typeof import('naive-ui')['NInputGroupLabel']
NInputNumber: typeof import('naive-ui')['NInputNumber']
@ -165,6 +166,7 @@ declare module '@vue/runtime-core' {
NProgress: typeof import('naive-ui')['NProgress']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSlider: typeof import('naive-ui')['NSlider']
NSpace: typeof import('naive-ui')['NSpace']
NStatistic: typeof import('naive-ui')['NStatistic']
NSwitch: typeof import('naive-ui')['NSwitch']
NTable: typeof import('naive-ui')['NTable']

View file

@ -1,6 +1,6 @@
{
"name": "it-tools",
"version": "2023.11.2-7d94e11",
"version": "2023.12.21-5ed3693",
"description": "Collection of handy online tools for developers, with great UX. ",
"keywords": [
"productivity",
@ -86,7 +86,7 @@
"unplugin-auto-import": "^0.16.4",
"uuid": "^9.0.0",
"vue": "^3.3.4",
"vue-i18n": "^9.2.2",
"vue-i18n": "^9.9.1",
"vue-router": "^4.1.6",
"vue-tsc": "^1.8.1",
"xml-formatter": "^3.3.2",
@ -95,7 +95,7 @@
"devDependencies": {
"@antfu/eslint-config": "^0.41.0",
"@iconify-json/mdi": "^1.1.50",
"@intlify/unplugin-vue-i18n": "^0.13.0",
"@intlify/unplugin-vue-i18n": "^2.0.0",
"@playwright/test": "^1.32.3",
"@rushstack/eslint-patch": "^1.2.0",
"@tsconfig/node18": "^18.2.0",

View file

@ -5,8 +5,8 @@ import { translate } from '@/plugins/i18n.plugin';
export const tool = defineTool({
name: translate('tools.rsa-key-pair-generator.title'),
path: '/rsa-key-pair-generator',
description: translate('tools.rsa-key-pair-generator.description'),
keywords: ['rsa', 'key', 'pair', 'generator', 'public', 'private', 'secret', 'ssh', 'pem'],
description: 'Generate new random RSA private and public keys (with or without passphrase).',
keywords: ['rsa', 'key', 'pair', 'generator', 'public', 'private', 'secret', 'ssh', 'pem', 'passphrase', 'password'],
component: () => import('./rsa-key-pair-generator.vue'),
icon: Certificate,
});

View file

@ -1,5 +1,6 @@
import { pki } from 'node-forge';
import workerScript from 'node-forge/dist/prime.worker.min?url';
import sshpk from 'sshpk';
export { generateKeyPair };
@ -16,11 +17,39 @@ function generateRawPairs({ bits = 2048 }) {
);
}
async function generateKeyPair(config: { bits?: number } = {}) {
async function generateKeyPair(config: {
bits?: number
password?: string
format?: sshpk.PrivateKeyFormatType
comment?: string
} = {}) {
const { privateKey, publicKey } = await generateRawPairs(config);
const privateUnencryptedKeyPem = pki.privateKeyToPem(privateKey);
if (config?.format === 'pem') {
return {
publicKey: pki.publicKeyToPem(publicKey),
privateKey: config?.password
? pki.encryptRsaPrivateKey(privateKey, config?.password)
: privateUnencryptedKeyPem,
};
}
const privKey = sshpk.parsePrivateKey(privateUnencryptedKeyPem);
privKey.comment = config?.comment;
const pubFormat = config.format ?? 'ssh';
let privFormat = config.format ?? 'ssh';
if (privFormat === 'ssh') {
privFormat = 'ssh-private';
}
const pubKey = privKey.toPublic();
return {
publicKeyPem: pki.publicKeyToPem(publicKey),
privateKeyPem: pki.privateKeyToPem(privateKey),
publicKey: pubKey.toString(pubFormat),
privateKey: config?.password
? privKey.toString(privFormat,
{ passphrase: config?.password, comment: config?.comment },
)
: privKey.toString(privFormat, { comment: config?.comment }),
};
}

View file

@ -1,4 +1,5 @@
<script setup lang="ts">
import type sshpk from 'sshpk';
import { generateKeyPair } from './rsa-key-pair-generator.service';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
import { withDefaultOnErrorAsync } from '@/utils/defaults';
@ -6,7 +7,21 @@ import { useValidation } from '@/composable/validation';
import { computedRefreshableAsync } from '@/composable/computedRefreshable';
const bits = ref(2048);
const emptyCerts = { publicKeyPem: '', privateKeyPem: '' };
const comment = ref('');
const password = ref('');
const emptyCerts = { publicKey: '', privateKey: '' };
const format = useStorage('rsa-key-pair-generator:format', 'ssh');
const formatOptions = [
{ value: 'pem', label: 'PEM' },
{ value: 'pkcs1', label: 'PKCS#1' },
{ value: 'pkcs8', label: 'PKCS#8' },
{ value: 'ssh', label: 'OpenSSH Standard' },
{ value: 'openssh', label: 'OpenSSH New' },
{ value: 'putty', label: 'PuTTY' },
];
const supportsPassphrase = computed(() => format.value === 'pem' || format.value === 'ssh');
const { attrs: bitsValidationAttrs } = useValidation({
source: bits,
@ -19,31 +34,67 @@ const { attrs: bitsValidationAttrs } = useValidation({
});
const [certs, refreshCerts] = computedRefreshableAsync(
() => withDefaultOnErrorAsync(() => generateKeyPair({ bits: bits.value }), emptyCerts),
() => withDefaultOnErrorAsync(() => generateKeyPair({
bits: bits.value,
password: password.value,
format: format.value as sshpk.PrivateKeyFormatType,
comment: comment.value,
}), emptyCerts),
emptyCerts,
);
</script>
<template>
<div style="flex: 0 0 100%">
<div item-style="flex: 1 1 0" style="max-width: 600px" mx-auto flex gap-3>
<n-form-item label="Bits :" v-bind="bitsValidationAttrs as any" label-placement="left" label-width="100">
<div>
<n-space justify="space-between" mb-1>
<c-select
v-model:value="format"
label-position="left"
label="Format:"
:options="formatOptions"
placeholder="Select a key format"
/>
<n-form-item label="Bits :" v-bind="bitsValidationAttrs as any" label-placement="left">
<n-input-number v-model:value="bits" min="256" max="16384" step="8" />
</n-form-item>
</n-space>
<div v-if="supportsPassphrase" mb-1>
<n-form-item label="Passphrase :" label-placement="left">
<n-input
v-model:value="password"
type="password"
show-password-on="mousedown"
placeholder="Passphrase"
/>
</n-form-item>
</div>
<div mb-1>
<n-form-item label="Comment :" label-placement="left">
<n-input
v-model:value="comment"
type="text"
placeholder="Comment"
/>
</n-form-item>
</div>
<n-space justify="center" mb-1>
<c-button @click="refreshCerts">
Refresh key-pair
</c-button>
</n-space>
<div>
<h3>Public key</h3>
<TextareaCopyable :value="certs.publicKey" :word-wrap="true" />
</div>
<div>
<h3>Private key</h3>
<TextareaCopyable :value="certs.privateKey" />
</div>
</div>
<div>
<h3>Public key</h3>
<TextareaCopyable :value="certs.publicKeyPem" />
</div>
<div>
<h3>Private key</h3>
<TextareaCopyable :value="certs.privateKeyPem" />
</div>
</template>