TIL14. Bcrypt

박경철·2021년 6월 6일
0

TIL

목록 보기
14/19

1. Bcrypt

로그인 인증 시 비밀번호를 암호화 하기 위하여 사용한 라이브러리

pip install bcrypt
password = '1234'
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())

new_password = '1234'
bcrypt.checkpw(new_password.encode('utf-8'),hashed_password)

bcrypt는 str 데이터가 아닌 Bytes 데이터를 암호화 합니다. 따라서 암호화시에 비밀번호 데이터를 bytes화 해야합니다.
파이썬에서는 str 을 encode하면 bytes(이진화) 되고, Bytes 를 decode하면 str 화 합니다. encode, decode시에는 우리가 인식할 수 있는 형태로 변환하기 위해 'UTF-8' 유니코드 문자 규격을 사용합니다.
비밀번호를 암호화 하기 위해 hashpw() 함수, 복호화 하기 위해 checkpw()를 사용합니다.

pip install django-bcrypt
password = '1234'
hashed_password = bcrypt.hashpw(password, bcrypt.gensalt())

new_password = '1234'
bcrypt.checkpw(new_password, hashed_password)

하지만 django-bcrypt를 사용하면 encode, decode 과정을 생략할 수 있습니다.

profile
안녕하세요!

0개의 댓글