1. ホーム
  2. プログラミング言語
  3. パイソン

python TypeError: Unicode オブジェクトはハッシュ化する前にエンコードする必要がある 解決策

2022-01-21 14:52:09
<パス
# Import the modules needed for md5 encryption
import hashlib
# Create md5 object
m = hashlib.md5()
# Generate the encrypted string, where password is the string to be encrypted
m.update("password")
# Get the encrypted string
pw = m.hexdigest()
print(pw)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

最初のエラーメッセージ

TypeError: Unicode-objects must be encoded before hashing
# Import the modules needed for md5 encryption
import hashlib
# Create md5 object
m = hashlib.md5()
# Generate the encrypted string, where password is the string to be encrypted

*************** is modified as follows ******************
m.update("password".encode('utf-8'))

# Get the encrypted string
pw = m.hexdigest()

print(pw)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13