Python3 Encryption (HASHLIB/HMAC)
This article is written by Luzhuo and is forwarded please keep this information.
Original: http://blog.csdn.net/Rozol/article/details/72566661
The following code takes Python3.6.1 as an example
Less is more!
Hashlib: Non-reversible encryption
HMAC: Irreversible key-value alignment encryption
Base64: Reversible encryption Hashlib
#!/usr/bin/env Python # coding=utf-8 __author__ = ' Luzhuo ' __date__ = ' 2017/5/19 ' # hash_demo.py hash encryption related (secure hash) # Support: MD5, SHA1 SHA224 SHA256 SHA384 SHA512 import hashlib def hash_demo (): M = Hashlib.md5 () m.update (b "Hello") m.u Pdate (b "world!")
# = Hello + world! Hash_hex = hashlib.sha3_512 (b "luzhuo.me"). Hexdigest () print (m.digest_size) print (M.digest ()) # binary hash Print (M.hexdigest ()) # hex hash Print (hash_hex) # add salt encryption hash_bytes = Hashlib.pbkdf2_hmac (' sha256 ', b ' luzhuo.me ', b ' 100000 ', print (hash_bytes) def hash_func (): # hashlib.new (name[, data])//Create HASHLIB (not preferred), Name= algorithm name, data : Data hash = hashlib.new (' ripemd160 ', b ' luzhuo.me ') # constant dics = hashlib.algorithms_guaranteed # All platform supported hash algorithm Name dics = hashlib.algorithms_available # The name of the hash algorithm available in the Python parser, which is passed to new () to recognize the # Hashlib.pbkdf2_hmac (hash_name , password, salt, iterations, Dklen=none)//Add salt to encrypt hash_name:hash name, password: data, salt: salt, iterations: Cycle times, Dklen: Key Length hash_bytes = Hashlib.pbkdf2_hmac (' sha256 ', b ' luzhuo.me ', b ', 100000 ') # hash Object num = Hash.dige St_size # hash result size num = hash.block_size # hash Algorithm's internal block size STRs = hash.name # hash name, can be passed to new () using Hash.update (b "Data") # Byte buffer Hash.update (a) hash.update (b) = = Hash.update (a+b) hash_bytes = Hash.digest () # byte hash hash_str
= Hash.hexdigest () # 16 String hash = Hash.copy () # Copy Hash object copy if __name__ = "__main__": Hash_demo ()
# Hash_func ()
HMAC
#!/usr/bin/env Python # coding=utf-8 __author__ = ' Luzhuo ' __date__ = ' 2017/5/19 ' # hmac_demo.py HMAC algorithm # differs from Hashlib in many ways
Key Import HMAC def hmac_demo (): # Encrypt H = hmac.new (b "NET") H.update (b "luzhuo.me") H_str = H.hexdigest () Print (H_STR) # comparison Password Boolean = Hmac.compare_digest (H_str, hmac.new (b "NET", B "luzhuo.me"). Hexdigest ()) PRI NT (Boolean) def Hmac_func (): # Create key and content, then encrypt # hmac.new (key, Msg=none, Digestmod=none)//Create new HMAC object, key: Key, Msg:update (msg), Digestmod:hash name (same as Hashlib.new ()) (default MD5) HC = Hmac.new (b "key") # HMAC Object Hc.update (b "msg") # BYTE buffer Hc.update (a) hc.update (b) = = Hc.update (a+b) hash_bytes = Hc.digest () # byte hash hash_str = Hc.hexdigest () #
16 binary Hash String HC = Hc.copy () # copy HMAC replica num = hc.digest_size # hash Size num = hc.block_size # hash algorithm internal block size STRs = hc.name # hash name # hmac.compare_digest (A, B)//Compare two hash keys are the same, parameters can be: Str/bytes-like object, (note: Recommended use, do not recommend the use of a= =b) Boolean = hmac.cOmpare_digest (hmac.new (b "NET", B "luzhuo.me"). Digest (), hmac.new (b "NET", B "luzhuo.me"). Digest ()) If __name__ = "__mai
N__ ": Hmac_demo () # Hmac_func ()