1) bcryp와 pyjwt 프로그램 설치
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pip -V
pip 19.3.1 from /Users/marie/miniconda3/envs/test_project/lib/python3.7/site-packages/pip (python 3.7)
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » 2 ↵
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pip freeze 2 ↵
asgiref==3.2.3
certifi==2019.11.28
Django==3.0.1
django-cors-headers==3.2.0
pytz==2019.3
sqlparse==0.3.0
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pip install bcrypt
Collecting bcrypt
Using cached https://files.pythonhosted.org/packages/62/20/4c94f3f8dfc6b8720c8bc903ce2951ec6397ad864e3a64b4abdced014514/bcrypt-3.1.7-cp34-abi3-macosx_10_6_intel.whl
Collecting cffi>=1.1
Using cached https://files.pythonhosted.org/packages/d5/61/32b1aa5ef1bf60be4ef679c4aae082a7ceef98517e0e0fde68072c6ef8b6/cffi-1.13.2-cp37-cp37m-macosx_10_6_intel.whl
Collecting six>=1.4.1
Using cached https://files.pythonhosted.org/packages/65/26/32b8464df2a97e6dd1b656ed26b2c194606c16fe163c695a992b36c11cdf/six-1.13.0-py2.py3-none-any.whl
Processing /Users/marie/Library/Caches/pip/wheels/f2/9a/90/de94f8556265ddc9d9c8b271b0f63e57b26fb1d67a45564511/pycparser-2.19-py2.py3-none-any.whl
Installing collected packages: pycparser, cffi, six, bcrypt
Successfully installed bcrypt-3.1.7 cffi-1.13.2 pycparser-2.19 six-1.13.0
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pip install pyjwt
Collecting pyjwt
Using cached https://files.pythonhosted.org/packages/87/8b/6a9f14b5f781697e51259d81657e6048fd31a113229cf346880bb7545565/PyJWT-1.7.1-py2.py3-none-any.whl
Installing collected packages: pyjwt
Successfully installed pyjwt-1.7.1
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » pip freeze
asgiref==3.2.3
bcrypt==3.1.7
certifi==2019.11.28
cffi==1.13.2
Django==3.0.1
django-cors-headers==3.2.0
pycparser==2.19
PyJWT==1.7.1
pytz==2019.3
six==1.13.0
sqlparse==0.3.0
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango »
bcrypt로 해시 패스워드 생성하기
bycrypt.hashpw(바이너리값, bcrypt.gensalt())
bcrypt 해시패스워드 값 체크
bycrypt.checkpw(바이너리값, 비크립트된해시값)
(test_project) marie@MarieJungui-MacBookPro:~/mynewdjango » python
Python 3.7.5 (default, Oct 25 2019, 10:52:18)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import bcrypt
>>>
>>> test = bcrypt.hashpw(bytes('1234','utf-8'), bcrypt.gensalt())
>>> test
b'$2b$12$CUiWQ.aDa3xLrltBKbCalu0F1A/o6pB7wxaqAiDtRCaL3mde1zLa6'
>>>
>>>
>>> a = '1234'.encode('utf-8')
>>> b = bcrypt.hashpw('1234'.encode('utf-8'), bcrypt.gensalt())
>>>
>>> bcrypt.checkpw(a,b)
True
>>>
>>>
[참고] 파이썬으로 바이너리값 인코딩<-->디코딩 방법 2가지
파이썬DB에 저장할 때는 바이너리를 다시 디코딩해서 저장해야해요!>> >> bytes('1234','utf-8') b'1234' >> >> bytes('1234','utf-8').decode('utf-8') '1234' >> >> >> '1234'.encode('utf-8') b'1234' >> >> '1234'.encode('utf-8').decode('utf-8') '1234' >>
jwt.decode(제이슨타입의값,시크릿키번호,algorithm='HS256')
>> import jwt >> >> temp = jwt.encode(1,'abc',algorithm='HS256') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/marie/miniconda3/envs/test_project/lib/python3.7/site-packages/jwt/api_jwt.py", line 49, in encode raise TypeError('Expecting a mapping object, as JWT only supports ' TypeError: Expecting a mapping object, as JWT only supports JSON objects as payloads. >> >> temp = jwt.encode({'id':1},'abc',algorithm='HS256') >> temp b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MX0.UQs9ZDN6wv9OmYLkHp9JFHCxfJHvz0AtUuijOLze_aE' >> >> temp.decode('utf-8') 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MX0.UQs9ZDN6wv9OmYLkHp9JFHCxfJHvz0AtUuijOLze_aE' >> >> >> jwt.decode(temp, 'abc', algorithm="HS256") {'id': 1} >>
결론적으로, jwt.decode를 통해서
암호화했던 제이슨타입의 데이터를 복호화 할 수 있다.