Add KeyPair type for asym primitives

This commit is contained in:
2024-05-14 22:23:58 +02:00
parent f30516bd2c
commit 3d2fe5f517
3 changed files with 14 additions and 12 deletions
+10 -10
View File
@@ -1,13 +1,13 @@
import {beforeAll, expect, test} from 'bun:test'
import * as asymmetric from 'boxes/asymmetric'
import {type KeyPair, PrivateBox} from 'boxes/asymmetric'
let k1!: [asymmetric.PrivKey, asymmetric.PubKey];
let k2!: [asymmetric.PrivKey, asymmetric.PubKey];
let k1!: KeyPair;
let k2!: KeyPair;
beforeAll(async () => {
k1 = await asymmetric.PrivateBox.gen_keypair(false)
k2 = await asymmetric.PrivateBox.gen_keypair(true)
k1 = await PrivateBox.gen_keypair(false)
k2 = await PrivateBox.gen_keypair(true)
expect(k1[0].extractable).toBe(false)
expect(k1[1].extractable).toBe(true)
@@ -19,7 +19,7 @@ test('base case', async () => {
const [priv, pub] = k1
const data = new Uint8Array([1, 2, 3, 4, 5])
const box = (await asymmetric.PrivateBox.encrypt<Uint8Array>(pub, data)).expect("Should encrypt the data")
const box = (await PrivateBox.encrypt<Uint8Array>(pub, data)).expect("Should encrypt the data")
const result = (await box.decrypt(priv)).expect("Should decrypt the data")
expect(result).toEqual(data)
@@ -29,9 +29,9 @@ test('toString and fromString are inverses', async () => {
const [priv, pub] = k1
const data = new Uint8Array([1, 2, 3, 4, 5])
const box = (await asymmetric.PrivateBox.encrypt<Uint8Array>(pub, data)).expect("Should encrypt the data")
const box = (await PrivateBox.encrypt<Uint8Array>(pub, data)).expect("Should encrypt the data")
const str = box.toString()
const box2 = asymmetric.PrivateBox.fromString<Uint8Array>(str).expect("Should parse the string")
const box2 = PrivateBox.fromString<Uint8Array>(str).expect("Should parse the string")
expect(box).toEqual(box2)
const plain = (await box2.decrypt(priv)).expect("Should decrypt the data")
@@ -42,7 +42,7 @@ test('Tampered cipher fails', async () => {
const [priv, pub] = k1
const data = new Uint8Array([1, 2, 3, 4, 5])
const box = (await asymmetric.PrivateBox.encrypt<Uint8Array>(pub, data)).expect("Should encrypt the data")
const box = (await PrivateBox.encrypt<Uint8Array>(pub, data)).expect("Should encrypt the data")
box.cipher[0] += 1
;(await box.decrypt(priv)).expect_err("Should fail to decrypt the data")
})
@@ -52,6 +52,6 @@ test('Wrong pubkey should fail', async () => {
const [priv, _pub] = k2
const data = new Uint8Array([1, 2, 3, 4, 5])
const box = (await asymmetric.PrivateBox.encrypt(pub, data)).expect("Should encrypt the data")
const box = (await PrivateBox.encrypt(pub, data)).expect("Should encrypt the data")
;(await box.decrypt(priv)).expect_err("Should fail to decrypt the data")
})