1. ==md5 模块==
  2.  
  3. ``md5`` (Message-Digest Algorithm 5)模块用于计算信息密文(信息摘要).
  4.  
  5. ``md5`` 算法计算一个强壮的128位密文. 这意味着如果两个字符串是不同的,
  6. 那么有极高可能它们的 ``md5`` 也不同. 也就是说, 给定一个 ``md5`` 密文,
  7. 那么几乎没有可能再找到另个字符串的密文与此相同. [Example 2-35 #eg-2-35]
  8. 展示了如何使用 ``md5`` 模块.
  9.  
  10. ====Example 2-35. 使用 md5 模块====[eg-2-35]
  11.  
  12. ```
  13. File: md5-example-1.py
  14.  
  15. import md5
  16.  
  17. hash = md5.new()
  18. hash.update("spam, spam, and eggs")
  19.  
  20. print repr(hash.digest())
  21.  
  22. *B* 'L\005J\243\266\355\243u`\305r\203\267\020F\303'*b*
  23. ```
  24.  
  25. 注意这里的校验和是一个二进制字符串. [Example 2-36 #eg-2-36] 展示了如何获得一个十六进制或
  26. base64 编码的字符串.
  27.  
  28. ====Example 2-36. 使用 md5 模块获得十六进制或 base64 编码的 md5 值====[eg-2-36]
  29.  
  30. ```
  31. File: md5-example-2.py
  32.  
  33. import md5
  34. import string
  35. import base64
  36.  
  37. hash = md5.new()
  38. hash.update("spam, spam, and eggs")
  39.  
  40. value = hash.digest()
  41. print hash.hexdigest()
  42. # before 2.0, the above can be written as
  43. # 在 2.0 前, 以上应该写做:
  44. # print string.join(map(lambda v: "%02x" % ord(v), value), "")
  45.  
  46. print base64.encodestring(value)
  47.  
  48. *B*4c054aa3b6eda37560c57283b71046c3
  49. TAVKo7bto3VgxXKDtxBGww==*b*
  50. ```
  51.  
  52. [Example 2-37 #eg-2-37] 展示了如何使用 ``md5``
  53. 校验和来处理口令的发送与应答的验证(不过我们将稍候讨论这里使用随机数字所带来的问题).
  54.  
  55. ====Example 2-37. 使用 md5 模块来处理口令的发送与应答的验证====[eg-2-37]
  56.  
  57. ```
  58. File: md5-example-3.py
  59.  
  60. import md5
  61. import string, random
  62.  
  63. def getchallenge():
  64. # generate a 16-byte long random string. (note that the built-
  65. # in pseudo-random generator uses a 24-bit seed, so this is not
  66. # as good as it may seem...)
  67. # 生成一个 16 字节长的随机字符串. 注意内建的伪随机生成器
  68. # 使用的是 24 位的种子(seed), 所以这里这样用并不好..
  69. challenge = map(lambda i: chr(random.randint(0, 255)), range(16))
  70. return string.join(challenge, "")
  71.  
  72. def getresponse(password, challenge):
  73. # calculate combined digest for password and challenge
  74. # 计算密码和质询(challenge)的联合密文
  75. m = md5.new()
  76. m.update(password)
  77. m.update(challenge)
  78. return m.digest()
  79.  
  80. #
  81. # server/client communication
  82. # 服务器/客户端通讯
  83.  
  84. # 1. client connects. server issues challenge.
  85. # 1. 客户端连接, 服务器发布质询(challenge)
  86.  
  87. print "client:", "connect"
  88.  
  89. challenge = getchallenge()
  90.  
  91. print "server:", repr(challenge)
  92.  
  93. # 2. client combines password and challenge, and calculates
  94. # the response.
  95. # 2. 客户端计算密码和质询(challenge)的组合后的密文
  96.  
  97. client_response = getresponse("trustno1", challenge)
  98.  
  99. print "client:", repr(client_response)
  100.  
  101. # 3. server does the same, and compares the result with the
  102. # client response. the result is a safe login in which the
  103. # password is never sent across the communication channel.
  104. # 3. 服务器做同样的事, 然后比较结果与客户端的返回,
  105. # 判断是否允许用户登陆. 这样做密码没有在通讯中明文传输.
  106.  
  107. server_response = getresponse("trustno1", challenge)
  108.  
  109. if server_response == client_response:
  110. print "server:", "login ok"
  111.  
  112. *B*client: connect
  113. server: '\334\352\227Z#\272\273\212KG\330\265\032>\311o'
  114. client: "l'\305\240-x\245\237\035\225A\254\233\337\225\001"
  115. server: login ok*b*
  116. ```
  117.  
  118. [Example 2-38 #eg-2-38] 提供了 ``md5``
  119. 的一个变种, 你可以通过标记信息来判断它是否在网络传输过程中被修改(丢失).
  120.  
  121. ====Example 2-38. 使用 md5 模块检查数据完整性====[eg-2-38]
  122.  
  123. ```
  124. File: md5-example-4.py
  125.  
  126. import md5
  127. import array
  128.  
  129. class HMAC_MD5:
  130. # keyed md5 message authentication
  131.  
  132. def _ _init_ _(self, key):
  133. if len(key) > 64:
  134. key = md5.new(key).digest()
  135. ipad = array.array("B", [0x36] * 64)
  136. opad = array.array("B", [0x5C] * 64)
  137. for i in range(len(key)):
  138. ipad[i] = ipad[i] ^ ord(key[i])
  139. opad[i] = opad[i] ^ ord(key[i])
  140. self.ipad = md5.md5(ipad.tostring())
  141. self.opad = md5.md5(opad.tostring())
  142.  
  143. def digest(self, data):
  144. ipad = self.ipad.copy()
  145. opad = self.opad.copy()
  146. ipad.update(data)
  147. opad.update(ipad.digest())
  148. return opad.digest()
  149.  
  150. #
  151. # simulate server end
  152. # 模拟服务器端
  153.  
  154. key = "this should be a well-kept secret"
  155. message = open("samples/sample.txt").read()
  156.  
  157. signature = HMAC_MD5(key).digest(message)
  158.  
  159. # (send message and signature across a public network)
  160. # (经过由网络发送信息和签名)
  161.  
  162. #
  163. # simulate client end
  164. #模拟客户端
  165.  
  166. key = "this should be a well-kept secret"
  167.  
  168. client_signature = HMAC_MD5(key).digest(message)
  169.  
  170. if client_signature == signature:
  171. print "this is the original message:"
  172. print
  173. print message
  174. else:
  175. print "someone has modified the message!!!"
  176. ```
  177.  
  178. ``copy`` 方法会对这个内部对象状态做一个快照( snapshot ).
  179. 这允许你预先计算部分密文摘要(例如 [Example 2-38 #eg-2-38] 中的 padded key).
  180.  
  181. 该算法的细节请参阅
  182. //HMAC-MD5:Keyed-MD5 for Message Authentication//
  183. ( http://www.research.ibm.com/security/draft-ietf-ipsec-hmac-md5-00.txt ) by Krawczyk, 或其他.
  184.  
  185. *Note*千万别忘记内建的伪随机生成器对于加密操作而言并不合适. 千万小心. *note*

python标准库介绍——28 md5 模块详解的更多相关文章

  1. python标准库介绍——28 sha 模块详解

    ==sha 模块== ``sha`` 模块提供了计算信息摘要(密文)的另种方法, 如 [Example 2-39 #eg-2-39] 所示. 它与 ``md5`` 模块类似, 但生成的是 160 位签 ...

  2. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  3. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  4. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  5. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  6. python标准库介绍——18 StringIO 模块详解

    ==StringIO 模块== [Example 2-8 #eg-2-8] 展示了 ``StringIO`` 模块的使用. 它实现了一个工作在内存的文件对象 (内存文件). 在大多需要标准文件对象的地 ...

  7. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  8. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  9. python标准库介绍——23 UserString 模块详解

    ==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...

随机推荐

  1. RAID5工作原理介绍

    RAID 5是一种存储性能.数据安全和存储成本兼顾的存储解决方案.以四个硬盘组成的RAID 5为例,其数据存储方式如图4所示:图中,P0为D0,D1和D2的奇偶校验信息,P1为D3,D4,D5的奇偶校 ...

  2. 如何利用Framework模型生成IQD文件

    很多Cognos的新手在接触Transform建模的时候对于iqd文件都有一种朦胧的感觉,当然也不必去死记硬别它的格式,下面我们就来说一下如何用Framework工具来生成iqd文件. 1:打开fra ...

  3. myeclipse查询mysql出来的汉字是乱码

    乱码肯定是编码和解码方式不一致产生的! 1: 在mysql 中输入 status 可看到db 编码是什么         可选utf-8 or gbk 2:  连接时jdbc:mysql://serv ...

  4. mybatis如何根据mapper接口生成其实现类(springboot)

    序 mybatis里头给sqlSession指定执行哪条sql的时候,有两种方式,一种是写mapper的xml的namespace+statementId,如下: public Student fin ...

  5. [Node.js] Level 3 new. Steam

    File Read Stream Lets use the fs module to read a file and log its contents to the console. Use the  ...

  6. LESS详解之变量(@)

    变量基本上是每个语言脚本上都会涉及的一个小小知识点,是学好语言脚本的必经之路.LESS中也可以设置变量,然后通过变量可以改变整个网站的设计风格.良好的掌握LESS中变量的用法,是LESS的基础. 变量 ...

  7. JDK5.0特性,使用ProcessBuilder执行本地命令

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.IO ...

  8. java thread dump日志分析

    jstack Dump 日志文件中的线程状态 dump 文件里,值得关注的线程状态有: 死锁,Deadlock(重点关注)  执行中,Runnable 等待资源,Waiting on conditio ...

  9. 虚拟机里面做了个MySQLS主从:

    虚拟机里面做了个主从: 但是IO现成一直不能跟主库上的dump现成通信, Slave_IO_Running: No Last_IO_Error: error connecting to master ...

  10. 高阶函数 实现sum(2)(3) 柯里化

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...