溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python基于hashlib模塊中文件MD5一致性加密驗證怎么實現(xiàn)

發(fā)布時間:2021-08-03 12:31:21 來源:億速云 閱讀:154 作者:小新 欄目:開發(fā)技術

這篇文章主要為大家展示了“Python基于hashlib模塊中文件MD5一致性加密驗證怎么實現(xiàn)”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Python基于hashlib模塊中文件MD5一致性加密驗證怎么實現(xiàn)”這篇文章吧。

使用hashlib模塊,可對文件MD5一致性加密驗證:

#python 檢測文件MD5值
#python version 2.6
import hashlib
import os,sys
#簡單的測試一個字符串的MD5值
def GetStrMd5(src):
  m0=hashlib.md5()
  m0.update(src)
  print m0.hexdigest()
  pass
#大文件的MD5值
def GetFileMd5(filename):
  if not os.path.isfile(filename):
    return
  myhash = hashlib.md5()
  f = file(filename,'rb')
  while True:
    b = f.read(8096)
    if not b :
      break
    myhash.update(b)
  f.close()
  return myhash.hexdigest()
def CalcSha1(filepath):
  with open(filepath,'rb') as f:
    sha1obj = hashlib.sha1()
    sha1obj.update(f.read())
    hash = sha1obj.hexdigest()
    print(hash)
    return hash
def CalcMD5(filepath):
  with open(filepath,'rb') as f:
    md5obj = hashlib.md5()
    md5obj.update(f.read())
    hash = md5obj.hexdigest()
    print(hash)
    return hash
if __name__ == "__main__":
  if len(sys.argv)==2 :
    hashfile = sys.argv[1]
    if not os.path.exists(hashfile):
      hashfile = os.path.join(os.path.dirname(__file__),hashfile)
      if not os.path.exists(hashfile):
        print("cannot found file")
      else
        CalcMD5(hashfile)
    else:
      CalcMD5(hashfile)
      #raw_input("pause")
  else:
    print("no filename")

以上是“Python基于hashlib模塊中文件MD5一致性加密驗證怎么實現(xiàn)”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI