Implementation of the Python3 encryption (hashlib and hmac) module, python3hashlib

Source: Internet
Author: User
Tags hmac

Implementation of the Python3 encryption (hashlib and hmac) module, python3hashlib

The following code uses Python3.6.1 as an example:

  1. Hashlib: irreversible encryption
  2. Hmac: Irreversible key-Value Pair Encryption

Introduction to the hashlib module:

The hashlib module implements a common and common interface for different Secure Hash Algorithm and Message Digest algorithms, it can also be said to be a unified entrance. The hashlib module not only integrates the functions of the md5 and sha modules, but also provides function implementation for more medium algorithms, such as MD5, SHA1, shaloud, SHA256, SHA384, and SHA512.

Steps for using the hashlib module:

1) obtain the hash object corresponding to a hash algorithm (for example, the name is hash): You can use hashlib. new (hash algorithm name, initial entry/exit information) function to obtain the hash object, such as hashlib. new ('md5', 'Hello'), hashlib. new ('sha1', 'Hello'), etc. You can also use hashlib. hash algorithm name () to obtain the hash object, such as hashlib. md5 (), hashlib. sha1.

2) Set/append input information: You can call the update (input information) method of the hash object to set or append the input information. This method is called multiple times, it is equivalent to using each passed parameter as a parameter to the update () method. That is to say, multiple calls are accumulated rather than overwritten.

3) obtain the abstract of the string parameter passed to the update () method by calling the digest () method or hexdigest () method of the obtained hash object. The digest information returned by the digest () method is a binary string that may contain non-ASCII characters, including NUL bytes. the string length can be obtained through the digest_size attribute of the hash object; the digest information returned by the hexdigest () method is a string in hexadecimal format. The string contains only hexadecimal numbers and the length is digest () the returned result is twice the length, which can be used for secure interaction between emails or other non-binary environments.

#! /Usr/bin/env python # coding = UTF-8 _ author _ = 'luzhuo' _ date _ = '2017/19' # hash_demo.py Hash encryption (Secure Hash) # supported: MD5, SHA1 sha1_sha256 SHA384 SHA512import hashlibdef hash_demo (): m = hashlib. md5 () m. update (B "hello") m. update (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 () # hexadecimal hash print (hash_hex) # add salt to encrypt hash_bytes = hashlib. pbkdf2_hmac ('sha256 ', B' luzhuo. me ', B '80', 100000) print (hash_bytes) def hash_func (): # hashlib. new (name [, data]) // create hashlib (not preferred), name = algorithm name, data: data hash = hashlib. new ('ripmd160 ', B' luzhuo. me ') # constant dics = hashlib. algorithms_guaranteed # Name of the hash algorithm supported by all platforms dics = hashlib. algorithms_available # Name of the hash algorithm available in the Python parser. It can be recognized when it is passed to new (). # hashlib. pbkdf2_hmac (hash_name, password, salt, iterations, dklen = None) // Add salt to encrypt hash_name: hash name, password: data, salt: salt, iterations: cycles, dklen: key Length hash_bytes = hashlib. pbkdf2_hmac ('sha256 ', B' luzhuo. me ', B '80', 100000) # hash object num = hash. digest_size # hash result size num = hash. block_size # size of the internal block of the hash algorithm strs = hash. name # hash name, which can be passed to new () for hash. update (B "data") # hash of the byte buffer. update (a) hash. update (B) = hash. update (a + B) hash_bytes = hash. digest () # byte hash hash_str = hash. hexdigest () # hexadecimal string hash = hash. copy () # copy a copy of the hash object if _ name _ = "_ main _": hash_demo () # hash_func ()

Introduction to the hashmac module:

As mentioned above, the HMAC algorithm is also a single encryption algorithm based on various hash algorithms and hash algorithms, it can only use a key to increase security during the operation. The hmac module implements the HAMC algorithm, provides corresponding functions and methods, and is basically consistent with the APIS provided by hashlib.

Steps for using the hmac module:

The steps for using the hmac module are basically the same as those for the hashlib module. Only hmac can be used to obtain hmac objects in step 1. new () function, because the hmac module does not provide a function corresponding to a specific hash algorithm to obtain hmac objects.

#! /Usr/bin/env python # coding = UTF-8 _ author _ = 'luzhuo' _ date _ = '2017/19' # hmac_demo.py HMAC algorithm # different from hashlib keyimport hmacdef hmac_demo (): # encryption h = hmac. new (B "net") h. update (B "luzhuo. me ") h_str = h. hexdigest () print (h_str) # Compare password boolean = hmac. compare_digest (h_str, hmac. new (B "net", B "luzhuo. me "). hexdigest () print (boolean) def hmac_func (): # create key and content, and then encrypt them all # hmac. new (key, msg = None, digestmod = None) // create a new hmac object, key: key, msg: update (msg), digestmod: hash name (same as hashlib. new () (md5 by default) hc = hmac. new (B "key") # hmac object hc. update (B "msg") # bytes buffer hc. update (a) hc. update (B) = hc. update (a + B) hash_bytes = hc. digest () # byte hash hash_str = hc. hexdigest () # hexadecimal hash string hc = hc. copy () # copy hmac copy num = hc. digest_size # hash size num = hc. block_size # size of the internal block of the hash algorithm strs = hc. name # hash name # hmac. compare_digest (a, B) // compare whether the two hash keys are the same. The parameter can be str/bytes-like object. (Note: We recommend that you use this parameter. a = B is not recommended) boolean = hmac. compare_digest (hmac. new (B "net", B "luzhuo. me "). digest (), hmac. new (B "net", B "luzhuo. me "). digest () if _ name _ = "_ main _": hmac_demo () # hmac_func ()

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.