涉及加密服务:14. Cryptographic Services其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1、SHA224、SHA256、SHA384、SHA512、MD5等。

快速入门

  1. import hashlib
  2. m = hashlib.md5() #创建hash对象,md5:(message-Digest Algorithm 5)消息摘要算法,得出一个128位的密文
  3. print m #<md5 HASH object @ 000000000254ADF0>
  4. m.update('BeginMan')#更新哈希对象以字符串参数
  5. print m.digest() #返回摘要,作为二进制数据字符串值
  6. print m.hexdigest() #返回十六进制数字字符串 0b28251e684dfbd9102f8b6f0281c0c5
  7. print m.digest_size #16
  8. print m.block_size #64

使用new()创建指定加密模式的hash对象

  1. new(name, string='')
  2. """
  3. Return a new hashing object using the named algorithm;
  4. optionally initialized with a string.
  5. """
  6. h = hashlib.new('md5')
  7. print h #<md5 HASH object @ 000000000260BDB0>
  8. h2 = hashlib.new('ripemd160','what')
  9. print h2 #<ripemd160 HASH object @ 000000000271B9F0>
  10. h.update('beginman')
  11. print h.hexdigest() #666fc5baa93a7fb207c5bfff03b67732
  12. #等效
  13. s = hashlib.md5()
  14. s.update('beginman')
  15. print s.hexdigest() #666fc5baa93a7fb207c5bfff03b67732
  16. print h2.hexdigest() #9c1185a5c5e9fc54612808977ee8f548b2258d31

常用属性

  1. print hashlib.algorithms #('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') 列出所有加密算法
  2. print h.digest_size #16 产生的散列的字节大小。
  3. print h.block_size #64 The internal block size of the hash algorithm in bytes.

常用方法

  1. hash.update(arg)
  2. 更新哈希对象以字符串参数,如果同一个hash对象重复调用该方法,则m.update(a); m.update(b) is equivalent to m.update(a+b).
  3. hash.digest()
  4. 返回摘要,作为二进制数据字符串值,
  5. hash.hexdigest()
  6. 返回摘要,作为十六进制数据字符串值,
  7. hash.copy()
  8. 复制

应用实例

  1. 注册、登录、文件上传、相册加密....
  1. import datetime
  2. KEY_VALUE = '/BeginMan/'
  3. now = datetime.datetime.now()
  4. m = hashlib.md5()
  5. str = '%s%s' %(KEY_VALUE,now.strftime("%Y%m%d"))
  6. m.update(str)
  7. value = m.hexdigest()
  8. print value #8db42d3e90b41105ed061b8347a7c850

python笔记之hashlib模块的更多相关文章

  1. 13.python笔记之pyyaml模块

    Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...

  2. python笔记之常用模块用法分析

    python笔记之常用模块用法分析 内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像 ...

  3. python笔记之bisect模块

    python笔记之bisect模块 当你决定使用二分搜索时,这个模块会给你带来很大的帮助. 例子 import bisect L = [1,3,3,6,8,12,15] x = 3 #在L中查找x,x ...

  4. python笔记之itertools模块

    python笔记之itertools模块 itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生 ...

  5. python笔记之ZipFile模块

    python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...

  6. python笔记之subprocess模块

    python笔记之subprocess模块 [TOC] 从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spaw ...

  7. python笔记之Cmd模块

    python笔记之Cmd模块 Cmd类型提供了一个创建命令行解析器的框架,默认情况下,它使用readline来进行交互式操作.命令行编辑和命令完成. 使用cmd创建的命令行解释器循环读取输入的所有行并 ...

  8. python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)

    logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...

  9. Python正则表达式与hashlib模块

    菜鸟学python第十六天 1.re模块(正则表达式) 什么是正则表达式 正则表达式是一个由特殊字符组成的序列,他能帮助对字符串的某种对应模式进行查找. 在python中,re 模块使其拥有全部的正则 ...

随机推荐

  1. Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  2. Only2 Labs — A Visual Design Studio

    Only2 Labs - A Visual Design Studio 设计合作 对您目前的设计很不满意?或是急缺一个设计供应商?您的团队最近做的项目需要指导?Only2都很乐意为您解困惑. 或者,你 ...

  3. mysql用户和权限管理

    用户和权限管理 Information about account privileges is stored in the user, db, host, tables_priv, columns_p ...

  4. hdu 5640 King's Cake(模拟)

    Problem Description   It is the king's birthday before the military parade . The ministers prepared ...

  5. html中把li前面的的小圆点换成小图片的方法

    li { list-style: none; background: url(../img/li_dis.png) no-repeat left; padding-left: 20px; }

  6. MVC中的View2(转)

    MVC中View是专门用来向浏览器显示结果的,它只负责把传入到View的数据展现给用户: 一,自定义view引擎:实现IViewEngine接口 namespaceSystem.Web.Mvc { p ...

  7. 使用DataReader读取数据

    List<User> allUsers = new List<User>(); SqlConnection conn = new SqlConnection(连接字符串); S ...

  8. JAVA加密

    [源地址http://www.iteye.com/topic/1122076/] 加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信 ...

  9. 调用[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad判断设备

    将模拟器改为Ipad时,调用[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad判断设备是否为Ipad,但程序并 ...

  10. 数据存储: sqlite,coredata plist 归档

    sql 语句  结构化查询语言 通用数据库操作语言1.创建数据库create database 1407EDB2.删除数据库drop database 1407EDB3.备份use master ex ...