added tests for cipher functions; fixed ciphers

This commit is contained in:
Myzel394 2022-12-28 19:46:00 +01:00
parent e942740004
commit 22f83642a8
5 changed files with 36 additions and 11 deletions

27
__tests__/ciphers.test.ts Normal file
View File

@ -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)
})
})

View File

@ -8,6 +8,6 @@ describe("getEncryptionPassword", () => {
it("returns a string", async () => { it("returns a string", async () => {
const result = await getEncryptionPassword("test@kleckrelay.example", "password", "salt") const result = await getEncryptionPassword("test@kleckrelay.example", "password", "salt")
expect(typeof result).toBe("string") expect(typeof result).toBe("object")
}) })
}) })

View File

@ -1,10 +1,7 @@
import Crypto from "crypto-js" import {AES, enc} from "crypto-js"
export default function decryptString( export default function decryptString(ciphertext: string, secret: CryptoJS.lib.WordArray): string {
ciphertext: string, const bytes = AES.decrypt(ciphertext, secret.toString())
password: string,
): string {
const bytes = Crypto.AES.decrypt(ciphertext, password)
return bytes.toString(Crypto.enc.Utf8) return bytes.toString(enc.Utf8)
} }

View File

@ -1,5 +1,5 @@
import Crypto from "crypto-js" import {AES} from "crypto-js"
export default function encryptString(value: string, key: CryptoKey): string { export default function encryptString(value: string, secret: CryptoJS.lib.WordArray): string {
return Crypto.AES.encrypt(value).toString() return AES.encrypt(value, secret.toString()).toString()
} }

View File

@ -12,5 +12,6 @@ export default async function getEncryptionPassword(
return CryptoJS.PBKDF2(cryptoPassword, cryptoSalt, { return CryptoJS.PBKDF2(cryptoPassword, cryptoSalt, {
keySize: 512 / 32, keySize: 512 / 32,
iterations: process.env.NODE_ENV === "production" ? 100_394 : 1,
}) })
} }