Hashlib
Hashlib mainly provides character encryption function, integrates MD5 and SHA modules together, supports MD5,SHA1, sha224, sha256, sha384, sha512 and other algorithms
Specific applications
#!/usr/bin/env python#-*-Coding:utf-8-*-#pyversion:python3.5#Owner:fuzjImportHashlib######### MD5 ########String ="Beyongjie"MD5 =HASHLIB.MD5 () md5.update (String.encode (‘Utf-8‘))#Note transcoding res =Md5.hexdigest ()Print"MD5 encryption Results:", res)######### SHA1 ########SHA1 =HASHLIB.SHA1 () sha1.update (String.encode (‘Utf-8‘)) res =Sha1.hexdigest ()Print"SHA1 Encryption Results:", res)######### sha256 ########SHA256 =hashlib.sha256 () sha256.update (String.encode (‘Utf-8‘)) res =Sha256.hexdigest ()Print"SHA256 Encryption Results:", res)######### sha384 ########sha384 =hashlib.sha384 () sha384.update (String.encode ('utf-8') res = sha384.hexdigest ()print ("sha384 encryption Result:", Res) # # # # # # # # # # # # # # # # # # sha512 ########sha512= hashlib.sha512 () sha512.update (String.encode ('utf-8')) res = sha512.hexdigest ()print ("sha512 encryption Result:", res)
Output Result:
MD5 encryption Result: 0E725E477851FF4076F774DC312D4748SHA1 encryption Result: 458d32be8ea38b66300174970ab0a8c0b734252fsha256 encryption Result: 1e62b55bfd02977943f885f6a0998af7cc9cfb95c8ac4a9f30ecccb7c05ec9f4sha384 Encryption Results: E91CDF0D2570DE5C96EE84E8A12CDDF16508685E7A03B3E811099CFCD54B7F52183E20197CFF7C07F312157F0BA4875BSHA512 Encryption Results: 3f0020a726e9c1cb5d22290c967f3dd1bcecb409a51a8088db520750c876aaec3f17a70d7981cd575ed4b89471f743f3f24a146a39d59f215ae3e208d 0170073
Note: Hashlib encrypted string type is binary encoded, the direct encryption string will report the following error:
SHA1 = hashlib.sha1 () sha1.update (string) res = sha1.hexdigest ()print ("SHA1 encryption Result:", Res) Typeerror:unicode-objects must be encoded before hashing
Can be converted using encode
Shaa1 = hashlib.sha1 () shaa1.update (String.encode ('utf-8')) res = shaa1.hexdigest () Print ("SHA1 with encode conversion encryption result:", res)
or use byte to convert to binary
SHAB1 = hashlib.sha1 () shab1.update (bytes (string,encoding='utf-8')) res = Shab1.hexdigest () print ("SHA1 results with byte conversion:", res)
Above output:
SHA1 uses the Encode conversion encryption result: 458D32BE8EA38B66300174970AB0A8C0B734252FSHA1 uses the result of byte conversion: 458d32be8ea38b66300174970ab0a8c0b734252f
Common methods
- Hash.update (ARG) updates the hash object with a string parameter, note: If the same hash object repeats the method, then M.update (a); M.update (b) is equivalent to M.update (A+B), see the following example
m =HASHLIB.MD5 () M.update (‘A'. Encode (‘Utf-8‘)) res =M.hexdigest ()Print"First-time a encryption:", res) M.update (‘B'. Encode (‘Utf-8‘)) res =M.hexdigest ()Print"Second B encryption:", RES) M1 =HASHLIB.MD5 () M1.update (‘B'. Encode (‘Utf-8 ' M1.hexdigest () print (" b separate encryption: ",res) m2 = Hashlib.md5 () m2.update ( ab encode ( ' Utf-8 ' M2.hexdigest () print ( "ab separate encryption: " Span class= "Cye-lm-tag" >,res) output: First a encryption: 0cc175b9c0f1b6a831c399e269772661 second B encryption: 187ef4436122d1cc2f40dc2b92f0eba0b separate encryption: 92eb5ffee6ae2fec3ad71c777531578fab separate encryption: 187EF4436122D1CC2F40DC2B92F0EBA0
Hash.digest () returns a digest, as a binary data string value,
Hash.hexdigest () returns a digest, as a hexadecimal data string value,
Hash.copy () copy
Advanced Encryption
Although the above encryption algorithm is still very strong, but the time has the flaw, namely: through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.
Low =HASHLIB.MD5 () Low.update (‘Ab'. Encode (‘Utf-8‘)) res = Low.hexdigest () print ( " Ordinary encryption: " ,res) high = HASHLIB.MD5 (b beyondjie ' ) high.update ( "ab Encode ( ' utf-8 ' )) Res = High.hexdigest () print ( " with key encryption: ",res" output: Normal encryption: 187EF4436122D1CC2F40DC2B92F0EBA0 with key encryption: 1b073f6b8cffe609751e4c98537b7653
Additional HMAC-SHA1 for each language version
In the big open platform of the Internet development trend of the road, call the Platform API interface process, no exception will be used to calculate the signature value (sig value). And in the various methods of calculating signatures, is often used is HMAC-SHA1, now on hmac-SHA1 to do a simple introduction: HMAC, Hash message authentication code, based on the key hash algorithm authentication protocol. The implementation principle is: using the already public hash function and the private key, to generate a fixed-length message authentication code; SHA1, MD5 and other hash algorithms are more commonly used in the calculation of irreversible hash signature; BASE64, converts 8-byte characters of any sequence to a character that cannot be directly recognized by the human eye The implementation of each language version is: Python version:ImportHmacImportHashlibImportBase64 Hmac.new (TOKEN,DATA,HASHLIB.SHA1). Digest (). Encode (base64 ' ). Rstrip () Token: The Keydata of the interface: data to encrypt PHP version: Base64_encode (Hash_hmac (sha1 ",clientstr,token, True) C++< Span class= "Cye-lm-tag" > version (OPENSSL): HMAC (EVP_SHA1 (),/*key data*/ Strkey.data (),/*key len*/ Strkey.size (),/*data */(unsigned char*digest_len)) Shell version: Echo-n 3f88a95c532bea70 ' | OpenSSL dgst-hmac ' 123 '-sha1-binary | base64
Python hashlib module