mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-18 23:45:26 +02:00
added tests for cipher functions; fixed ciphers
This commit is contained in:
parent
e942740004
commit
22f83642a8
27
__tests__/ciphers.test.ts
Normal file
27
__tests__/ciphers.test.ts
Normal 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)
|
||||
})
|
||||
})
|
@ -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")
|
||||
})
|
||||
})
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user