python hashlib 模組

來源:互聯網
上載者:User

標籤:XA   AC   hashlib   bin   value   eal   pre   string   sage   

hashlib模組  用於加密相關的操作,3.x裡代替了md5模組和sha模組,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 演算法import hashlib m = hashlib.md5()m.update(b"Hello")m.update(b"It‘s me")print(m.digest())m.update(b"It‘s been a long time since last time we ...") print(m.digest()) #2進位格式hashprint(len(m.hexdigest())) #16進位格式hash‘‘‘def digest(self, *args, **kwargs): # real signature unknown    """ Return the digest value as a string of binary data. """    passdef hexdigest(self, *args, **kwargs): # real signature unknown    """ Return the digest value as a string of hexadecimal digits. """    pass‘‘‘import hashlib # ######## md5 ######## hash = hashlib.md5()hash.update(‘admin‘)print(hash.hexdigest()) # ######## sha1 ######## hash = hashlib.sha1()hash.update(‘admin‘)print(hash.hexdigest()) # ######## sha256 ######## hash = hashlib.sha256()hash.update(‘admin‘)print(hash.hexdigest())  # ######## sha384 ######## hash = hashlib.sha384()hash.update(‘admin‘)print(hash.hexdigest()) # ######## sha512 ######## hash = hashlib.sha512()hash.update(‘admin‘)print(hash.hexdigest())還不夠吊?python 還有一個 hmac 模組,它內部對我們建立 key 和 內容 再進行處理然後再加密散列訊息鑒別碼,簡稱HMAC,是一種基於訊息鑒別碼MAC(Message Authentication Code)的鑒別機制。使用HMAC時,訊息通訊的雙方,通過驗證訊息中加入的鑒別密鑰K來鑒別訊息的真偽;一般用於網路通訊中訊息加密,前提是雙方先要約定好key,就像接頭暗號一樣,然後訊息發送把用key把訊息加密,接收方用key + 訊息明文再加密,拿加密後的值 跟 寄件者的相對比是否相等,這樣就能驗證訊息的真實性,及寄件者的合法性了。 import hmach = hmac.new(b‘天王蓋地虎‘, b‘寶塔鎮河妖‘)print h.hexdigest()更多關於md5,sha1,sha256等介紹的文章看這裡https://www.tbs-certificates.co.uk/FAQ/en/sha256.html 

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.