#python3.4
hashlib module - A common interface to many hash functions.
hash.digest() - Return the digest of the data passed to the update() method so far. This is a bytes object of size digest_size which may contain bytes in the whole range from 0 to 255.
hash.hexdigest() - Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.

注1:python 3 由于默认是 Unicode

因此 update(arg) 中的 arg如果是字符串时,需要用b开头,例如 b"hello world!!"。

注2:digest 和 hexdigest 的区别

MD5为例,及校验和为128bit数据,对应16Byte 数据。但这个16Byte无法用ASCII直接表示(存储、通信等)。
因此 使用 hexdigest 将 128bit分为 32个4bit数据,4bit数据用hex(0~9,A~F)的ASCII表示。这样对很多通信协议就好用了,也风方便 人来阅读及比较。

举例:

>>>import hashlib
>>>hashlib.md5(b"hello").digest()
b']A@*\xbcK*v\xb9q\x9d\x91\x10\x17\xc5\x92'
>>>hashlib.md5(b"hello").hexdigest()
'5d41402abc4b2a76b9719d911017c592'

1、将 hexdigest拆分如下:
5d 41 40 2a bc 4b 2a 76 b9 71 9d 91 10 17 c5 92

2、每2为hex转换为 hex( 其中 大于0x80的无法转换,0x0~0x7F中有些也需要转移。可得
b ' ] A @ * \xbc K * v \xb9 q \x9d \x91 \x10 \x17 \xc5 \x92。 和 digest( 就对起来了)。

注3:HASH主要函数

MD5 -- 128bit,SHA-1 -- 160bit, SHA-256 -- 256bit, SHA-512 -- 512bit;

python 3.6开始 支持 SHA3相关算法。

>>> hashlib.md5(b"").hexdigest()
md5= d41d8cd98f00b204e9800998ecf8427e

>>> hashlib.sha1(b"").hexdigest()
sha1= da39a3ee5e6b4b0d3255bfef95601890afd80709

>>> hashlib.sha256(b"").hexdigest()
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'

>>> hashlib.sha512(b"").hexdigest()
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

 ============== 备注

A、Windows下校验 文件 hash值 ( MD5,SHA1,SHA256,SHA512 必须是大写):

certutil -hashfile filename.ext MD5
certutil -hashfile filename.ext SHA1
certutil -hashfile filename.ext SHA256

B、Linux下校验 文件hash值

参见:《【笔记】shell下的主要工具》 中

md5sum,sha1sum,sha256sum 等命令

Python Hashlib笔记的更多相关文章

  1. Python学习笔记基础篇——总览

    Python初识与简介[开篇] Python学习笔记——基础篇[第一周]——变量与赋值.用户交互.条件判断.循环控制.数据类型.文本操作 Python学习笔记——基础篇[第二周]——解释器.字符串.列 ...

  2. Python学习笔记(十一)

    Python学习笔记(十一): 生成器,迭代器回顾 模块 作业-计算器 1. 生成器,迭代器回顾 1. 列表生成式:[x for x in range(10)] 2. 生成器 (generator o ...

  3. Python学习笔记六

    Python课堂笔记六 常用模块已经可以在单位实际项目中使用,可以实现运维自动化.无需手工备份文件,数据库,拷贝,压缩. 常用模块 time模块 time.time time.localtime ti ...

  4. Python学习笔记,day5

    Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...

  5. Web Scraping with Python读书笔记及思考

    Web Scraping with Python读书笔记 标签(空格分隔): web scraping ,python 做数据抓取一定一定要明确:抓取\解析数据不是目的,目的是对数据的利用 一般的数据 ...

  6. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  7. VS2013中Python学习笔记[Django Web的第一个网页]

    前言 前面我简单介绍了Python的Hello World.看到有人问我搞搞Python的Web,一时兴起,就来试试看. 第一篇 VS2013中Python学习笔记[环境搭建] 简单介绍Python环 ...

  8. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  9. python datetime笔记

    python datetime笔记 http://mint-green.diandian.com/post/2011-09-09/4892024 获取当前时间,并通过字符串输出. 格式为:%Y-%m- ...

随机推荐

  1. Codeforces Round #390 (Div. 2) A

    One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into sev ...

  2. eclipse打开jsp的方式怎么设置成默认

    https://jingyan.baidu.com/article/4ae03de34137be3eff9e6b93.html

  3. python学习之队列

    import queue task_queue = queue.Queue() #创建队列

  4. [已读]CSS禅意花园

    蛮早的一本书,提到了一些小tip,比如负margin实现居中.FIR图像替换.

  5. git(代码仓库)

    第1章 git介绍 1.1 参数: 第2章 git管理一个项目 2.1 图示 2.2 cd /项目路径 2.3 git config --globle user.email  "邮箱地址&q ...

  6. AndroidStudio进行Build时出现DexArchiveMergerException异常的解决办法

    今天在AndroidStudio中导入了一个项目,编译的时候没有什么问题,但是在执行Rebuild Project 和 Build APK(s)时报错了,提示: Error:Execution fai ...

  7. 使用Android-Debug-Database 在浏览器中查看App的数据库

    使用参考:http://www.jianshu.com/p/89ccae3e590b源码地址:https://github.com/amitshekhariitbhu/Android-Debug-Da ...

  8. c语言中的->代表什么意思

    c语言中 ->符号是什么意思? 比如c=a->b a为结构体或联合体的指针,->表示调用其成员

  9. Redis性能优化之redis.cnf配置参数

    redis调优总结 1.相应的参数调优 加内存2.redis使用结构调优3.使用合理的数据类型说明:redis存储的数据为redis hash(字符映射表) 单key多字段结构. 1)调整配置文件中配 ...

  10. SSave ALAsset image to disk fast on iOS

    I am using ALAsset to retrieve images like that: [[asset defaultRepresentation] fullResolutionImage] ...