1. 程式人生 > 其它 >MD5和SHA1演算法的理解

MD5和SHA1演算法的理解

技術標籤:pythonpythonmd5

1、介紹


2、演算法

# @Time : 2021/2/10 14:51
# @Description : (Message Digest Algorithm)MD5和(Secure Hash Algorithm)SHA1演算法測試

import hashlib

if __name__ == "__main__":
    print('------------------------------MD5演算法----------------------------------')
    md5 = hashlib.md5()
md5.update('This is a sentence.'.encode("UTF-8")) md5.update('This is a second sentence.'.encode("UTF-8")) # 不出意外,這個將是“亂碼”: b'+\xfe\x14I\x16QUL\x05\x99k\x16)$\x85q' print('不出意外,這個將是“亂碼”:', md5.digest()) # 轉化為十六進位制後輸出 # MD5: 2bfe14491651554c05996b1629248571 print
('MD5:', md5.hexdigest()) md5 = hashlib.md5() md5.update('This is a sentence.This is a second sentence.'.encode("UTF-8")) # MD5: 2bfe14491651554c05996b1629248571 print('MD5:', md5.hexdigest()) # 輸出一些細節資訊 # 16 64 print(md5.digest_size, md5.block_size) print(
'------------------------------SHA1演算法----------------------------------') sha1 = hashlib.sha1() sha1.update('This is a sentence.'.encode("UTF-8")) sha1.update('This is a second sentence.'.encode("UTF-8")) # 不出意外,這個將是“亂碼”: b'\x99\xa7JH\xd5\xfba\x1ae%\x96+\xa3\xd5\x111\xcb:\x1d,' print('不出意外,這個將是“亂碼”:', sha1.digest()) # SHA1: 99a74a48d5fb611a6525962ba3d51131cb3a1d2c print('SHA1:', sha1.hexdigest()) sha1 = hashlib.sha1() sha1.update('This is a sentence.This is a second sentence.'.encode("UTF-8")) # SHA1: 99a74a48d5fb611a6525962ba3d51131cb3a1d2c print('SHA1:', sha1.hexdigest()) # 20 64 print(sha1.digest_size, sha1.block_size) print('=====================') md5 = hashlib.new('md5', 'This is a sentence.This is a second sentence.'.encode("UTF-8")) # 2bfe14491651554c05996b1629248571 print(md5.hexdigest()) sha1 = hashlib.new('sha1', 'This is a sentence.This is a second sentence.'.encode("UTF-8")) # 99a74a48d5fb611a6525962ba3d51131cb3a1d2c print(sha1.hexdigest()) # print(hashlib.algorithms) print(hashlib.algorithms_available)