작업 환경 : Python 3.7 / Linux

 

# python 프롬프트

import hashlib

test_password = 'cat'
after_password = test_password.encode('utf-8')

password_hash = hashlib.new('sha256')
password_hash.update(after_after_password)
print(password_hash.hexdigest())
# 77af778b51abd4a3c51c5ddd97204a9c3ae614ebccb75a606c3b6865aed6744e

 

Python3 버전에서의 기본 문자열은 unicode이다. hashlib은 byte를 필요로 하기 때문에 유니코드 문자열을 hashlib으로 가공 시도시 아래 에러 문구가 발생되며 작동하지 않는다.

TypeError: Unicode-objects must be encoded before hashing
이러한 문제에 대한 해결을 위하여 위 예시처럼 encode('utf-8') 을 통해 utf-8로 변환시켜주었다.

 

+ Recent posts