비밀번호를 암호화할 때, 일단 암호화 되면 복호화가 어렵기 때문에 단방향 암호화 기법을 사용한다.
그래서 비밀번호 찾기를 하면 임시 비밀번호를 발급해주거나 다시 설정하게 하는 경우가 대다수이다.
key), 그 출력값 = 해시(hash)
-> John Smith와 Sandra Dee 키들 사이에 충돌이 발생


npm install bcrypt
// HashEncryptionUtil
import bcrypt from "bcrypt";
import { BadRequest } from "../errors/BadRequest";
import { InternalServerError } from "../errors/InternalServerError";
export class HashEncryptionUtil {
private saltRounds: number;
constructor(saltRounds: number) {
this.saltRounds = saltRounds;
}
// 비밀번호 암호화
public encryptPassword = async (password: string) => {
try {
const salt = await bcrypt.genSalt(this.saltRounds); // salt
const hashedPwd = await bcrypt.hash(password, salt); // hash function
return hashedPwd;
} catch (error) {
console.log(error);
throw new InternalServerError("비밀번호 암호화 실패");
}
};
// 비밀번호 확인
public comparePassword = async (password: string, hashedPwd: string) => {
try {
const match = await bcrypt.compare(password, hashedPwd);
if (match === true) return match;
} catch (error) {
console.log(error);
throw new BadRequest("비밀번호 확인에 실패했습니다.");
}
};
}
// UserService 회원가입 비즈니스 로직에 추가
// 비밀번호 암호화
const hashEncryptionUtil = new HashEncryptionUtil(10);
const hashedPwd = hashEncryptionUtil.encryptPassword(pwd);
https://krauser085.github.io/encryption/
[보안] 비밀번호 암호화: 해쉬와 솔트
https://inpa.tistory.com/entry/NODE-%F0%9F%93%9A-bcrypt-%EB%AA%A8%EB%93%88-%EC%9B%90%EB%A6%AC-%EC%82%AC%EC%9A%A9%EB%B2%95#bcrypt