Files
ssw-mvp/dacli/tests/unit/crypto.test.ts
T
admin 4d096dd97e chore(dacli): 提交 dacli CLI 工具源码并过滤敏感/噪声文件
- dacli/ 整目录被 .gitignore 忽略,本次 force-add 其业务源码(766 文件)
- 排除 node_modules/.env(含 PGPASSWORD 等敏感信息)/.DS_Store/__pycache__/*.pyc
- 新增 dacli/.gitignore 长期管理内部噪声,新增文件仍需 git add -f
- 根 .gitignore 补充 .cache/ 与诊断脚本忽略,并加 dacli 段说明
2026-08-06 19:02:39 +08:00

47 lines
1.4 KiB
TypeScript

/**
* 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 字符
})
})