Python 學習之路 - 反射,hashlib

來源:互聯網
上載者:User

標籤:pat   class   att   通過   log   os.path   學習   upd   style   

反射
 1 import os 2 mo = "common" 3 # path = os.path.dirname(os.path.abspath(__file__)) 4 mod = __import__("other."+ mo,fromlist=True)#反射的方式匯入一個模組,other.是模組的目錄,mo是模組 5  6  7 # setattr() 在某個模組設定某個功能 8 # delattr() 在某個模組刪除某個功能 9 10 def fun():11     p = input("Please enter operate:")12 13     if hasattr(mod,p):# hasattr() 在某個模組中某個功能是否存在14         a = getattr(mod,p)# getattr() 在某個模組中得到某個功能15         a()16     else:17         print("404")18 19 20 if __name__ == ‘__main__‘:21     fun()
 hashlib

用於加密相關的操作,代替了md5模組和sha模組,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 演算法

 1 import hashlib 2   3 # ######## md5 ######## 4   5 hash = hashlib.md5() 6 hash.update(b‘admin‘) 7 print(hash.hexdigest())#hexdigest 16進位 8   9 # ######## sha1 ########10  11 hash = hashlib.sha1()12 hash.update(b‘admin‘)13 print hash.hexdigest()14  15 # ######## sha256 ########16  17 hash = hashlib.sha256()#比md5安全18 hash.update(b‘admin‘)19 print(hash.hexdigest())20  21  22 # ######## sha384 ########23  24 hash = hashlib.sha384()25 hash.update(b‘admin‘)26 print(hash.hexdigest())27  28 # ######## sha512 ########29  30 hash = hashlib.sha512()31 hash.update(b‘admin‘)32 print(hash.hexdigest())

以上密碼編譯演算法雖然依然非常厲害,但時候存在缺陷,即:通過撞庫可以反解。所以,有必要對密碼編譯演算法中添加自訂key再來做加密。

1 import hashlib2  3 # ######## md5 ########4  5 hash = hashlib.md5(b‘898oaFs09f‘)6 hash.update(b‘admin‘)7 print(hash.hexdigest())

python 還有一個 hmac 模組,它內部對我們建立 key 和 內容 再進行處理然後再加密,常用於網路訊息的加密傳輸。

1 import hmac2 h = hmac.new(b‘xxx‘)3 h.update(b‘hellowo‘)4 print(h.hexdigest())

 

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.