- Add arrayToBase64/base64ToArray conversion functions - Add deriveKey for PBKDF2 key derivation - Add encrypt/decrypt using AES-GCM - Add generateMasterKey for random key generation - Update setup.ts to use real Web Crypto API instead of mock - Add comprehensive unit tests for all crypto functions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { arrayToBase64, base64ToArray, deriveKey, encrypt, decrypt } from '../../src/lib/crypto-utils';
|
|
|
|
describe('crypto-utils', () => {
|
|
describe('arrayToBase64', () => {
|
|
it('should convert Uint8Array to base64 string', () => {
|
|
const arr = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
|
|
const result = arrayToBase64(arr);
|
|
expect(result).toBe('SGVsbG8=');
|
|
});
|
|
|
|
it('should handle empty array', () => {
|
|
const arr = new Uint8Array([]);
|
|
const result = arrayToBase64(arr);
|
|
expect(result).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('base64ToArray', () => {
|
|
it('should convert base64 string to Uint8Array', () => {
|
|
const base64 = 'SGVsbG8=';
|
|
const result = base64ToArray(base64);
|
|
expect(result).toEqual(new Uint8Array([72, 101, 108, 108, 111]));
|
|
});
|
|
|
|
it('should handle empty string', () => {
|
|
const result = base64ToArray('');
|
|
expect(result).toEqual(new Uint8Array([]));
|
|
});
|
|
});
|
|
|
|
describe('encrypt and decrypt', () => {
|
|
it('should encrypt and decrypt text correctly', async () => {
|
|
// Use real Web Crypto API (setup.ts polyfills this)
|
|
const key = await crypto.subtle.generateKey(
|
|
{ name: 'AES-GCM', length: 256 },
|
|
true,
|
|
['encrypt', 'decrypt']
|
|
);
|
|
|
|
const plaintext = 'secret message';
|
|
const encrypted = await encrypt(plaintext, key);
|
|
|
|
expect(encrypted.iv).toBeDefined();
|
|
expect(encrypted.data).toBeDefined();
|
|
expect(encrypted.data).not.toBe(plaintext);
|
|
|
|
const decrypted = await decrypt(encrypted, key);
|
|
expect(decrypted).toBe(plaintext);
|
|
});
|
|
});
|
|
|
|
describe('deriveKey', () => {
|
|
it('should derive a key from a master key', async () => {
|
|
const masterKey = 'test-master-key-123';
|
|
const key = await deriveKey(masterKey);
|
|
|
|
expect(key).toBeDefined();
|
|
expect(key.type).toBe('secret');
|
|
expect(key.algorithm).toHaveProperty('name', 'AES-GCM');
|
|
});
|
|
|
|
it('should derive same key from same master key and salt', async () => {
|
|
const masterKey = 'test-master-key-123';
|
|
const salt = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
|
|
const key1 = await deriveKey(masterKey, salt);
|
|
const key2 = await deriveKey(masterKey, salt);
|
|
|
|
// Keys should be usable for encryption/decryption
|
|
const plaintext = 'test data';
|
|
const encrypted = await encrypt(plaintext, key1);
|
|
const decrypted = await decrypt(encrypted, key2);
|
|
|
|
expect(decrypted).toBe(plaintext);
|
|
});
|
|
});
|
|
});
|