IT/NodeJS

NodeJS 에서 Bcrypt 사용

성준하이 2024. 5. 12. 08:42
반응형

NodeJS 에서 Bcrypt를 사용하여 값을 암호화 하는 방법에 대해 소개한다.

 

Bcrypt에 대한 내용은 아래 참고 포스팅 참고 바란다.

 

우선 사용을 위해서 npm install 을 진행해야한다.

 

  • npm install bcrypt

그리고 설치된 모듈을 불러온다.

  • const bcrypt = require('bcrypt');

암호화 코드

.. 이상 생략
//hash
const passwd = 'test123'
bcrypt.hash(passwd, 10, (err, encryptedPW) => {
  //callback method 구현
})

// hashSync
const passwd = 'test123';
const encryptedPW = bcrypt.hashSync(passwd, 10); //비밀번호 암호화

 

검증 코드

const passwd = 'test123';
const encryptedPW = bcrypt.hashSync(passwd, 10);

bcrypt.compare(passwd , encryptedPW, (err, same) => {
  console.log(same);  //=> true
})

//------------------------------------------------

const passwd = 'test123';
const encryptedPW = bcrypt.hashSync(passwd, 10);

const same = bcrypt.compareSync(passwd, encryptedPW);
console.log(same); // same = true

참고 포스팅

https://thenicesj.tistory.com/99

 

암호화란?

먼저 해당 포스팅을 작성하기 앞서 나는 보안이나 암호화 관련된 전문 인력은 아니므로 , 어디까지나 얕은 지식이나 잘못된 지식이 있을수도 있습니다. 틀린게 있다면 댓글을 통해서 알려주시

thenicesj.tistory.com

 

반응형