From 3d2fe5f517a916bfff1bf7589b7933fec20740dc Mon Sep 17 00:00:00 2001 From: Pascal Perrenoud Date: Tue, 14 May 2024 22:23:58 +0200 Subject: [PATCH] Add KeyPair type for asym primitives --- src/boxes/asymmetric.ts | 3 ++- src/signature.ts | 3 ++- test/boxes/asymmetric.test.ts | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/boxes/asymmetric.ts b/src/boxes/asymmetric.ts index 146610e..8a7ec0d 100644 --- a/src/boxes/asymmetric.ts +++ b/src/boxes/asymmetric.ts @@ -3,6 +3,7 @@ import * as misc from 'misc' export type PubKey = CryptoKey export type PrivKey = CryptoKey +export type KeyPair = [PrivKey, PubKey] const algorithm: RsaOaepParams = {name: "RSA-OAEP"} @@ -14,7 +15,7 @@ export class PrivateBox { this.cipher = cipher } - public static async gen_keypair(extractable: boolean = false): Promise<[PrivKey, PubKey]> { + public static async gen_keypair(extractable: boolean = false): Promise { const keys = await window.crypto.subtle.generateKey( { name: "RSA-OAEP", diff --git a/src/signature.ts b/src/signature.ts index 1168462..e71e864 100644 --- a/src/signature.ts +++ b/src/signature.ts @@ -2,6 +2,7 @@ import {Result} from 'result' export type PrivKey = CryptoKey export type PubKey = CryptoKey +export type KeyPair = [PrivKey, PubKey] const algorithm: EcdsaParams = { name: "ECDSA", @@ -13,7 +14,7 @@ const algorithm: EcdsaParams = { * @param extractable if the keys must be extractable or not * @return [privkey, pubkey] keys */ -export async function gen_keypair(extractable: boolean = false) : Promise<[PrivKey, PubKey]> { +export async function gen_keypair(extractable: boolean = false) : Promise { let key = await window.crypto.subtle.generateKey( { name: "ECDSA", diff --git a/test/boxes/asymmetric.test.ts b/test/boxes/asymmetric.test.ts index 6124cb8..e4ff763 100644 --- a/test/boxes/asymmetric.test.ts +++ b/test/boxes/asymmetric.test.ts @@ -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(pub, data)).expect("Should encrypt the data") + const box = (await PrivateBox.encrypt(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(pub, data)).expect("Should encrypt the data") + const box = (await PrivateBox.encrypt(pub, data)).expect("Should encrypt the data") const str = box.toString() - const box2 = asymmetric.PrivateBox.fromString(str).expect("Should parse the string") + const box2 = PrivateBox.fromString(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(pub, data)).expect("Should encrypt the data") + const box = (await PrivateBox.encrypt(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") })