import { AesUtil } from '../utils'; import CryptoJS from 'crypto-js'; import { describe, expect, it } from 'vitest'; describe('test crypto', async () => { it('should encrypt', async () => { const iv = CryptoJS.lib.WordArray.random(128 / 8).toString(CryptoJS.enc.Hex); const key = 'Applica'; const salt = CryptoJS.lib.WordArray.random(128 / 8).toString(CryptoJS.enc.Hex); const aesUtil = new AesUtil(128, 1000); const ciphertext = aesUtil.encrypt(salt, iv, key, 'Applica Software Guru'); const aesPassword = iv + '::' + salt + '::' + ciphertext; const password = btoa(aesPassword); expect(password).toBeDefined(); }); it('should encrypt and decrypt symmetrically (simulating server behavior)', async () => { const secret = 'test-secret-key-12345'; const plainText = '1234'; // PIN code const aesUtil = new AesUtil(128, 1000); // Client side: encrypt const iv = CryptoJS.lib.WordArray.random(128 / 8).toString(CryptoJS.enc.Hex); const salt = CryptoJS.lib.WordArray.random(128 / 8).toString(CryptoJS.enc.Hex); const ciphertext = aesUtil.encrypt(salt, iv, secret, plainText); // Build the payload exactly like pinLogin does const aesPassword = iv + '::' + salt + '::' + ciphertext; const encodedPayload = btoa(aesPassword); // Server side: decode and decrypt (simulating Java Device.decrypt) const decodedPayload = atob(encodedPayload); const parts = decodedPayload.split('::'); expect(parts.length).toBe(3); const [ivDecoded, saltDecoded, ciphertextDecoded] = parts; const decrypted = aesUtil.decrypt(saltDecoded, ivDecoded, secret, ciphertextDecoded); // Verify the decrypted text matches the original expect(decrypted).toBe(plainText); }); it('should test key derivation with known values', async () => { // Test that PBKDF2 key derivation is working correctly const aesUtil = new AesUtil(128, 1000); const passPhrase = 'testPassword'; const saltHex = '0123456789abcdef0123456789abcdef'; const key = aesUtil.generateKey(saltHex, passPhrase); const keyHex = key.toString(CryptoJS.enc.Hex); console.log('=== PBKDF2 Key Derivation Test ==='); console.log('Pass phrase:', passPhrase); console.log('Salt (hex):', saltHex); console.log('Key size:', 128 / 32, 'words (128 bits)'); console.log('Iterations:', 1000); console.log('Derived key (hex):', keyHex); console.log('Key length (hex chars):', keyHex.length, '(should be 32 for 128 bits)'); console.log('=== END Test ==='); // Verify key is 128 bits (32 hex characters) expect(keyHex.length).toBe(32); }); });