Python hashlib模組

來源:互聯網
上載者:User

標籤:enc   3.x   int   pre   strong   class   sha   hello   encode   

hashlib模組

 

 

一.hashlib模組

用於加密相關的操作,3.x力代替了md5模組和sha模組,主要提供

:SHA1,SHA224,SHA256,SHA384,SHA512,MD5演算法。

 

1.使用hashlib模組進行MD5加密。

import hashlibm = hashlib.md5()m.update(b"Hello")m.update(b"It‘s me")print(m.hexdigest())m.update(b"It‘s been a long time since last time we ...")print(m.digest())

註:hashlib.md5():建立一個md5加密參數。

註:變數.hexdigest():十六進位加密。

註:變數.digest():二進位加密

註:總共有32個值。

 

 

2.使用SHA1演算法加密。

import hashlibs2 = hashlib.sha1()s2.update(b"abc")print(s2.hexdigest())

註:總共有38個值。

 

 

3.使用SHA256演算法加密

s2 = hashlib.sha256()s2.update(b"abc")print(s2.hexdigest())print(len(s2.hexdigest()))

註:有64個加密數值。

 

4.使用SHA384演算法加密。

import hashlibs2 = hashlib.sha384()s2.update(b"abc")print(s2.hexdigest())print(len(s2.hexdigest()))

註:有96個加密數值。

 

5.使用SHA512演算法加密。

import hashlibs2 = hashlib.sha512()s2.update(b"abc")print(s2.hexdigest())print(len(s2.hexdigest()))

註:有128個加密數值。

 

 

二.密碼編譯演算法進階

import hmach = hmac.new(‘wueiqi‘)h.update(‘hellowo‘)print h.hexdigest()

註:相當於加密後又進行一層加密。雙層加密。

import hmach = hmac.new(b"xsk","y風的風x".encode(encoding="utf-8"))print(h.digest())print(h.hexdigest())

註:雙層加密。

註:加入中文key需要,轉換字元類型。

 

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.