feat(crypto): add AES-GCM encryption utilities
- 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>
This commit is contained in:
78
desktop/tests/lib/crypto-utils.test.ts
Normal file
78
desktop/tests/lib/crypto-utils.test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -56,21 +56,11 @@ Object.defineProperty(global, 'localStorage', {
|
||||
configurable: true,
|
||||
});
|
||||
|
||||
// Mock crypto.subtle for tests (already set via webcrypto above, but ensure subtle mock works)
|
||||
const subtleMock = {
|
||||
encrypt: vi.fn(),
|
||||
decrypt: vi.fn(),
|
||||
generateKey: vi.fn(),
|
||||
deriveKey: vi.fn(),
|
||||
importKey: vi.fn(),
|
||||
exportKey: vi.fn(),
|
||||
digest: vi.fn(),
|
||||
sign: vi.fn(),
|
||||
verify: vi.fn(),
|
||||
};
|
||||
|
||||
// Override subtle if needed for specific test scenarios
|
||||
Object.defineProperty(global.crypto, 'subtle', {
|
||||
value: subtleMock,
|
||||
configurable: true,
|
||||
});
|
||||
// Note: We intentionally do NOT mock crypto.subtle here.
|
||||
// Tests that need real crypto operations (like crypto-utils) will use the real Web Crypto API.
|
||||
// Tests that need to mock crypto operations should do so in their own test files.
|
||||
//
|
||||
// If you need to mock crypto.subtle for specific tests, use:
|
||||
// vi.spyOn(crypto.subtle, 'encrypt').mockImplementation(...)
|
||||
// Or restore after mocking:
|
||||
// afterAll(() => vi.restoreAllMocks())
|
||||
|
||||
Reference in New Issue
Block a user