Python2中没有这个问题

python3中

hashlib.md5(data)函数中data 参数的类型应该是bytes

hash前必须把数据转换成bytes类型

Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from hashlib import md5
>>> name = md5("hello")
>>> print(name.hexdigest())
5d41402abc4b2a76b9719d911017c592
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from hashlib import md5
>>> name = md5("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Unicode-objects must be encoded before hashing
>>> name = md5("hello".encode("utf-8"))
>>> print(name.hexdigest())
5d41402abc4b2a76b9719d911017c592

Python hashlib Unicode-objects must be encoded before hashing的更多相关文章

  1. Python——在Unicode和普通字符串之间转换

    1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASC ...

  2. Python: 在Unicode和普通字符串之间转换

    Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding): <!-- Inject Script Filtered --> Toggle line nu ...

  3. python hashlib模块 md5加密 sha256加密 sha1加密 sha512加密 sha384加密 MD5加盐

      python hashlib模块   hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, ...

  4. 关于python hashlib模块的使用

    hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, sha512等算法 #!/usr/bin/ ...

  5. python decode unicode encode

    字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...

  6. Python中Unicode字符串

    Python中Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte ...

  7. hashlib使用时出现: Unicode-objects must be encoded before hashing

    # hashlib.md5(data)函数中,data参数的类型应该是bytes# hash前必须把数据转换成bytes类型>>> from hashlib import md5 F ...

  8. python中unicode和str的组合

    python中unicode对象和str对象拼接在一起,会自动将str对象转换成unicode对象 即:a="aa" b=u"bb" c=a+b type(c) ...

  9. django注册在使用hashlib对密码加密时报Unicode-objects must be encoded before hashing

    在使用sh1等hashlib方法进行加密时报:Unicode-objects must be encoded before hashing 解决办法:对要加密的字符串指定编码格式 解决之前: s1=s ...

随机推荐

  1. 有关类朋友圈设计(3) -- 数据库设计&现有技术&流程设计

    在写之前,先说说当前的系统架构吧 spring cloud + zuul + eureka + oauth2 + redis + rabbitMq 这个系统是由我搭建的,当时采用的springClou ...

  2. JavaScript循环 — for、for/in、while、do/while

    for 多次遍历代码块 const array = []for (var i = 0; i < 5; i++) { array.push(i)}console.log(array) // [0, ...

  3. TP5多条件搜索,同时有必要条件

    $model = $this->model; // 查询是否有搜索参数 $search = input('?get.search') ? trim(input('get.search')) : ...

  4. I/O流中的字节流

    今天总结一下Java中重要的知识点I/O流,今天主要学习了字节流(自己的理解) 什么是I/O:我们把这种数据的传输,可以看做是一种数据的流动,按照流动的方向,以内存为基准,分为输入input和输出ou ...

  5. git 要求密码的解决方法:【生成gitLab公钥】:以及如何配置GitLab中的SSH key

    参考链接: https://www.cnblogs.com/yjlch1016/p/9692840.html https://blog.csdn.net/u011925641/article/deta ...

  6. struts2 中 form-action action-form 的传参方式

    1.  struts2 Action获取表单提交数据 主要有三种方式: 1.1 使用ActionContext类 //获取actionContext对象 ActionContext context = ...

  7. jmeter如何确定ramp-up时间

    原文来自:https://www.cnblogs.com/hjhsysu/p/9189897.html 线程属性包含了:线程数.Ramp-Up时间(秒).循环次数. 我整理下线程属性的定义,如图: 难 ...

  8. [转载]使用postgresql安装wordpress

    1. 环境安装sudo apt-get install apache2sudo apt-get install postgresql-9.1sudo apt-get install php5sudo ...

  9. 【C++ Primer Plus】编程练习答案——第2章

    1 void ch2_1() { 2 using namespace std; 3 cout << "xxxxxxxx" << endl; 4 } 5 6 ...

  10. xLua中C#调用Lua

    C#调用Lua 一.前提 这里使用的是XLua框架,需要提前配置xlua,设置加载器路径: 可以参考之前的Blog:<xlua入门基础>: 二.C#调用Lua全局变量 lua中所有的全局变 ...