Files
wzdj/old/app/admin/screen/_lib/utils/crypto.ts
2026-04-20 14:56:28 +08:00

44 lines
1.0 KiB
TypeScript

/**
* Simple MD5 hash implementation for password hashing
* Note: In production, use bcrypt or similar for better security
*/
/**
* Create MD5 hash of a string
*/
export function hashPassword(password: string): string {
// Simple hash function for demo purposes
// In production, use bcrypt or similar
let hash = 0
if (password.length === 0) return hash.toString()
for (let i = 0; i < password.length; i++) {
const char = password.charCodeAt(i)
hash = (hash << 5) - hash + char
hash = hash & hash // Convert to 32-bit integer
}
return Math.abs(hash).toString(16)
}
/**
* Verify password against hash
*/
export function verifyPassword(password: string, hash: string): boolean {
return hashPassword(password) === hash
}
/**
* Generate MD5 hash (legacy support)
*/
export function md5Hash(input: string): string {
return hashPassword(input)
}
/**
* Verify MD5 hash (legacy support)
*/
export function verifyMd5Hash(input: string, hash: string): boolean {
return verifyPassword(input, hash)
}