35 lines
818 B
TypeScript
35 lines
818 B
TypeScript
import {beforeAll, expect, test} from 'bun:test'
|
|
import {PwdBox} from '..'
|
|
|
|
let k1!: string
|
|
let k2!: string
|
|
let message!: Uint8Array
|
|
|
|
beforeAll(async () => {
|
|
k1 = 'abc'
|
|
k2 = 'def'
|
|
message = new TextEncoder().encode('Salut ! ça va ?')
|
|
})
|
|
|
|
test('base case', async () => {
|
|
const box = await PwdBox.encrypt(message, k1)
|
|
const unboxed = await box.decrypt(k1)
|
|
expect(unboxed).toEqual(message)
|
|
})
|
|
test("Different key can't decrypt", async () => {
|
|
const box = await PwdBox.encrypt(message, k1)
|
|
const unboxed = await box.decrypt(k2)
|
|
expect(unboxed).toBeNull()
|
|
})
|
|
|
|
test('serialization', async () => {
|
|
const message = new Uint8Array(8)
|
|
const box = await PwdBox.encrypt(message, k1)
|
|
|
|
const ser = box.toString()
|
|
const de = PwdBox.fromString(ser)
|
|
expect(de).not.toBeNull()
|
|
|
|
expect(de).toEqual(box)
|
|
})
|