Files
libcrypto/test/signature.test.ts
T
2024-06-04 16:51:17 +02:00

74 lines
2.4 KiB
TypeScript

import {test, expect} from 'bun:test'
import {signature} from '../index'
async function base_case(keypair: signature.KeyPair) {
let data = new TextEncoder().encode("Message 123 !")
const sig = (await signature.sign(keypair[0], data)).expect("Should sign the message")
let verification = await signature.verify(keypair[1], data, sig)
expect(verification).toBe(true)
}
async function inverted(keypair: signature.KeyPair) {
let data = new TextEncoder().encode("Message 123 !")
const sig = (await signature.sign(keypair[1], data)).expect("Should sign the message")
let verification = await signature.verify(keypair[0], data, sig)
expect(verification).toBe(false)
}
test('base case', async () => {
let keypair = await signature.gen_keypair()
return base_case(keypair)
})
test('base case with password', async () => {
let keypair = await signature.from_password("Yeet", new Uint8Array(16))
return base_case(keypair)
})
test('inverted with password', async () => {
let keypair = await signature.from_password("Yeet", new Uint8Array(16))
return inverted(keypair)
})
test('inverted keys', async () => {
let keypair = await signature.gen_keypair()
return inverted(keypair)
})
test('tampered message', async () => {
let keypair = await signature.gen_keypair()
let data1 = new TextEncoder().encode("Message 123 !")
let data2 = new TextEncoder().encode("Message 321 !")
expect(data1).not.toEqual(data2)
const sig = (await signature.sign(keypair[0], data1)).expect("Should sign the message")
let verification = await signature.verify(keypair[1], data2, sig)
expect(verification).toBe(false)
})
test('different keypair', async () => {
const keypair = await signature.gen_keypair()
const keypair2 = await signature.gen_keypair()
let data = new TextEncoder().encode("Message 123 !")
const sig = (await signature.sign(keypair[0], data)).expect("Should sign the message")
let verification = await signature.verify(keypair2[1], data, sig)
expect(verification).toBe(false)
})
test('tampered signature', async () => {
let keypair = await signature.gen_keypair()
let data = new TextEncoder().encode("Message 123 !")
const sig = (await signature.sign(keypair[0], data)).expect("Should sign the message")
sig[0] ^= 1
let verification = await signature.verify(keypair[1], data, sig)
expect(verification).toBe(false)
})