標籤:update 字元 sse 一致性 color algorithm att 方式 dom
hashlib模組是python內建的摘要演算法。
hashlib有兩種調用方式:
第一種是hashlib.new方法
new(name, data=b‘‘, **kwargs) - returns a new hash object implementing the given hash function; initializing the hash using the given binary data.
import hashlibmd5obj = hashlib.new("md5", b‘alex3714‘)md5obj.hexdigest()
第二種是直接建立被允許的的演算法
Named constructor functions are also available, these are faster than using new(name):
md5(), sha1(), sha224(), sha256(), sha384(), sha512(), blake2b(), blake2s(), sha3_224, sha3_256, sha3_384, sha3_512, shake_128, and shake_256.
More algorithms may be available on your platform but the above are guaranteed to exist. See the algorithms_guaranteed and algorithms_available attributes to find out what algorithm names can be passed to new().
import hashlib# 將一個字串進行摘要運算,拿到一個固定的值;,包含了多中密碼編譯演算法md5obj = hashlib.md5() # 摘要md5obj.update(b‘alex‘) # 要加密的字串的位元組md5obj.update(b"3714") # 要加密的字串的位元組# 多次update是拼接到一起的,再進行hexdigest(),結果和一次update的時一樣的# md5obj.update(b‘alex3714)md5obj.hexdigest() #轉成16進位,加密結果是32位
使用者名稱與密碼加密:
名詞:撞庫,根據輸入和摘要演算法的結果來驗證輸入與儲存的密碼編譯演算法結果的一致性。
加鹽,通過加入隨機字串來防止撞庫。
對使用者名稱和密碼加密,通常會進行加鹽:
1.根據使用者名稱產生隨機字串
2.將該隨機字串與密碼拼接在一起,進行摘要演算法,得到最後的密碼加密結果
import hashlib, randoml1 = list(range(48, 58))l2 = list(range(65, 91))l3 = list(range(97, 123))l1.extend(l2)l1.extend(l3)username = "alex"passwd = "alex3714"# 根據使用者名稱產生隨機字串lis = ""for i in range(16): v = random.randint(0, len(l1)-1) lis += chr(l1[v])passwd += lismd5 = hashlib.new("md5", bytes(passwd, encoding="utf8"))passwd = md5.hexdigest()user = dict( username=username, user_string=lis, passwd=passwd,)print(user)# {‘username‘: ‘alex‘, ‘user_string‘: ‘MnkCViH3avdDYp3U‘, ‘passwd‘: ‘35a5a0e38ea22720a78d594d8f02a021‘}
python之hashlib模組