<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # hashlib.md5() `md5值` #### 一、hashlib概述 涉及加密服務:[Cryptographic Services][1] 其中 hashlib是涉及 `安全散列` 和 `消息摘要` ,提供多個不同的加密算法借口,如SHA1、SHA224、SHA256、SHA384、SHA512、MD5等。 ```python >>> import hashlib >>> m = hashlib.md5() #創建hash對象,md5:(message-Digest Algorithm 5)消息摘要算法,得出一個128位的密文 >>> m.update("Nobody inspects") #更新哈希對象以字符串參數 >>> m.update(" the spammish repetition") #更新哈希對象以字符串參數 >>> m.digest() #返回摘要,作為二進制數據字符串值 >>> m.hexdigest() #返回十六進制數字字符串 ``` #### 二、基本用法 ``` hashlib.md5('hiyang').hexdigest() '97b21efa4d5de4c1a6a2b332f1e183e5' ``` ``` >>> m = hashlib.md5() >>> m.update('hiyang') >>> m.hexdigest() '97b21efa4d5de4c1a6a2b332f1e183e5' ``` #### 三、使用new指定加密算法 new(name, string='') ``` hashlib.new('md5','hiyang').hexdigest() '97b21efa4d5de4c1a6a2b332f1e183e5' ``` ``` h = hashlib.new('md5') h.update('hiyang') hashlib.md5('hiyang').hexdigest() '97b21efa4d5de4c1a6a2b332f1e183e5' ``` #### 四、范例 > * md5sum() 計算文件的md5 > * md5sumd() 計算目錄下文件的md5 > * md5sumd2() 計算目錄下文件的md5,返回一個生成器 > * md5CheckFile() 通過md5比較文件內容是否相同 ```python #!/usr/bin/env python # python 2 # encoding:utf8 import hashlib import sys import os def md5sum(f, readsize=4096): """to calculate md5 of a file, readsize default is 4096 bytes return result""" m = hashlib.md5() with open(f, 'rb') as fd: data = fd.read(readsize) while data: m.update(data) data = fd.read(readsize) else: return m.hexdigest() def md5sumd(filename): """to calculate md5 of a directory return result""" _md5sumd = '' for paths, dirs, files in os.walk(filename): for f in files: fn = os.path.join(paths, f) _md5sumd += md5sum(fn)+' '+fn+'\n' return _md5sumd def md5sumd2(filename): """to calculate md5 of a directory return generator""" for paths, dirs, files in os.walk(filename): for f in files: fn = os.path.join(paths, f) yield md5sum(fn)+' '+fn def md5CheckFile(filename='.'): """calculate md5 of a directory, to check the same file return a directory {md5: [filename,...]}, these file has same md5""" dic = {} for paths, dirs, files in os.walk(filename): for f in files: fn = os.path.join(paths, f) md5 = md5sum(fn) if dic.has_key(md5): dic[md5].append(fn) else: dic[md5] = [fn] return {k: v for k, v in dic.items() if len(v) >= 2} if __name__ == '__main__': try: filename = sys.argv[1] except IndexError: filename = '.' if os.path.isfile(filename): print md5sum(filename)+' '+filename elif os.path.isdir(filename): _md5sumd = md5sumd(filename) print _md5sumd, print '-' * 20, 'total', '-' * 20 print hashlib.md5(_md5sumd).hexdigest()+' '+filename else: print >> sys.stderr, 'file %s is not exist.' % filename ``` [1]: https://docs.python.org/2.7/library/crypto.html
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看