One, HASH
HASH-- 'hash' the input of arbitrary length value, generating a hash value of a fixed length.
Note: hash value generated when the python program of this operation has been, the next time the program will generate another hash value calculation.
Two, MD5
MD5 message digest algorithm
2.1 features:
1, of any length, algorithm, a fixed length output 128 through [image outside the chain dump value (digital fingerprint) failure.
2, different input, different results (Uniqueness)
2.2 Features:
1, compressibility: enter any, MD5 value fixed length
2, it is easy to calculate: the original data can be easily calculated from the value of MD5.
3, anti-Modify: modify a little bit, MD5 value calculated very different.
4, strong collision: a known MD5 value, to the same value by calculating other values is difficult.
2.3 Are reversible?
MD5 hash algorithm is based, in the calculation process the original data is lost, it is irreversible.
2.4 What is the use?
1. prevent tampering
2. Preventing see the plaintext
3. Digital Signature
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
# @File : 4.10.hash加密算法.py
# @Software: PyCharm
import hashlib
# 1.将字符串,通过加密算法,变成固定长度的输出
s = 'abc'
print(len(str(hash(s)))*4, hash(s))
# 2.生成md5数字指纹。
# 第1种写法:
s2 = b'!@abc' # 定义字节型字符串
md = hashlib.md5() # 导入md5算法
md.update(s2) # 把值传给md5算法
print(md.digest()) # 生成一个128位的2进制数
print('MD5', '长度:', len(md.hexdigest())*4, md.hexdigest())
# 第2种写法:
print(hashlib.md5("!@abc".encode("utf-8")).hexdigest())
# 3.SHA-1
hash = hashlib.sha1()
hash.update('admin'.encode('utf-8'))
print('SHA-1', '长度:', len(hash.hexdigest())*4, hash.hexdigest())
# 4.SHA-256
hash = hashlib.sha256()
hash.update('admin'.encode('utf-8'))
print('SHA-256', '长度:', len(hash.hexdigest())*4, hash.hexdigest())
# 5.SHA-384
hash = hashlib.sha384()
hash.update('admin'.encode('utf-8'))
print('SHA-384', '长度:', len(hash.hexdigest())*4, hash.hexdigest())
# 5.SHA-512
hash = hashlib.sha512()
hash.update('admin'.encode('utf-8'))
print('SHA-384', '长度:', len(hash.hexdigest())*4, hash.hexdigest())