From 4a3adaa34711c6f7dcbdc918e5f4e157a1de09a3 Mon Sep 17 00:00:00 2001 From: Tuan Dang Date: Sun, 19 Feb 2023 10:54:54 +0700 Subject: [PATCH] Begin in-memory privat key storage --- frontend/src/helpers/key.ts | 4 ++- frontend/src/services/KeyService.ts | 53 ++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/frontend/src/helpers/key.ts b/frontend/src/helpers/key.ts index 60a117927..05562628d 100644 --- a/frontend/src/helpers/key.ts +++ b/frontend/src/helpers/key.ts @@ -82,4 +82,6 @@ let privateKey; return privateKey; } -export { decryptPrivateKeyHelper }; \ No newline at end of file +export { + decryptPrivateKeyHelper +}; \ No newline at end of file diff --git a/frontend/src/services/KeyService.ts b/frontend/src/services/KeyService.ts index 4d3f950e0..0213cbc08 100644 --- a/frontend/src/services/KeyService.ts +++ b/frontend/src/services/KeyService.ts @@ -1,9 +1,17 @@ -import { decryptPrivateKeyHelper } from '@app/helpers/key'; +import { + decryptAssymmetric, + encryptAssymmetric} from '@app/components/utilities/cryptography/crypto'; +import { + decryptPrivateKeyHelper +} from '@app/helpers/key'; /** * Class to handle key actions + * TODO: in future, all private key-related encryption operations + * must pass through this class */ class KeyService { + private static privateKey: string = ''; /** Return the user's decrypted private key * @param {Object} obj @@ -51,6 +59,49 @@ class KeyService { protectedKeyTag }); } + + /** + * Return [plaintext] encrypted by the user's private key + * @param {Object} obj + * @param {String} obj.plaintext - plaintext to encrypt + */ + static encryptWithPrivateKey({ + plaintext, + publicKey, + }: { + plaintext: string; + publicKey: string; + }) { + return encryptAssymmetric({ + plaintext, + publicKey, + privateKey: KeyService.privateKey + }); + } + + /** + * Return [ciphertext] decrypted by the user's private key + * @param {Object} obj + * @param {String} obj.ciphertext - ciphertext to decrypt + * @param {String} obj.ciphertext - iv of ciphertext + * @param {String} obj.ciphertext - tag of ciphertext + */ + static decryptWithPrivateKey({ + ciphertext, + nonce, + publicKey + }: { + ciphertext: string; + nonce: string; + publicKey: string; + }) { + return decryptAssymmetric({ + ciphertext, + nonce, + publicKey, + privateKey: KeyService.privateKey + }); + } } export default KeyService; \ No newline at end of file