Files
weeklyreport/backend/utils/password.ts

35 lines
1.0 KiB
TypeScript

import * as crypto from 'crypto'
const SALT_ROUNDS = 10
/**
* 비밀번호 해시 생성 (bcrypt 대신 Node.js 내장 crypto 사용)
*/
export async function hashPassword(password: string): Promise<string> {
const salt = crypto.randomBytes(16).toString('hex')
const hash = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha512').toString('hex')
return `${salt}:${hash}`
}
/**
* 비밀번호 검증
*/
export async function verifyPassword(password: string, storedHash: string): Promise<boolean> {
const [salt, hash] = storedHash.split(':')
if (!salt || !hash) return false
const verifyHash = crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha512').toString('hex')
return hash === verifyHash
}
/**
* 임시 비밀번호 생성
*/
export function generateTempPassword(length: number = 12): string {
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789!@#$%'
let password = ''
for (let i = 0; i < length; i++) {
password += chars.charAt(Math.floor(Math.random() * chars.length))
}
return password
}