From 22f83642a8e1ed42236f592b61a1eee544f3dd0d Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 28 Dec 2022 19:46:00 +0100 Subject: [PATCH] added tests for cipher functions; fixed ciphers --- __tests__/ciphers.test.ts | 27 +++++++++++++++++++++++ __tests__/get-encryption-password.test.ts | 2 +- src/utils/decrypt-string.ts | 11 ++++----- src/utils/encrypt-string.ts | 6 ++--- src/utils/get-encryption-password.ts | 1 + 5 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 __tests__/ciphers.test.ts diff --git a/__tests__/ciphers.test.ts b/__tests__/ciphers.test.ts new file mode 100644 index 0000000..5a81824 --- /dev/null +++ b/__tests__/ciphers.test.ts @@ -0,0 +1,27 @@ +import {decryptString, encryptString} from "../src/utils" +import getEncryptionPassword from "../src/utils/get-encryption-password" + +describe("ciphers", () => { + const email = "test@kleckrelay.example" + const password = "password" + const salt = "salt" + + it("encryption work", async () => { + const secret = await getEncryptionPassword(email, password, salt) + + const encrypted = encryptString("test", secret) + + expect(typeof encrypted).toBe("string") + }) + + it("encryption and decryption work", async () => { + const secret = await getEncryptionPassword(email, password, salt) + + const message = "test" + const encrypted = encryptString(message, secret) + + const decrypted = decryptString(encrypted, secret) + + expect(decrypted).toBe(message) + }) +}) diff --git a/__tests__/get-encryption-password.test.ts b/__tests__/get-encryption-password.test.ts index 0ad9d7c..36f09a0 100644 --- a/__tests__/get-encryption-password.test.ts +++ b/__tests__/get-encryption-password.test.ts @@ -8,6 +8,6 @@ describe("getEncryptionPassword", () => { it("returns a string", async () => { const result = await getEncryptionPassword("test@kleckrelay.example", "password", "salt") - expect(typeof result).toBe("string") + expect(typeof result).toBe("object") }) }) diff --git a/src/utils/decrypt-string.ts b/src/utils/decrypt-string.ts index 83ea00d..4a4ba70 100644 --- a/src/utils/decrypt-string.ts +++ b/src/utils/decrypt-string.ts @@ -1,10 +1,7 @@ -import Crypto from "crypto-js" +import {AES, enc} from "crypto-js" -export default function decryptString( - ciphertext: string, - password: string, -): string { - const bytes = Crypto.AES.decrypt(ciphertext, password) +export default function decryptString(ciphertext: string, secret: CryptoJS.lib.WordArray): string { + const bytes = AES.decrypt(ciphertext, secret.toString()) - return bytes.toString(Crypto.enc.Utf8) + return bytes.toString(enc.Utf8) } diff --git a/src/utils/encrypt-string.ts b/src/utils/encrypt-string.ts index 86958a5..bbb9c82 100644 --- a/src/utils/encrypt-string.ts +++ b/src/utils/encrypt-string.ts @@ -1,5 +1,5 @@ -import Crypto from "crypto-js" +import {AES} from "crypto-js" -export default function encryptString(value: string, key: CryptoKey): string { - return Crypto.AES.encrypt(value).toString() +export default function encryptString(value: string, secret: CryptoJS.lib.WordArray): string { + return AES.encrypt(value, secret.toString()).toString() } diff --git a/src/utils/get-encryption-password.ts b/src/utils/get-encryption-password.ts index a44e69f..01a82aa 100644 --- a/src/utils/get-encryption-password.ts +++ b/src/utils/get-encryption-password.ts @@ -12,5 +12,6 @@ export default async function getEncryptionPassword( return CryptoJS.PBKDF2(cryptoPassword, cryptoSalt, { keySize: 512 / 32, + iterations: process.env.NODE_ENV === "production" ? 100_394 : 1, }) }