python3下,利用hash值对字符串进行md5加密时报错:
TypeError: Unicode-objects must be encoded before hashing

原因是:
python3跟python2区别:python3下字符串为Unicode类型,而hash传递时需要的是utf-8类型,因此,需要类型转换
调用函数时,将字符串进行类型转换

import hashlib

def get_md5(s):
    m = hashlib.md5()
    m.update(s)
    return m.hexdigest()

print(get_md5("gg".encode("utf8")))

或者
def get_md5(s):
    m = hashlib.md5()
    m.update(str(s).encode('utf-8'))
    return m.hexdigest()

print(get_md5("gg"))

done!

python3 TypeError: Unicode-objects must be encoded before hashing的更多相关文章

  1. Python - TypeError: unicode argument expected, got 'str'

    参考:TypeError: unicode argument expected, got 'str' Python代码: from io import StringIO def main(): f = ...

  2. appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!

    这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...

  3. Python hashlib Unicode-objects must be encoded before hashing

    Python2中没有这个问题 python3中 hashlib.md5(data)函数中data 参数的类型应该是bytes hash前必须把数据转换成bytes类型 Python 2.7.12 (d ...

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

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

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

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

  6. python3 TypeError: a bytes-like object is required, not 'str'

    在学习<Python web开发学习实录>时, 例11-1: # !/usr/bin/env python # coding=utf-8 import socket sock = sock ...

  7. python3.3 unicode(encode&decode)

    最近在用python写多语言的一个插件时,涉及到python3.x中的unicode和编码操作,本文就是针对编码问题研究的汇总,目前已开源至github.以下内容来自项目中的README. 1 ASC ...

  8. Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

    python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io ...

  9. 爬虫python3:TypeError: cannot use a string pattern on a bytes-like object

    import re from common_p3 import download def crawl_sitemap(url): sitemap = download(url) links = re. ...

随机推荐

  1. Android SurfaceView内容获取

    Android SurfaceView内容获取 这几天在做一个Android的小项目,项目中需要使用到SurfaceView来显示相机捕获的内容,同时还有一个SurfaceView用于绘制一些辅助线, ...

  2. php优秀框架codeigniter学习系列——异常和错误处理机制

    这篇介绍下CI框架的异常和错误处理机制. 在入口文件index.php中,根据设置的环境参数设置error_reporting的范围,和是否显示错误. 在CI初始化程序CodeIgniter.php中 ...

  3. calc() --- css3

    http://www.w3cplus.com/css3/how-to-use-css3-calc-function.html

  4. 使用 TortoiseSVN 创建 svn branch

    1.使用TortoiseSVN->Repo-browser进入仓库. 2.选择需要创建分支的文件->Copy to 添加分支路径后,点击ok Rename:trunk路径 格式:https ...

  5. 百度api--之导航

    其实挺简单的,只要知道出发点和终点的经纬度就可以了; 百度webURLAPI : http://lbsyun.baidu.com/index.php?title=uri/api/web 这个是百度的a ...

  6. tmux学习

    1.基本命令: http://blog.chinaunix.net/uid-26285146-id-3252286.html (重要) http://blog.csdn.net/longxibendi ...

  7. Android内核栈溢出与ROP(CVE-2013-2597)

    一.准备 由于内核栈不可执行(NX),栈溢出利用需用到ROP.简单回顾一下ARM ROP. 漏洞演示代码如下,网上随便找了个. char *str="/system/bin/sh" ...

  8. 【leetcode】66-PlusOne

    problem Plus One code class Solution { public: vector<int> plusOne(vector<int>& digi ...

  9. Linux按照时间顺序列出文件

    按照递增时间顺序列出所有文件 ls -ltr -l表示列出长串数据,-t表示按照时间顺序,-r表示将排序的结果反向输出 按照时间递减的顺序列出所有文件 ls -lt

  10. Pytorch加载模型推荐的方法

    https://stackoverflow.com/questions/42703500/best-way-to-save-a-trained-model-in-pytorch