bcrypt.hashpw(password,bcrypt.gensalt())
Bcrypt 란?
- 1999년에 출시된 password-hashing function이다.
- Blowfish 암호를 기반으로 설계된 암호화 함수이며 현재까지 가장 강력한 해시 매커니즘 중 하나
- 보안으로 유명한 OpenBSD에서 사용 중
- 반복횟수를 늘려 연산 속도를 늦출 수 있으므로 연산 능력이 증가하더라고 brute-force 공격 대비 가능
- 구현이 쉽고 비교적 걍력하다
사용법
1. 설치
yarn add bcrypt
2. 비밀번호 암호화
- hash는 동기, hashSync는 비동기 방식
- 파라미터로 넣은 숫자는 암호화에 사용되는 Salt로, 값이 높을수록 암호화 연산이 증가함.(속도는 느려짐)
//비밀번호 암호화
const bcrypt = require("bcrypt");
const hash = bcrypt.hashSync(information.password, 12);
const user = await client.user.create({
data: {
name: information.name,
email: information.email,
password: hash,
},
});
/**
bcrypt.hash(PW, 12,(err,encryptedPW) =>{
//hash 비동기 콜백
});
//hashSync 비동기
const hash = bcrypt.hasySync(PW,salt);
// async/await 사용
const hash = await bcrypt.hash(PW,salt);
*/
// 비밀번호 검증하기
const PW = 'abcd1234';
const hash = bcrypt.hashSync(PW,12);
bcrypt.compare(PW,hash,(err,same)=>{
console.log('same');
})
const match = bcrypt.compareSync(PW,hash);
if(match){
//login
}
const match = await bcrypt.compare(PW,hash);
if(match){
//login
}
'FrontEnd > Next.js' 카테고리의 다른 글
[나 혼자 볼거] 소셜로그인을 위한 API 키 받기(feat. kakao, naver, google) (0) | 2023.01.23 |
---|---|
[Next.js] NextAuth로 로그인 처리하기(feat. 로그인 페이지 커스텀) (0) | 2023.01.23 |
[Next.js] Next.js API 만들기 (feat. handler, withHandler) (0) | 2023.01.22 |
[Next.js] OAuth(2.0)를 통한 로그인 인증 (0) | 2023.01.10 |
페이지 렌더링 비교 (SSR vs CSR vs SSG (0) | 2023.01.04 |