hashlib - hashlib module - A common interface to many hash functions
hashlib模块是一个很多hash函数的通用接口,提供了md5, sha1, sha224, sha256, sha384, sha512等加密算法,主要用来加密。
############# md5 ############# >>> import hashlib >>> m = hashlib.md5() >>> m.update(bytes('passwd', encoding='utf-8')) >>> m.hexdigest() '76a2173be6393254e72ffa4d6df1030a' ############# sha1 ############# >>> m = hashlib.sha1() >>> m.update(bytes('passwd', encoding='utf-8')) >>> m.hexdigest() '30274c47903bd1bac7633bbf09743149ebab805f' ############# sha256 ############# >>> m = hashlib.sha256() >>> m.update(bytes('passwd', encoding='utf-8')) >>> m.hexdigest() '0d6be69b264717f2dd33652e212b173104b4a647b7c11ae72e9885f11cd312fb' ############# sha384 ############# >>> m = hashlib.sha384() >>> m.update(bytes('passwd', encoding='utf-8')) >>> m.hexdigest() '851fab8375c23d5749c028b18f2844e754c30af17a11df995ea4faa8743ea67bb0ebf920332d9473d78bbdf9f47657ca' ############# sha512 ############# >>> m = hashlib.sha512() >>> m.update(bytes('passwd', encoding='utf-8')) >>> m.hexdigest() 'bf98889d70675da7dc753cdf8ac78c49cdcced21919dce21b83dc8c81607e4eaf719aa5f9bc09bbe316d8e98f663c607cfc6eb4337eaa41601cd9bc303c5f583'
写一个封装md5加密的函数
import hashlib def _encrypt(field): m = hashlib.md5() m.update(bytes(field, encoding='utf-8')) return m.hexdigest()