Python module-Hashlib

Source: Internet
Author: User
Tags base64 hmac md5 encryption openssl openssl library sha1 sha1 encryption

Hashlib

The Hashlib module in Python is used for hashing or MD5 encryption, and this encryption is irreversible, so this algorithm is called the Digest algorithm. It supports all the algorithms provided by the OpenSSL library, including MD5, SHA1, sha224, SHA256, sha512, and so on.

Common Properties and methods:

Algorithms: List all cryptographic algorithms (' MD5 ', ' SHA1 ', ' sha224 ', ' sha256 ', ' sha384 ', ' sha512 ')

Digesti_size: The size of the resulting hash bytes

MD5 ()/SHA1 (): Create a Hash object that MD5 or SHA1 encryption mode

Update (ARG): The hash object is updated with a string parameter, if the method is called repeatedly by the same has object, as follows: M.update (a); M.update (b) is equal to M.update (A+B)

Digest (): Returns the digest as a binary data string value

Hexdigest (): Returns the digest as a hexadecimal data string value

Copy (): Copy

Specific applications
#!/usr/bin/env python#-*-coding:utf-8-*-import hashlib# ######## MD5 ####### #string = "Beyongjie" MD5 = HASHLIB.MD5 () MD 5.update (String.encode (' Utf-8 '))     #注意转码res = Md5.hexdigest () print ("MD5 encryption Result:", res) # ######## SHA1 ####### #sha1 = HASHLIB.SHA1 () sha1.update (String.encode (' Utf-8 ')) res = Sha1.hexdigest () print ("SHA1 encryption Result:", res) # ######## sha256 # # # # # # # #sha256 = hashlib.sha256 () sha256.update (String.encode (' utf-8 ') res = Sha256.hexdigest () print ("SHA256 encryption Result:", 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 convert encrypted result with encode:", 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 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 ')) res = M1.hexdigest () print (" b Separate encryption: ", res) m2 = hashlib.md5 () m2.update (' ab '. Encode (' Utf-8 ')) res = M2.hexdigest () print (" AB Separate encryption: ", 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 ("Normal encryption:", res) high = HASHLIB.MD5 (b ') Beyondjie ') high.update (' Ab ' Encode (' utf-8 ') res = High.hexdigest () print ("Encrypted with key:", res) output: Normal encryption: 187EF4436122D1CC2F40DC2B92F0EBA0 with key encryption: 1b073f6b8cffe609751e4c98537b7653

Additional HMAC-SHA1 for each language version
 in the major open platform of the Internet development trend, the call 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, is now a simple introduction to HMAC-SHA1: HMAC, hash message identification code, based on the cryptographic 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: Import HMAC import hashlib import base              Hmac.new (TOKEN,DATA,HASHLIB.SHA1). Digest (). Encode (' base64 '). Rstrip () Token: The Keydata of the interface: PHP version of the data to encrypt: Base64_encode (Hash_hmac ("SHA1", Clientstr,token, True)) C + + (OPENSSL): HMAC (EVP_SHA1  (),/*key data*/strkey.data (),/*key len*/strkey.size (),/*data */(unsigned char*) strrandom.data (),/*data len*/strrandom.size (), Digest, &digest_len)) Shel L Version: Echo-n ' 3f88a95c532bea70 ' | OpenSSL Dgst-hmac ' 123 '-sha1-binary | Base64 

Python module-hashlib

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.