Hashing ์๊ณ ๋ฆฌ์ฆ
- Hashing์๊ณ ๋ฆฌ์ฆ์ ๋จ๋ฐฉํฅ ์ํธํ๋ก,
- ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ํด์๊ฐ์ผ๋ก๋ถํฐ ๋ณต์ํ๋ ๊ฒ์ด ๋ถ๊ฐ๋ฅํ๋ฏ๋ก ๋ณด์์ด ์ค์ํ ๋ถ์ผ์์ ๋ง์ด ํ์ฉ๋๋ค.
- ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ ๊ฒ์ฌ, ๋น๋ฐ๋ฒํธ ์ ์ฅ, ๋์งํธ ์๋ช , ๋ธ๋ก์ฒด์ธ ๋ฑ ๋ค์ํ ๋ถ์ผ์์ ์ฌ์ฉ๋๋ค.
vscode ํฐ๋ฏธ๋ cmd์์ ์ค์นํ๊ธฐ
pip install email-validator
pip install passlib
pip install psycopg2-binary
SALT๋ฅผ config ํ์ผ์ ๋ง๋ ๋ค. (SALT๋ random_state์ ๋น์ทํ ๊ฐ๋ ์ผ๋ก, ๋ ธ์ถ๋๋ฉด ํดํน๋ ์ ์๋ค.)
SALT๋ ๋ณธ์ธ์ด ์ํ๋๋๋ก ์๋ฌด๋ ๊ฒ๋ ์ง์ ํ๋ฉด ๋๋ค
utils.py ๋ง๋ค๊ธฐ
from passlib.hash import pbkdf2_sha256
from config import Config
# ์๋ฌธ ๋น๋ฐ๋ฒํธ๋ฅผ, ๋จ๋ฐฉํฅ์ผ๋ก ์ํธํํ๋ ํจ์
def hash_password(original_password) :
original_password = original_password + Config.SALT
password = pbkdf2_sha256.hash(original_password)
return password
# ์ ์ ๊ฐ ๋ก๊ทธ์ธํ ๋ ์
๋ ฅํ ๋น๋ฐ๋ฒํธ๊ฐ ๋ง๋์ง ์ฒดํฌ(๋ณตํธํ)ํ๋ ํจ์
def check_password(original_password, hashed_password) :
original_password = original_password + Config.SALT
return pbkdf2_sha256.verify(original_password, hashed_password)
๋น๋ฐ๋ฒํธ ์ ๋ ฅํ๊ณ , ์คํ์ํค๋ฉด(python utils.py) 1234๋ฅผ ๋น๋ฐ๋ฒํธ๋ก ๋ง๋ ๋ค.
hashed_password = hash_password('1234')
print(hashed_password)
๋ณต์ฌํด์, ๋น๋ฐ๋ฒํธ ์ฒดํฌํ๋ ์คํ๋ฌธ ๋ง๋ค๊ธฐ
check = check_password('1235', '$pbkdf2-sha256$29000$936P8T7nfK/13rvXmnMOYQ$45uMcjbt6Cg4hDHbsJs3tuR7abOJ4j8Wfczblc5FC9E')
print(check)