本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的 dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。 - 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12) - 历史未带 ssw main 整体历史(避免大仓污染) - 此为新仓基线 commit,未来以本仓历史为主
47 lines
1.4 KiB
TypeScript
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 字符
|
|
})
|
|
})
|