Add examples

This commit is contained in:
2024-06-06 01:51:04 +02:00
parent 44094a31f1
commit c05f0536e2
5 changed files with 78 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
import {expect} from 'bun:test'
import {PrivateWrap} from '../index'
const key_wraps = await PrivateWrap.gen_keypair()
const key_wrapped = await PrivateWrap.gen_keypair()
const cipher = await PrivateWrap.encrypt(key_wrapped.privateKey, key_wraps.publicKey)
const box = await cipher.toString()
const unbox = await PrivateWrap.fromString(box)
const plain = await unbox.decrypt(key_wraps.privateKey)
expect(plain).toEqual(key_wrapped.privateKey)
console.log("ecdh-wraps-ecdh OK")
+14
View File
@@ -0,0 +1,14 @@
import {expect} from 'bun:test'
import {PrivateWrap, SecretBox} from '../index'
const k_wraps = await PrivateWrap.gen_keypair()
const k_wrapped = await SecretBox.gen_key()
const cipher = await PrivateWrap.encrypt(k_wrapped, k_wraps.publicKey)
const box = await cipher.toString()
const unbox = await PrivateWrap.fromString(box)
const plain = await unbox.decrypt(k_wraps.privateKey) as CryptoKey
expect(plain).toEqual(k_wrapped)
console.log("ecdh-wraps-key OK")
+14
View File
@@ -0,0 +1,14 @@
import {expect} from 'bun:test'
import {SecretWrap, PrivateWrap} from '../index'
const key_wraps = await SecretWrap.gen_key()
const key_wrapped = await PrivateWrap.gen_keypair()
const cipher = await SecretWrap.encrypt(key_wrapped.privateKey, key_wraps)
const box = cipher.toString()
const unbox = SecretWrap.fromString(box)
const plain = await unbox.decrypt(key_wraps)
expect(plain).toEqual(key_wrapped.privateKey)
console.log("key-wraps-ecdh OK")
+17
View File
@@ -0,0 +1,17 @@
import {expect} from 'bun:test'
import {hkdf, pbkdf, Usage} from '../src/kdf'
import {get_pubkey, sign, verify} from '../src/signature'
const salt = new Uint8Array(32)
const pwd = "test"
const message = new TextEncoder().encode("Yeet")
const kd = await pbkdf(salt, pwd)
const privk = await hkdf(kd, Usage.sign) as Uint8Array
const pubk = await get_pubkey(privk)
const sig = await sign(message, privk)
const verification = await verify(message, pubk, sig)
expect(verification).toBeTrue()
console.log("pwd-to-signing OK")
+19
View File
@@ -0,0 +1,19 @@
import {expect} from 'bun:test'
import {hkdf, pbkdf, Usage} from '../src/kdf'
import {PrivateWrap, SecretWrap} from '../index'
const salt = new Uint8Array(32)
const pwd = "test"
const keypair = await PrivateWrap.gen_keypair()
const kd = await pbkdf(salt, pwd)
const k = await hkdf(kd, Usage.wrap) as CryptoKey
const cipher = await SecretWrap.encrypt(keypair.privateKey, k)
const box = cipher.toString()
const unbox = SecretWrap.fromString(box)
const plain = await unbox.decrypt(k)
expect(plain).toEqual(keypair.privateKey)
console.log("pwd-wraps-ecdh OK")