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:
iven
2026-03-21 17:05:15 +08:00
parent 47a84f52a2
commit f070d9151e
3 changed files with 197 additions and 18 deletions

View File

@@ -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())