python笔记之hashlib模块
涉及加密服务:14. Cryptographic Services其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1、SHA224、SHA256、SHA384、SHA512、MD5等。
快速入门
import hashlib
m = hashlib.md5() #创建hash对象,md5:(message-Digest Algorithm 5)消息摘要算法,得出一个128位的密文
print m #<md5 HASH object @ 000000000254ADF0>
m.update('BeginMan')#更新哈希对象以字符串参数
print m.digest() #返回摘要,作为二进制数据字符串值
print m.hexdigest() #返回十六进制数字字符串 0b28251e684dfbd9102f8b6f0281c0c5
print m.digest_size #16
print m.block_size #64
使用new()创建指定加密模式的hash对象
new(name, string='')
"""
Return a new hashing object using the named algorithm;
optionally initialized with a string.
"""
h = hashlib.new('md5')
print h #<md5 HASH object @ 000000000260BDB0>
h2 = hashlib.new('ripemd160','what')
print h2 #<ripemd160 HASH object @ 000000000271B9F0>
h.update('beginman')
print h.hexdigest() #666fc5baa93a7fb207c5bfff03b67732
#等效
s = hashlib.md5()
s.update('beginman')
print s.hexdigest() #666fc5baa93a7fb207c5bfff03b67732
print h2.hexdigest() #9c1185a5c5e9fc54612808977ee8f548b2258d31
常用属性
print hashlib.algorithms #('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') 列出所有加密算法
print h.digest_size #16 产生的散列的字节大小。
print h.block_size #64 The internal block size of the hash algorithm in bytes.
常用方法
hash.update(arg)
更新哈希对象以字符串参数,如果同一个hash对象重复调用该方法,则m.update(a); m.update(b) is equivalent to m.update(a+b).
hash.digest()
返回摘要,作为二进制数据字符串值,
hash.hexdigest()
返回摘要,作为十六进制数据字符串值,
hash.copy()
复制
应用实例
注册、登录、文件上传、相册加密....
import datetime
KEY_VALUE = '/BeginMan/'
now = datetime.datetime.now()
m = hashlib.md5()
str = '%s%s' %(KEY_VALUE,now.strftime("%Y%m%d"))
m.update(str)
value = m.hexdigest()
print value #8db42d3e90b41105ed061b8347a7c850
python笔记之hashlib模块的更多相关文章
- 13.python笔记之pyyaml模块
Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...
- python笔记之常用模块用法分析
python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...
- python笔记之bisect模块
python笔记之bisect模块 当你决定使用二分搜索时,这个模块会给你带来很大的帮助. 例子 import bisect L = [1,3,3,6,8,12,15] x = 3 #在L中查找x,x ...
- python笔记之itertools模块
python笔记之itertools模块 itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生 ...
- python笔记之ZipFile模块
python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...
- python笔记之subprocess模块
python笔记之subprocess模块 [TOC] 从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spaw ...
- python笔记之Cmd模块
python笔记之Cmd模块 Cmd类型提供了一个创建命令行解析器的框架,默认情况下,它使用readline来进行交互式操作.命令行编辑和命令完成. 使用cmd创建的命令行解释器循环读取输入的所有行并 ...
- python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)
logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...
- Python正则表达式与hashlib模块
菜鸟学python第十六天 1.re模块(正则表达式) 什么是正则表达式 正则表达式是一个由特殊字符组成的序列,他能帮助对字符串的某种对应模式进行查找. 在python中,re 模块使其拥有全部的正则 ...
随机推荐
- AngularJS中的控制器示例_2
<!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ...
- css 完美替换图片
1.css替换简单图标的展示方法 ;display:inline-block;position:absolute;left:11px;top:10px;border-right:6px solid t ...
- Codeforces Round #277 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...
- IC封装图片认识(一):BGA
在上篇文章<常用IC封装技术介绍>第一个提到的IC封装形式就是BGA,全称是Ball Grid Array(球栅阵列结构的PCB),它是集成电路采用有机载板的一种封装法.其具有以下五个特点 ...
- Linux kernel API的查看
一般来说Linux上查看一些函数API的说明咱们可以man一下.man 2是syscall,man 3是一些库的函数API. 以下是man sections的一些说明 The table below ...
- 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)
简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...
- Python与MySQL首次交互
前两天在工作之余研究了一下Python,对基础有了大致了解,就想拿她很MqSQL交互一下. 一开始就遇到了问题,要import MySQLdb,search发现有人说安装mysql-python,于是 ...
- WPF最基本的4个引用
Windowsbase Windows基本类库 PresentationCore wpf核心类库 PresentationFramework wpf框架 System.Axml 系统类库
- VirtualBox 扩展包卸载或安装失败(VERR_ALREADY_EXISTS)
最近在卸载VirtualBox出现了无法卸载的错误.提示为Failed to install the extension. The installer failed with exit code 1: ...
- 【iOS开发-66】QQ设置界面的案例:利用storyboard开发静态的tableView界面,核心是Static Cells
(1)效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JB ...