python提供了一个进行hash加密的模块:hashlib

下面主要记录下其中的md5加密方式(sha1加密一样把MD5换成sha1)

  1. >>> import hashlib
  2. >>> m = hashlib.md5()
  3. >>> m.update("Nobody inspects")
  4. >>> m.update(" the spammish repetition")
  5. >>> m.digest()
  6. '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
  7. >>> m.hexdigest()
  8. 'bb649c83dd1ea5c9d9dec9a18df0ffe9'

对以上代码的说明:

1.首先从python直接导入hashlib模块

2.调用hashlib里的md5()生成一个md5 hash对象

3.生成hash对象后,就可以用update方法对字符串进行md5加密的更新处理

4.继续调用update方法会在前面加密的基础上更新加密

5.加密后的二进制结果

6.十六进制结果

如果只需对一条字符串进行加密处理,也可以用一条语句的方式:

  1. >>>print hashlib.new("md5", "Nobody inspects the spammish repetition").hexdigest()
  2. 'bb649c83dd1ea5c9d9dec9a18df0ffe9'

引用官方文档部分:

The following values are provided as constant attributes of the hash objects returned by the constructors:

hash.digest_size

The size of the resulting hash in bytes.

hash.block_size

The internal block size of the hash algorithm in bytes.

A hash object has the following methods:

hash.update(arg)

Update the hash object with the string arg. Repeated calls are equivalent to a single call with the concatenation of all the arguments: m.update(a); m.update(b) is equivalent to m.update(a+b).

hash.digest()

Return the digest of the strings passed to the update() method so far. This is a string of digest_size bytes which may contain non-ASCII characters, including null bytes.

hash.hexdigest()

Like digest() except the digest is returned as a string of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.

hash.copy()

Return a copy (“clone”) of the hash object. This can be used to efficiently compute the digests of strings that share a common initial substring.

最新文章

  1. 教你如何快速下载旧版本的Firefox浏览器
  2. myeclipse maven pom.xml 配置错误
  3. .NET下实现分布式缓存系统Memcached (转自网络)
  4. VC++ 网络编程总结(一)
  5. Hackers' Crackdown( UVA UVA 11825状压dp)
  6. C# foreach 原理以及模拟的实现
  7. Cmake常用指令
  8. Javascript 浮点运算问题分析与解决
  9. linux下双网卡双网关配置
  10. vue 图片下载到本地,图片保存到本地
  11. HDU1711-KMP-水题
  12. JAVA中的引用
  13. ida pro 使用
  14. CSS Media Query
  15. 【收藏】介绍RCU的好文章
  16. web 开发常见问题--GET POST 区别
  17. Linux命令: 查找文件中的字符串
  18. Spring JdbcTemplate详解
  19. Ajax请求WCF服务以及跨域的问题解决
  20. Python的设计哲学

热门文章

  1. Android网络:开发浏览器(一)——基本的浏览网页功能开发
  2. Tomcat配置NIO
  3. throw与throw的区别
  4. 使用Dreamwaver cc中的SVN功能,用于传输BAE和SAE中的文件
  5. Spring 3 + Quartz 1.8.6 Scheduler Example--reference
  6. Swift的闭包(二):捕获值
  7. Ubuntu知识记录
  8. python 之路,Day27 - 主机管理+堡垒机系统开发
  9. SpringMVC10数据验证
  10. Sniffer抓包教程