Merge branch 'feat/pgp-encryption' into chore/all-my-stuffs

# Conflicts:
#	components.d.ts
#	pnpm-lock.yaml
#	src/tools/index.ts
This commit is contained in:
ShareVB committed 2024-11-03 23:03:44 +01:00
commit 95a83fe434
5 files changed
+230 -2

No files matched your search

-1
View File
@@ -10094,7 +10094,6 @@ packages:
/glob@7.2.3:
resolution: {integrity: sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==}
deprecated: Glob versions prior to v9 are no longer supported
dependencies:
fs.realpath: 1.0.0
inflight: 1.0.6
+20 -1
View File
@@ -1,6 +1,6 @@
import { type Ref, ref, watchEffect } from 'vue';
export { computedCatch };
export { computedCatch, computedCatchAsync };
function computedCatch<T, D>(getter: () => T, { defaultValue }: { defaultValue: D; defaultErrorMessage?: string }): [Ref<T | D>, Ref<string | undefined>];
function computedCatch<T, D>(getter: () => T, { defaultValue, defaultErrorMessage = 'Unknown error' }: { defaultValue?: D; defaultErrorMessage?: string } = {}) {
@@ -20,3 +20,22 @@ function computedCatch<T, D>(getter: () => T, { defaultValue, defaultErrorMessag
return [value, error] as const;
}
function computedCatchAsync<T, D>(getterAsync: () => Promise<T>, { defaultValue }: { defaultValue: D; defaultErrorMessage?: string }): [Ref<T | D>, Ref<string | undefined>];
function computedCatchAsync<T, D>(getterAsync: () => Promise<T>, { defaultValue, defaultErrorMessage = 'Unknown error' }: { defaultValue?: D; defaultErrorMessage?: string } = {}) {
const error = ref<string | undefined>();
const value = ref<T | D | undefined>();
watchEffect(async () => {
try {
error.value = undefined;
value.value = await getterAsync();
}
catch (err) {
error.value = err instanceof Error ? err.message : err?.toString() ?? defaultErrorMessage;
value.value = defaultValue;
}
});
return [value, error] as const;
}
+4
View File
@@ -127,6 +127,7 @@ import { tool as markdownCheatsheet } from './markdown-cheatsheet';
import { tool as markdownEditor } from './markdown-editor';
import { tool as nanoMemo } from './nano-memo';
import { tool as option43Generator } from './option43-generator';
import { tool as pgpEncryption } from './pgp-encryption';
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
import { tool as numeronymGenerator } from './numeronym-generator';
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -270,6 +271,9 @@ export const toolsByCategory: ToolCategory[] = [
ed25519KeyPairGenerator,
passwordStrengthAnalyser,
pdfSignatureChecker,
passwordStrengthAnalyser,
pdfSignatureChecker,
pgpEncryption,
],
},
{
+12
View File
@@ -0,0 +1,12 @@
import { Lock } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'PGP encryption',
path: '/pgp-encryption',
description: 'Encrypt and decrypt text clear text using PGP Keys.',
keywords: ['pgp', 'openpgp', 'encryption', 'cypher', 'encipher', 'crypt', 'decrypt'],
component: () => import('./pgp-encryption.vue'),
icon: Lock,
createdAt: new Date('2024-04-20'),
});
+194
View File
@@ -0,0 +1,194 @@
<script setup lang="ts">
import * as openpgp from 'openpgp';
import { computedCatchAsync } from '@/composable/computed/catchedComputed';
const cryptInput = ref('');
const cryptPublicKey = ref('');
const cryptPrivateKey = ref('');
const cryptPrivateKeyPassphrase = ref('');
const [cryptOutput, cryptError] = computedCatchAsync(async () => {
const publicKeyArmored = cryptPublicKey.value;
const privateKeyArmored = cryptPrivateKey.value;
const passphrase = cryptPrivateKeyPassphrase.value;
const text = cryptInput.value;
const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
const privateKey = privateKeyArmored !== ''
? (passphrase !== ''
? await openpgp.decryptKey({
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
passphrase,
})
: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }))
: undefined;
return await openpgp.encrypt({
message: await openpgp.createMessage({ text }),
encryptionKeys: publicKey,
signingKeys: privateKey,
});
}, {
defaultValue: '',
defaultErrorMessage: 'Unable to encrypt your text',
});
const decryptInput = ref('');
const decryptPublicKey = ref('');
const decryptPrivateKey = ref('');
const decryptPrivateKeyPassphrase = ref('');
const [decryptOutput, decryptError] = computedCatchAsync(async () => {
const publicKeyArmored = decryptPublicKey.value;
const privateKeyArmored = decryptPrivateKey.value;
const passphrase = decryptPrivateKeyPassphrase.value;
const encrypted = decryptInput.value;
const publicKey = publicKeyArmored !== '' ? await openpgp.readKey({ armoredKey: publicKeyArmored }) : undefined;
const privateKey = passphrase !== ''
? await openpgp.decryptKey({
privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
passphrase,
})
: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored });
const message = await openpgp.readMessage({
armoredMessage: encrypted, // parse armored message
});
const { data: decrypted, signatures } = await openpgp.decrypt({
message,
verificationKeys: publicKey, // optional
decryptionKeys: privateKey,
});
if (signatures.length > 0) {
try {
await signatures[0].verified; // throws on invalid signature
}
catch (e: any) {
return {
decryptedText: decrypted,
signatureError: `Signature could not be verified: ${e.toString()}`,
};
}
}
return {
decryptedText: decrypted,
signatureError: '',
};
}, {
defaultValue: { decryptedText: '', signatureError: '' },
defaultErrorMessage: 'Unable to encrypt your text',
});
</script>
<template>
<div>
<c-card title="Encrypt">
<div>
<c-input-text
v-model:value="cryptInput"
label="Your text:"
placeholder="The string to encrypt"
rows="4"
multiline raw-text monospace autosize flex-1
/>
<div flex flex-1 flex-col gap-2>
<c-input-text
v-model:value="cryptPublicKey"
label="Target public key:"
placeholder="Target public key"
rows="5"
multiline raw-text monospace autosize flex-1
/>
<details>
<summary>Signing private key (optional)</summary>
<c-input-text
v-model:value="cryptPrivateKey"
label="Your private key:"
placeholder="The private key to use to sign message"
rows="5"
multiline raw-text monospace autosize flex-1
/>
<c-input-text
v-model:value="cryptPrivateKeyPassphrase"
label="Your private key password:" clearable raw-text
/>
</details>
</div>
</div>
<c-alert
v-if="cryptError && cryptPublicKey !== ''"
type="error" mt-12 title="Error while encrypting"
>
{{ cryptError }}
</c-alert>
<n-form-item label="Your text encrypted:" mt-3>
<TextareaCopyable
:value="cryptOutput || ''"
rows="3"
placeholder="Your string encrypted"
multiline monospace readonly autosize mt-5
/>
</n-form-item>
</c-card>
<c-card title="Decrypt">
<div>
<c-input-text
v-model:value="decryptInput"
label="Your PGP Message to decrypt:"
placeholder="The string to decrypt"
rows="4"
multiline raw-text monospace autosize flex-1
/>
<div flex flex-1 flex-col gap-2>
<c-input-text
v-model:value="decryptPrivateKey"
label="Your private key:"
placeholder="The private key to use to decrypt message"
rows="5"
multiline raw-text monospace autosize flex-1
/>
<c-input-text
v-model:value="decryptPrivateKeyPassphrase"
label="Your private key password:" clearable raw-text
/>
<details>
<summary>Signing public key (optional)</summary>
<c-input-text
v-model:value="decryptPublicKey"
label="Sender public key:"
placeholder="Sender public key"
rows="5"
multiline raw-text monospace autosize flex-1
/>
</details>
</div>
</div>
<c-alert v-if="decryptError && decryptPrivateKey !== ''" type="error" mt-3 title="Error while decrypting">
{{ decryptError }}
</c-alert>
<c-alert v-if="decryptOutput?.signatureError !== ''" type="error" mt-3 title="Signature verification">
{{ decryptOutput?.signatureError }}
</c-alert>
<n-form-item label="Your text decrypted:" mt-3>
<TextareaCopyable
:value="decryptOutput?.decryptedText || ''"
rows="3"
placeholder="Your string decrypted"
multiline monospace readonly autosize mt-5
/>
</n-form-item>
</c-card>
</div>
</template>