Encryption algorithm and hashlib module

Introduction to Encryption Algorithms

HASH

  • Hash, generally translated as "hash" , is also directly transliterated as "hash" , that is, the input of any length (also known as pre-map, pre-image) is transformed into a fixed-length output through a hash algorithm. The output is the hash value. This transformation is a compression map, that is, the space of the hash value is usually much smaller than the space of the input, different inputs may hash to the same output, and it is impossible to uniquely determine the input value from the hash value.
  • Simply put, it is a function that compresses a message of any length into a message digest of a fixed length.
  • HASH is mainly used for encryption algorithms in the field of information security. It converts some information of different lengths into chaotic 128-bit codes, which are called HASH values. It can also be said that hash is to find a kind of data content and data storage address. Mapping relations

MD5

What is the MD5 algorithm?
  • MD5 Message-Digest Algorithm (English: MD5 Message-Digest Algorithm) , a widely used cryptographic hash function, can generate a 128-bit hash value (hash value), used to ensure complete and consistent information transmission. The predecessors of MD5 are MD2, MD3 and MD4.
MD5 function
  • Input information of any length, after processing, the output is 128-bit information (digital fingerprint);
  • Different results for different inputs (uniqueness);
Features of MD5 Algorithm
  • Compressibility: For data of any length, the length of the calculated MD5 value is fixed
  • Easy to calculate: It is easy to calculate the MD5 value from the original data
  • Modification resistance: any modification to the original data, the MD5 value generated by modifying one byte will be very different
  • Strong anti-collision: Knowing the original data and MD5, it is very difficult to find a data with the same MD5 value (that is, fake data).
Is the MD5 algorithm reversible?
  • The reason why MD5 is irreversible is that it is a hash function that uses a hash algorithm, and part of the original text information is lost during the calculation process.
MD5 use
  • To prevent tampering:

    • For example, to send an electronic document, before sending, I first get the output result a of MD5. Then after the other party receives the electronic document, the other party also gets an MD5 output result b. If a is the same as b, it means that it has not been tampered with.
    • For example, I provide file downloads. In order to prevent criminals from adding Trojan horses to the installation program, I can publish the MD5 output results obtained by the installation files on the website.
    • SVN also uses MD5 to detect whether the file has been modified after CheckOut.
  • To prevent seeing the plaintext directly:

    • Now many websites store the MD5 value of the user's password when storing the user's password in the database. In this way, even if the criminals obtain the MD5 value of the user's password in the database, they cannot know the user's password. (For example, in the UNIX system, the user's password is encrypted with MD5 (or other similar algorithms) and stored in the file system. When the user logs in, the system calculates the password entered by the user into an MD5 value, and then goes and saves Compare the MD5 values ​​in the file system, and then determine whether the input password is correct. Through such steps, the system can determine the legitimacy of the user's login system without knowing the clear code of the user's password. This can not only avoid the user The password is known to users with system administrator privileges, and it also increases the difficulty of password cracking to a certain extent.)
  • Anti-repudiation (digital signature):

    • This requires a third-party certification body. For example, A writes a file, and the certification body uses the MD5 algorithm to generate summary information for this file and make a record. If A later says that the document was not written by him, the authority only needs to regenerate the summary information of the document, and then compare it with the summary information recorded in the record. If it is the same, it is proved that A wrote it. This is called a "digital signature".

SHA-1

  • The Secure Hash Algorithm is mainly applicable to the Digital Signature Algorithm DSA defined in the Digital Signature Standard DSS. For messages less than 2^64 bits in length, SHA1 produces a 160-bit message digest. When a message is received, this message digest can be used to verify the integrity of the data.
  • SHA is a series of cryptographic hash functions designed by the National Security Agency and published by the National Institute of Standards and Technology.
  • Since MD5 and SHA-1 were cracked by Wang Xiaoyun, a professor at Shandong University in 2005, scientists have launched SHA224, SHA256, SHA384, SHA512. Of course, the longer the number of digits, the more difficult it is to crack, but at the same time, it is necessary to generate encrypted message digests. It also takes longer. Currently the most popular encryption algorithm is SHA-256.

Comparison of MD5 and SHA-1

  • Since both MD5 and SHA-1 are developed from MD4, they have many similarities in structure and strength. The biggest difference between SHA-1 and MD5 is that its digest is 32 bits longer than MD5 digest. For a brute force attack, the difficulty of generating any one message with a digest equal to the digest of a given message: MD5 is an operation of the order of 2128, SHA-1 is an operation of the order of 2160. The difficulty of generating two packets with the same digest: MD5 is an order of magnitude 264 operation, SHA-1 is an order of magnitude operation. Therefore, SHA-1 is more powerful to brute force attacks. But because SHA-1 has 80:64 more round-robin steps than MD5 and the buffer to process is 160:128 bits larger, SHA-1 runs slower than MD5.

hashlib module

  • hashlib was used after python3.x to replace the md5 and sha modules and to make their APIs consistent.
  • It is supported by OpenSSL and supports the following algorithms: md5,sha1, sha224, sha256, sha384, sha512
import hashlib

m = hashlib.md5()
m.update(b"Hello")
m.update(b"It's me")
print(m.digest())
m.update(b"It's been a long time since last time we ...")

print(m.digest()) #2进制格式hash
print(len(m.hexdigest())) #16进制格式hash


import hashlib
# ######## md5 ########

hash = hashlib.md5()
hash.update('admin')
print(hash.hexdigest())

# ######## sha1 ########

hash = hashlib.sha1()
hash.update('admin')
print(hash.hexdigest())

# ######## sha256 ########

hash = hashlib.sha256()
hash.update('admin')
print(hash.hexdigest())


# ######## sha384 ########

hash = hashlib.sha384()
hash.update('admin')
print(hash.hexdigest())

# ######## sha512 ########

hash = hashlib.sha512()
hash.update('admin')
print(hash.hexdigest())

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325155585&siteId=291194637