python之hashlib模組

來源:互聯網
上載者:User

標籤: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模組

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.