Files
libcrypto/test/secret-box.test.ts
T
2024-09-09 16:10:27 +02:00

40 lines
997 B
TypeScript

import {beforeAll, expect, test} from 'bun:test'
import {SecretBox} from '..'
let k1!: CryptoKey;
let k2!: CryptoKey;
let message!: Uint8Array;
beforeAll(async () => {
k1 = await SecretBox.gen(false)
k2 = await SecretBox.gen(true)
message = new TextEncoder().encode("Salut ! ça va ?")
})
test('base case', async () => {
const box = await SecretBox.encrypt(message, k1)
const unboxed = await box.decrypt(k1)
expect(unboxed).toEqual(message)
})
test("Different key can't decrypt", async () => {
const box = await SecretBox.encrypt(message, k1)
const unboxed = await box.decrypt(k2)
expect(unboxed).toBeNull()
})
test('Key generation', async () => {
expect(k1.extractable).toBeFalse()
expect(k2.extractable).toBeTrue()
})
test('serialization', async () => {
const message = new Uint8Array(8)
const box = await SecretBox.encrypt(message, k1)
const ser = box.toString()
const de = SecretBox.fromString(ser)
expect(de).not.toBeNull()
expect(de).toEqual(box)
})