md5 encryption instance in python3

  • 2020-10-23 21:03:51
  • OfStack

In the standard library of python3, md5 has been removed, and the encryption algorithms of hash are all put in the standard library of hashlib, such as SHA1, SHA224, SHA256, SHA384, SHA512 and MD5.

Also: I searched md5 encryption of python on the Internet, and found that it was not the old article that did not apply to the current VERSION of py, or the article that was not clear enough, so It was better to read the official document by myself and sort out the usage of md5 by the way.

For learning any program knowledge, I think reading the official documents is one of the most effective learning methods, but generally these documents are in English. For some learners, there may be a certain threshold, but it is very important to get used to reading English articles.

It is recommended to read the hashlib document of python3 directly:

https://docs.python.org/3/library/hashlib.html?highlight=hashlib#credits

In the hash algorithm of hashlib library, many encryption algorithms are provided, including sha1(), sha224(), sha256(), sha384(), sha512(), blake2b() and blake2s(), md5(). These methods all return one object through unified interface. For example, using sha256(), one ES42en-256 hash object can be created.

Of course, for md5 encryption algorithm, md5() method is used:


>>> import hashlib
>>> m = hashlib.md5()
>>> m.update(b'123')
>>> m.hexdigest()
'202cb962ac59075b964b07152d234b70'

#  Or you could do that 
>>> hashlib.md5(b'123').hexdigest()
'202cb962ac59075b964b07152d234b70'

#  You can also use hash.new() this 1 A method of 
>>> hashlib.new('md5', b'123').hexdigest()
'202cb962ac59075b964b07152d234b70'

The above is md5 encryption for English. If you want to encrypt Chinese, you will get an error if you write it as above, because of the character transcoding problem. It should be written as follows:


>>> import hashlib
>>> data = ' hello '
>>> hashlib.md5(data.encode(encoding='UTF-8')).hexdigest()
'7eca689f0d3389d9dea66ae112e5cfd7'

Here, the data is converted into UTF-8 format first. Using the online tools to compare the encryption results, it is found that some md5 encryption tools do not use ES56en-8 format encryption.

Tested can be converted to find UTF - 8, GBK, GB2312, GB18030, case insensitive (because GBK GB2312 / GB18030 all is according to Chinese character coding, so after md5 encryption result 1 sample).

In addition to these coding formats, there will be other coding, so far have not found, waiting for you to add.

Take the following example:


>>> hashlib.md5(' hello '.encode(encoding='UTF-8')).hexdigest()
'7eca689f0d3389d9dea66ae112e5cfd7'

>>> hashlib.md5(' hello '.encode(encoding='GBK')).hexdigest()
'b94ae3c6d892b29cf48d9bea819b27b9'

>>> hashlib.md5(' hello '.encode(encoding='GB2312')).hexdigest()
'b94ae3c6d892b29cf48d9bea819b27b9'

>>> hashlib.md5(' hello '.encode(encoding='GB18030')).hexdigest()
'b94ae3c6d892b29cf48d9bea819b27b9'


If you just look at the way md5 is written, look at the example above.

If you are new to python and want to know what these methods mean and how to use them, read on.

parsing

1. hashlib. new(name[, data]) method

This is a 1 general method.

name passes in the name of the hash encryption algorithm, such as md5;

data passes in data that needs to be encrypted and can be ignored, which is passed in later in update().


>>> m = hashlib.new('md5')
>>> m.update(b'123456')
>>> m.hexdigest()
'202cb962ac59075b964b07152d234b70'


You can use the two built-in attributes of ES101en.algorithms_ES103en or hashlib.algorithms_ES106en to see which encryption algorithms hashlib supports.

hashlib.algorithms_guaranteed is a collection of hash algorithm names guaranteed to be supported by hashlib module on all platforms;

hashlib.algorithms_available is a collection of the hash algorithm names that are available in the current running python compiler, where the hash algorithm name may be duplicated due to OpenSSL.

hashlib.algorithms_guaranteed is a subset of ES131en.algorithms_ES133en.

See the output below:


>>> hashlib.algorithms_guaranteed
{'sha3_384', 'md5', 'blake2s', 'sha3_512', 'blake2b', 'shake_128', 'sha384', 'sha3_256', 'sha1', 'shake_256', 'sha3_224', 'sha512', 'sha256', 'sha224'}
>>> hashlib.algorithms_available
{'whirlpool', 'ripemd160', 'dsaEncryption', 'sha1', 'SHA224', 'sha512', 'sha256', 'SHA512', 'blake2s', 'blake2b', 'SHA256', 'sha384', 'sha3_256', 'SHA384', 'sha', 'sha224', 'RIPEMD160', 'shake_128', 'sha3_512', 'SHA', 'MD5', 'shake_256', 'DSA', 'sha3_384', 'DSA-SHA', 'ecdsa-with-SHA1', 'md5', 'SHA1', 'dsaWithSHA', 'md4', 'MD4', 'sha3_224'}

2. hash.update(arg)

The arg object is passed in to update the hash object. It is important to note that this method accepts only type byte, otherwise an error will be reported. This is why you add b before the argument to convert the type:


>>> m = hashlib.md5()
>>> m.update('123456')
TypeError: Unicode-objects must be encoded before hashing

Also note that repeatedly calling the update(arg) method concatenates, rather than overwrites, the arg parameters passed in. It is important to pay attention to this point, because it will probably sink you if you are not familiar with the update() principle.

In other words, m.update(a); m.update(b) is equivalent to m.update(a+b), see the following example:


>>> m = hashlib.md5()
>>> m.update(b'123')
>>> m.hexdigest()
'202cb962ac59075b964b07152d234b70'
>>> m.update(b'456')
>>> m.hexdigest()
'e10adc3949ba59abbe56e057f20f883e'

>>> hashlib.md5(b'123456').hexdigest()
'e10adc3949ba59abbe56e057f20f883e'

3. hash.hexdigest()

As you know, hex means base 106 in English, so the method is to convert the data in hash into data containing only Numbers in base 106. There is also the ES173en. digest() method.


Related articles: