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
+2 -1
View File
@@ -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<T> {
this.cipher = cipher
}
public static async gen_keypair(extractable: boolean = false): Promise<[PrivKey, PubKey]> {
public static async gen_keypair(extractable: boolean = false): Promise<KeyPair> {
const keys = await window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
+2 -1
View File
@@ -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<KeyPair> {
let key = await window.crypto.subtle.generateKey(
{
name: "ECDSA",
+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")
})