import { describe, expect, it } from 'vitest'; import { decrypt, decryptIdCard, encrypt, hash } from './id-card-cipher.js'; describe('id-card-cipher', () => { it('加密后可解密回原文(roundtrip)', () => { const plain = '530102199001011231'; expect(decrypt(encrypt(plain))).toBe(plain); }); it('同一明文两次加密产生不同密文(随机 IV)', () => { const plain = '530102199001011231'; expect(encrypt(plain)).not.toBe(encrypt(plain)); }); it('密文为 base64(iv || authTag || ciphertext),长度稳定且不含明文', () => { const plain = '530102199001011231'; const enc = encrypt(plain); const buf = Buffer.from(enc, 'base64'); // 12 IV + 16 authTag + 18 ciphertext(AES-GCM 不增补长度) expect(buf.length).toBe(12 + 16 + 18); expect(enc).not.toContain(plain); }); it('hash 输出 64 位 hex 且稳定', () => { const plain = '530102199001011231'; const h = hash(plain); expect(h).toMatch(/^[0-9a-f]{64}$/); expect(hash(plain)).toBe(h); expect(hash('530102199001011232')).not.toBe(h); }); it('decryptIdCard 空值/非法值返回空串而不抛错', () => { expect(decryptIdCard('')).toBe(''); expect(decryptIdCard(null)).toBe(''); expect(decryptIdCard(undefined)).toBe(''); expect(decryptIdCard('not-base64!!!')).toBe(''); }); });