mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-30 00:50:47 +03:00
1 parent
ea75c38a09
commit
8e73a01af5
4 files changed
+327
No files matched your search
@@ -0,0 +1,12 @@
|
||||
import { defineTool } from '../tool';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: 'SSH Key Format Converter',
|
||||
path: '/ssh-key-converter',
|
||||
description: 'Convert SSH keys between different formats',
|
||||
keywords: ['ssh', 'key', 'rfc4716', 'pkcs1', 'pem', 'pkcs8', 'converter'],
|
||||
component: () => import('./ssh-key-converter.vue'),
|
||||
icon: defineAsyncComponent(() => import('@vicons/tabler/es/ShieldChevron')),
|
||||
createdAt: new Date('2026-11-29'),
|
||||
category: 'Crypto',
|
||||
});
|
||||
@@ -0,0 +1,147 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { detectFormat, openSSHToRFC4716, rfc4716ToOpenSSH } from './ssh-key-converter.service';
|
||||
|
||||
const OPENSSH_ED25519 = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMaABCDEFGHIJKLMNopqrstuvwxyz1234567890 test@example.com';
|
||||
|
||||
describe('detectFormat', () => {
|
||||
it('detects openssh rsa key', () => {
|
||||
expect(detectFormat('ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC')).toBe('ssh');
|
||||
});
|
||||
|
||||
it('detects openssh ed25519 key', () => {
|
||||
expect(detectFormat(OPENSSH_ED25519)).toBe('ssh');
|
||||
});
|
||||
|
||||
it('detects ecdsa key', () => {
|
||||
expect(detectFormat('ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTY=')).toBe('ssh');
|
||||
});
|
||||
|
||||
it('detects rfc4716 key', () => {
|
||||
expect(
|
||||
detectFormat(`
|
||||
---- BEGIN SSH2 PUBLIC KEY ----
|
||||
AAAA
|
||||
---- END SSH2 PUBLIC KEY ----
|
||||
`),
|
||||
).toBe('rfc4716');
|
||||
});
|
||||
|
||||
it('detects pem public key', () => {
|
||||
expect(
|
||||
detectFormat(`
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
AAAA
|
||||
-----END PUBLIC KEY-----
|
||||
`),
|
||||
).toBe('pem');
|
||||
});
|
||||
|
||||
it('detects rsa pem public key', () => {
|
||||
expect(
|
||||
detectFormat(`
|
||||
-----BEGIN RSA PUBLIC KEY-----
|
||||
AAAA
|
||||
-----END RSA PUBLIC KEY-----
|
||||
`),
|
||||
).toBe('pem');
|
||||
});
|
||||
|
||||
it('detects pkcs8 private key', () => {
|
||||
expect(
|
||||
detectFormat(`
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
AAAA
|
||||
-----END PRIVATE KEY-----
|
||||
`),
|
||||
).toBe('pkcs8');
|
||||
});
|
||||
|
||||
it('throws for unknown format', () => {
|
||||
expect(() => detectFormat('this is not a key')).toThrow('Cannot detect key format');
|
||||
});
|
||||
});
|
||||
|
||||
describe('openSSHToRFC4716', () => {
|
||||
it('creates valid rfc4716 envelope', () => {
|
||||
const result = openSSHToRFC4716(OPENSSH_ED25519);
|
||||
|
||||
expect(result).toContain('BEGIN SSH2 PUBLIC KEY');
|
||||
|
||||
expect(result).toContain('END SSH2 PUBLIC KEY');
|
||||
|
||||
expect(result).toContain('Comment: "test@example.com"');
|
||||
|
||||
expect(result).toContain('AAAAC3NzaC1lZDI1NTE5');
|
||||
});
|
||||
|
||||
it('supports commentless keys', () => {
|
||||
const result = openSSHToRFC4716('ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC');
|
||||
|
||||
expect(result).not.toContain('Comment:');
|
||||
});
|
||||
|
||||
it('throws for malformed openssh input', () => {
|
||||
expect(() => openSSHToRFC4716('not-a-key')).toThrow('Invalid OpenSSH key');
|
||||
});
|
||||
|
||||
it('wraps long base64 content', () => {
|
||||
const longBase64 = 'A'.repeat(200);
|
||||
|
||||
const result = openSSHToRFC4716(`ssh-rsa ${longBase64}`);
|
||||
|
||||
const lines = result.split('\n');
|
||||
|
||||
const payloadLines = lines.filter((line) => !line.startsWith('----') && !line.startsWith('Comment:'));
|
||||
|
||||
expect(payloadLines.some((line) => line.length <= 72)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('rfc4716ToOpenSSH', () => {
|
||||
it('converts rfc4716 to openssh', () => {
|
||||
const blob = 'AAAAC3NzaC1lZDI1NTE5AAAAIMaABCDEFGHIJKLMNopqrstuvwxyz1234567890';
|
||||
|
||||
const rfc = `
|
||||
---- BEGIN SSH2 PUBLIC KEY ----
|
||||
Comment: "user@test"
|
||||
${blob}
|
||||
---- END SSH2 PUBLIC KEY ----
|
||||
`;
|
||||
|
||||
const result = rfc4716ToOpenSSH(rfc);
|
||||
|
||||
expect(result.startsWith('ssh-ed25519 ')).toBe(true);
|
||||
|
||||
expect(result.endsWith(blob)).toBe(true);
|
||||
});
|
||||
|
||||
it('ignores comments', () => {
|
||||
const blob = 'AAAAC3NzaC1lZDI1NTE5AAAAIMaABCDEFGHIJKLMNopqrstuvwxyz1234567890';
|
||||
|
||||
const result = rfc4716ToOpenSSH(`
|
||||
---- BEGIN SSH2 PUBLIC KEY ----
|
||||
Comment: "one"
|
||||
Comment: "two"
|
||||
${blob}
|
||||
---- END SSH2 PUBLIC KEY ----
|
||||
`);
|
||||
|
||||
expect(result.startsWith('ssh-ed25519 ')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('round trip conversions', () => {
|
||||
it('preserves payload through openssh->rfc4716 conversion', () => {
|
||||
const openssh = OPENSSH_ED25519;
|
||||
|
||||
const rfc = openSSHToRFC4716(openssh);
|
||||
|
||||
const payload = rfc
|
||||
.split('\n')
|
||||
.filter((line) => !line.startsWith('----') && !line.startsWith('Comment:'))
|
||||
.join('');
|
||||
|
||||
expect(payload).toBe(openssh.split(' ')[1]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
export type KeyFormat = 'auto' | 'ssh' | 'rfc4716' | 'pem' | 'pkcs8';
|
||||
|
||||
export function detectFormat(key: string): KeyFormat {
|
||||
const text = key.trim();
|
||||
|
||||
if (text.includes('BEGIN SSH2 PUBLIC KEY')) {
|
||||
return 'rfc4716';
|
||||
}
|
||||
|
||||
if (text.startsWith('ssh-rsa') || text.startsWith('ssh-ed25519') || text.startsWith('ecdsa-sha2-')) {
|
||||
return 'ssh';
|
||||
}
|
||||
|
||||
if (text.includes('BEGIN PUBLIC KEY') || text.includes('BEGIN RSA PUBLIC KEY')) {
|
||||
return 'pem';
|
||||
}
|
||||
|
||||
if (text.includes('BEGIN PRIVATE KEY')) {
|
||||
return 'pkcs8';
|
||||
}
|
||||
|
||||
throw new Error('Cannot detect key format');
|
||||
}
|
||||
|
||||
export function rfc4716ToOpenSSH(rfc: string): string {
|
||||
const lines = rfc
|
||||
.split(/\r?\n/)
|
||||
.map((x) => x.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
const payload = lines.filter((line) => !line.startsWith('----') && !line.startsWith('Comment:')).join('');
|
||||
|
||||
const blob = Buffer.from(payload, 'base64');
|
||||
|
||||
const typeLength = blob.readUInt32BE(0);
|
||||
|
||||
const type = blob.subarray(4, 4 + typeLength).toString();
|
||||
|
||||
return `${type} ${payload}`;
|
||||
}
|
||||
|
||||
export function openSSHToRFC4716(openssh: string): string {
|
||||
const parts = openssh.trim().split(/\s+/);
|
||||
|
||||
if (parts.length < 2) {
|
||||
throw new Error('Invalid OpenSSH key');
|
||||
}
|
||||
|
||||
const comment = parts.slice(2).join(' ');
|
||||
|
||||
const base64 = parts[1];
|
||||
|
||||
const output: string[] = ['---- BEGIN SSH2 PUBLIC KEY ----'];
|
||||
|
||||
if (comment) {
|
||||
output.push(`Comment: "${comment}"`);
|
||||
}
|
||||
|
||||
for (let i = 0; i < base64.length; i += 72) {
|
||||
output.push(base64.slice(i, i + 72));
|
||||
}
|
||||
|
||||
output.push('---- END SSH2 PUBLIC KEY ----');
|
||||
|
||||
return output.join('\n');
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<template>
|
||||
<div>
|
||||
<n-grid :cols="2" :x-gap="12">
|
||||
<n-gi>
|
||||
<n-form-item label="Source Format">
|
||||
<n-select v-model:value="sourceFormat" :options="sourceOptions" />
|
||||
</n-form-item>
|
||||
</n-gi>
|
||||
|
||||
<n-gi>
|
||||
<n-form-item label="Target Format">
|
||||
<n-select v-model:value="targetFormat" :options="targetOptions" />
|
||||
</n-form-item>
|
||||
</n-gi>
|
||||
</n-grid>
|
||||
|
||||
<n-form-item label="Input Key" label-placement="top">
|
||||
<n-input v-model:value="inputKey" type="textarea" :rows="12" placeholder="Paste SSH key here..." />
|
||||
</n-form-item>
|
||||
|
||||
<n-alert v-if="error" type="error">
|
||||
{{ error }}
|
||||
</n-alert>
|
||||
|
||||
<n-card v-if="outputKey" title="Output Key" type="info" size="small" mb-1>
|
||||
<textarea-copyable :value="outputKey" readonly />
|
||||
</n-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import sshpk from 'sshpk';
|
||||
import { detectFormat, openSSHToRFC4716, rfc4716ToOpenSSH, type KeyFormat } from './ssh-key-converter.service';
|
||||
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const inputKey = ref('');
|
||||
const error = ref('');
|
||||
|
||||
const sourceFormat = ref<KeyFormat>('auto');
|
||||
const targetFormat = useQueryParamOrStorage<KeyFormat>({
|
||||
name: 'fmt',
|
||||
storageName: 'ssh-key-conv:fmt',
|
||||
defaultValue: 'rfc4716',
|
||||
});
|
||||
|
||||
const sourceOptions = [
|
||||
{ label: 'Auto Detect', value: 'auto' },
|
||||
{ label: 'OpenSSH', value: 'ssh' },
|
||||
{ label: 'RFC4716', value: 'rfc4716' },
|
||||
{ label: 'PEM', value: 'pem' },
|
||||
{ label: 'PKCS8', value: 'pkcs8' },
|
||||
];
|
||||
|
||||
const targetOptions = [
|
||||
{ label: 'OpenSSH', value: 'ssh' },
|
||||
{ label: 'RFC4716', value: 'rfc4716' },
|
||||
{ label: 'PEM', value: 'pem' },
|
||||
{ label: 'PKCS8', value: 'pkcs8' },
|
||||
];
|
||||
|
||||
function parseKey(key: string, format: KeyFormat) {
|
||||
try {
|
||||
return sshpk.parseKey(key, format as any);
|
||||
} catch {
|
||||
return sshpk.parsePrivateKey(key, format as any);
|
||||
}
|
||||
}
|
||||
|
||||
const outputKey = computed(() => {
|
||||
error.value = '';
|
||||
|
||||
try {
|
||||
const actualFormat = sourceFormat.value === 'auto' ? detectFormat(inputKey.value) : sourceFormat.value;
|
||||
|
||||
let normalized = inputKey.value;
|
||||
|
||||
if (actualFormat === 'rfc4716') {
|
||||
normalized = rfc4716ToOpenSSH(inputKey.value);
|
||||
}
|
||||
|
||||
if (targetFormat.value === 'rfc4716') {
|
||||
let openssh: string;
|
||||
|
||||
if (actualFormat === 'rfc4716') {
|
||||
openssh = rfc4716ToOpenSSH(inputKey.value);
|
||||
} else {
|
||||
const key = parseKey(normalized, actualFormat);
|
||||
|
||||
openssh = key.toString('ssh');
|
||||
}
|
||||
|
||||
return openSSHToRFC4716(openssh);
|
||||
}
|
||||
|
||||
const key = parseKey(normalized, actualFormat === 'rfc4716' ? 'ssh' : actualFormat);
|
||||
|
||||
return key.toString(targetFormat.value as any);
|
||||
} catch (e: any) {
|
||||
error.value = e.toString();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
Reference in new issue
Block a user