/** * crypto.test.ts — 加密 1:1 一致性验证 * * 关键: '123456' + 默认密钥 'EY8WePvjM5GGwQzn' → 96 字符 hex */ import { describe, it, expect, beforeAll } from 'vitest' import { encryptByMd5, encryptPassword, isEncrypted } from '../../src/core/crypto.ts' describe('crypto', () => { beforeAll(() => { // 强制用固定 key, 避免 .env 漂移 process.env.VITE_CIPHER_KEY = 'EY8WePvjM5GGwQzn' }) it('encryptByMd5 输出 32 字符 hex', () => { const r = encryptByMd5('123456') expect(r).toMatch(/^[0-9a-f]{32}$/) expect(r).toBe('e10adc3949ba59abbe56e057f20f883e') // 知名 md5 }) it('encryptPassword 输出 96 字符 hex (与 web cipher.ts 一致)', () => { const r = encryptPassword('123456') expect(r).toMatch(/^[0-9a-f]+$/) expect(r).toHaveLength(96) }) it('encryptPassword 同输入同输出 (ECB 无 IV)', () => { const a = encryptPassword('123456') const b = encryptPassword('123456') expect(a).toBe(b) }) it('encryptPassword 不同输入不同输出', () => { const a = encryptPassword('123456') const b = encryptPassword('abcdef') expect(a).not.toBe(b) }) it('isEncrypted 判定', () => { expect(isEncrypted('123456')).toBe(false) expect(isEncrypted(encryptPassword('123456'))).toBe(true) expect(isEncrypted('')).toBe(false) expect(isEncrypted(null)).toBe(false) expect(isEncrypted('z'.repeat(96))).toBe(false) // 'z' 不是 hex 字符 }) })