<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python 程序:查找文件哈希 > 原文: [https://www.programiz.com/python-programming/examples/hash-file](https://www.programiz.com/python-programming/examples/hash-file) #### 在本文中,您將學習查找文件的哈希并顯示它。 要理解此示例,您應該了解以下 [Python 編程](/python-programming "Python tutorial")主題: * [Python 函數](/python-programming/function) * [Python 用戶定義函數](/python-programming/user-defined-function) * [Python 文件 I/O](/python-programming/file-operation) * * * 散列函數獲取任意數量的數據,并返回固定長度的位字符串。 函數的輸出稱為摘要消息。 它們被廣泛用于密碼學中以進行認證。 有許多哈希函數,例如 MD5,SHA-1 等。請參閱此頁面,以了解有關密碼學中的[哈希函數的更多信息。](http://en.wikipedia.org/wiki/Cryptographic_hash_function "Hash Function") 在此示例中,我們將說明如何對文件進行哈希處理。 我們將使用 SHA-1 哈希算法。 SHA-1 的摘要的長度為 160 位。 我們不會一次全部提供文件中的數據,因為某些文件非常大,無法一次全部放入內存中。 將文件分成小塊將提高進程內存的效率。 ## 查找哈希的源代碼 ```py # Python rogram to find the SHA-1 message digest of a file # importing the hashlib module import hashlib def hash_file(filename): """"This function returns the SHA-1 hash of the file passed into it""" # make a hash object h = hashlib.sha1() # open file for reading in binary mode with open(filename,'rb') as file: # loop till the end of the file chunk = 0 while chunk != b'': # read only 1024 bytes at a time chunk = file.read(1024) h.update(chunk) # return the hex representation of digest return h.hexdigest() message = hash_file("track1.mp3") print(message) ``` **輸出** ```py 633d7356947eec543c50b76a1852f92427f4dca9 ``` 在此程序中,我們以二進制模式打開文件。 哈希函數在`hashlib`模塊中可用。 我們使用`while`循環循環到文件末尾。 到達最后時,我們得到空字節對象。 在每次迭代中,我們僅從文件中讀取 1024 個字節(可以根據需要更改此值),并更新哈希函數。 最后,我們使用`hexdigest()`方法以十六進制表示形式返回摘要消息。
                  <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>

                              哎呀哎呀视频在线观看