Django注册时,使用hashlib加密密码,如何解决Unicode对象编码问题?

2026-05-26 14:301阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计205个文字,预计阅读时间需要1分钟。

Django注册时,使用hashlib加密密码,如何解决Unicode对象编码问题?

在使用sh1等hashlib方法进行加密时,若遇到Unicode-objects must be encoded before hashing的错误,解决方法如下:

1. 指定要加密的字符串的编码格式。

2.修改代码如下:

解决前:

python s1=sha1() s1.update(upwd) upwd2=s1.hexdigest()

解决后: python s1=sha1() s1.update(upwd.encode('utf-8')) upwd2=s1.hexdigest()


在使用sh1等hashlib方法进行加密时报:Unicode-objects must be encoded before hashing

解决办法:对要加密的字符串指定编码格式

解决之前:

s1=sha1()
s1.update(upwd)
upwd2 = s1.hexdigest()


解决之后:

Django注册时,使用hashlib加密密码,如何解决Unicode对象编码问题?

s1=sha1()
s1.update(upwd.encode("utf-8"))
upwd2 = s1.hexdigest()


就增加了encode("utf-8")



更多内容详见微信公众号:Python研究所



本文共计205个文字,预计阅读时间需要1分钟。

Django注册时,使用hashlib加密密码,如何解决Unicode对象编码问题?

在使用sh1等hashlib方法进行加密时,若遇到Unicode-objects must be encoded before hashing的错误,解决方法如下:

1. 指定要加密的字符串的编码格式。

2.修改代码如下:

解决前:

python s1=sha1() s1.update(upwd) upwd2=s1.hexdigest()

解决后: python s1=sha1() s1.update(upwd.encode('utf-8')) upwd2=s1.hexdigest()


在使用sh1等hashlib方法进行加密时报:Unicode-objects must be encoded before hashing

解决办法:对要加密的字符串指定编码格式

解决之前:

s1=sha1()
s1.update(upwd)
upwd2 = s1.hexdigest()


解决之后:

Django注册时,使用hashlib加密密码,如何解决Unicode对象编码问题?

s1=sha1()
s1.update(upwd.encode("utf-8"))
upwd2 = s1.hexdigest()


就增加了encode("utf-8")



更多内容详见微信公众号:Python研究所