Import packages/crypto/tests/crypto.test.ts

This commit is contained in:
QQ 2026-11-14 14:45:12 +08:00
parent e359a57888
commit c64d5c9604
1 changed files with 17 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest'
import { decryptFrame, deriveSharedKey, encryptFrame, generateKeyPair, identityFingerprint } from '../src/index.js'
describe('secure channel primitives', () => {
it('derives matching shared keys and decrypts a frame', () => {
const host = generateKeyPair()
const client = generateKeyPair()
const hostKey = deriveSharedKey(host.privateKey, client.publicKey)
const clientKey = deriveSharedKey(client.privateKey, host.publicKey)
expect(Buffer.from(hostKey).equals(Buffer.from(clientKey))).toBe(true)
const plaintext = new TextEncoder().encode('hello remote')
const frame = encryptFrame(hostKey, plaintext)
expect(new TextDecoder().decode(decryptFrame(clientKey, frame))).toBe('hello remote')
expect(identityFingerprint(host.publicKey)).toMatch(/^[A-F0-9]{12}$/)
})
})