|
| 1 | +<script setup lang="ts"> |
| 2 | +import * as openpgp from 'openpgp'; |
| 3 | +import { computedCatchAsync } from '@/composable/computed/catchedComputed'; |
| 4 | +
|
| 5 | +const cryptInput = ref(''); |
| 6 | +const cryptPublicKey = ref(''); |
| 7 | +const cryptPrivateKey = ref(''); |
| 8 | +const cryptPrivateKeyPassphrase = ref(''); |
| 9 | +const [cryptOutput, cryptError] = computedCatchAsync(async () => { |
| 10 | + const publicKeyArmored = cryptPublicKey.value; |
| 11 | + const privateKeyArmored = cryptPrivateKey.value; |
| 12 | + const passphrase = cryptPrivateKeyPassphrase.value; |
| 13 | + const text = cryptInput.value; |
| 14 | +
|
| 15 | + const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored }); |
| 16 | +
|
| 17 | + const privateKey = privateKeyArmored !== '' |
| 18 | + ? (passphrase !== '' |
| 19 | + ? await openpgp.decryptKey({ |
| 20 | + privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }), |
| 21 | + passphrase, |
| 22 | + }) |
| 23 | + : await openpgp.readPrivateKey({ armoredKey: privateKeyArmored })) |
| 24 | + : undefined; |
| 25 | +
|
| 26 | + return await openpgp.encrypt({ |
| 27 | + message: await openpgp.createMessage({ text }), |
| 28 | + encryptionKeys: publicKey, |
| 29 | + signingKeys: privateKey, |
| 30 | + }); |
| 31 | +}, { |
| 32 | + defaultValue: '', |
| 33 | + defaultErrorMessage: 'Unable to encrypt your text', |
| 34 | +}); |
| 35 | +
|
| 36 | +const decryptInput = ref(''); |
| 37 | +const decryptPublicKey = ref(''); |
| 38 | +const decryptPrivateKey = ref(''); |
| 39 | +const decryptPrivateKeyPassphrase = ref(''); |
| 40 | +const [decryptOutput, decryptError] = computedCatchAsync(async () => { |
| 41 | + const publicKeyArmored = decryptPublicKey.value; |
| 42 | + const privateKeyArmored = decryptPrivateKey.value; |
| 43 | + const passphrase = decryptPrivateKeyPassphrase.value; |
| 44 | + const encrypted = decryptInput.value; |
| 45 | +
|
| 46 | + const publicKey = publicKeyArmored !== '' ? await openpgp.readKey({ armoredKey: publicKeyArmored }) : undefined; |
| 47 | +
|
| 48 | + const privateKey = passphrase !== '' |
| 49 | + ? await openpgp.decryptKey({ |
| 50 | + privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }), |
| 51 | + passphrase, |
| 52 | + }) |
| 53 | + : await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }); |
| 54 | +
|
| 55 | + const message = await openpgp.readMessage({ |
| 56 | + armoredMessage: encrypted, // parse armored message |
| 57 | + }); |
| 58 | + const { data: decrypted, signatures } = await openpgp.decrypt({ |
| 59 | + message, |
| 60 | + verificationKeys: publicKey, // optional |
| 61 | + decryptionKeys: privateKey, |
| 62 | + }); |
| 63 | + if (signatures.length > 0) { |
| 64 | + try { |
| 65 | + await signatures[0].verified; // throws on invalid signature |
| 66 | + } |
| 67 | + catch (e: any) { |
| 68 | + return { |
| 69 | + decryptedText: decrypted, |
| 70 | + signatureError: `Signature could not be verified: ${e.toString()}`, |
| 71 | + }; |
| 72 | + } |
| 73 | + } |
| 74 | + return { |
| 75 | + decryptedText: decrypted, |
| 76 | + signatureError: '', |
| 77 | + }; |
| 78 | +}, { |
| 79 | + defaultValue: { decryptedText: '', signatureError: '' }, |
| 80 | + defaultErrorMessage: 'Unable to encrypt your text', |
| 81 | +}); |
| 82 | +</script> |
| 83 | + |
| 84 | +<template> |
| 85 | + <div> |
| 86 | + <c-card title="Encrypt"> |
| 87 | + <div> |
| 88 | + <c-input-text |
| 89 | + v-model:value="cryptInput" |
| 90 | + label="Your text:" |
| 91 | + placeholder="The string to encrypt" |
| 92 | + rows="4" |
| 93 | + multiline raw-text monospace autosize flex-1 |
| 94 | + /> |
| 95 | + <div flex flex-1 flex-col gap-2> |
| 96 | + <c-input-text |
| 97 | + v-model:value="cryptPublicKey" |
| 98 | + label="Target public key:" |
| 99 | + placeholder="Target public key" |
| 100 | + rows="5" |
| 101 | + multiline raw-text monospace autosize flex-1 |
| 102 | + /> |
| 103 | + |
| 104 | + <details> |
| 105 | + <summary>Signing private key (optional)</summary> |
| 106 | + <c-input-text |
| 107 | + v-model:value="cryptPrivateKey" |
| 108 | + label="Your private key:" |
| 109 | + placeholder="The private key to use to sign message" |
| 110 | + rows="5" |
| 111 | + multiline raw-text monospace autosize flex-1 |
| 112 | + /> |
| 113 | + |
| 114 | + <c-input-text |
| 115 | + v-model:value="cryptPrivateKeyPassphrase" |
| 116 | + label="Your private key password:" clearable raw-text |
| 117 | + /> |
| 118 | + </details> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + |
| 122 | + <c-alert |
| 123 | + v-if="cryptError && cryptPublicKey !== ''" |
| 124 | + type="error" mt-12 title="Error while encrypting" |
| 125 | + > |
| 126 | + {{ cryptError }} |
| 127 | + </c-alert> |
| 128 | + |
| 129 | + <n-form-item label="Your text encrypted:" mt-3> |
| 130 | + <TextareaCopyable |
| 131 | + :value="cryptOutput || ''" |
| 132 | + rows="3" |
| 133 | + placeholder="Your string encrypted" |
| 134 | + multiline monospace readonly autosize mt-5 |
| 135 | + /> |
| 136 | + </n-form-item> |
| 137 | + </c-card> |
| 138 | + |
| 139 | + <c-card title="Decrypt"> |
| 140 | + <div> |
| 141 | + <c-input-text |
| 142 | + v-model:value="decryptInput" |
| 143 | + label="Your PGP Message to decrypt:" |
| 144 | + placeholder="The string to decrypt" |
| 145 | + rows="4" |
| 146 | + multiline raw-text monospace autosize flex-1 |
| 147 | + /> |
| 148 | + <div flex flex-1 flex-col gap-2> |
| 149 | + <c-input-text |
| 150 | + v-model:value="decryptPrivateKey" |
| 151 | + label="Your private key:" |
| 152 | + placeholder="The private key to use to decrypt message" |
| 153 | + rows="5" |
| 154 | + multiline raw-text monospace autosize flex-1 |
| 155 | + /> |
| 156 | + |
| 157 | + <c-input-text |
| 158 | + v-model:value="decryptPrivateKeyPassphrase" |
| 159 | + label="Your private key password:" clearable raw-text |
| 160 | + /> |
| 161 | + |
| 162 | + <details> |
| 163 | + <summary>Signing public key (optional)</summary> |
| 164 | + |
| 165 | + <c-input-text |
| 166 | + v-model:value="decryptPublicKey" |
| 167 | + label="Sender public key:" |
| 168 | + placeholder="Sender public key" |
| 169 | + rows="5" |
| 170 | + multiline raw-text monospace autosize flex-1 |
| 171 | + /> |
| 172 | + </details> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + |
| 176 | + <c-alert v-if="decryptError && decryptPrivateKey !== ''" type="error" mt-3 title="Error while decrypting"> |
| 177 | + {{ decryptError }} |
| 178 | + </c-alert> |
| 179 | + |
| 180 | + <c-alert v-if="decryptOutput?.signatureError !== ''" type="error" mt-3 title="Signature verification"> |
| 181 | + {{ decryptOutput?.signatureError }} |
| 182 | + </c-alert> |
| 183 | + |
| 184 | + <n-form-item label="Your text decrypted:" mt-3> |
| 185 | + <TextareaCopyable |
| 186 | + :value="decryptOutput?.decryptedText || ''" |
| 187 | + rows="3" |
| 188 | + placeholder="Your string decrypted" |
| 189 | + multiline monospace readonly autosize mt-5 |
| 190 | + /> |
| 191 | + </n-form-item> |
| 192 | + </c-card> |
| 193 | + </div> |
| 194 | +</template> |
0 commit comments