hashing, bcrypt

y___h·2023년 4월 14일
0

사람들은 같은 비밀번호를 여러군데서 쓰기 때문에, 비밀번호 유출이 되면 큰 피해로 이어질 수 있다.

그래서 회원가입 이후 비밀번호를 잊어버렸을 때,
비밀번호 찾기가 아닌 새로 만들기만 서비스되는 이유가 바로 이러한 이유 때문이다.

✔️ hashing(해싱)은 단방향 함수

비밀번호 --해싱+saltRounds--> 해시값
해시값 ---> 비밀번호를 알아낼 수 없음.

해싱을 하더라도, rainbow table을 통해 해킹당할 위험이 있는데,
그래서 saltRounds값을 주어, 여러번 해싱하여 위험을 줄일 수 있다.

부트캠프에서도 해싱, jwt, 보안 인증 인가 등 관련 개념들을 배웠었는데, 자주 안쓰다보니 머릿속에서 희미한 기억으로만 남아 있을 것 같아 글을 남겨둔다 :)


지금 하고 있는 개인 프로젝트를 하다가 회원가입을 구현하던 중,
비밀번호 hashing이 필요하여 bcrypt를 사용했다.

import bcrypt from "bcrypt";
import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
  email: { type: String, required: true, unique: true },
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
});

// this : create 되는 User를 의미함.
userSchema.pre("save", async function () {
  console.log("User password:", this.password); // real pw
  this.password = await bcrypt.hash(this.password, 5);
  console.log("Hashed password:", this.password);
}); // hashed pw

const User = mongoose.model("User", userSchema);

export default User;
profile
기록 이사중 🐣

0개의 댓글