Home  >  Article  >  Backend Development  >  Analysis of hashlib encryption module in Python (code example)

Analysis of hashlib encryption module in Python (code example)

不言
不言forward
2018-11-23 17:01:181883browse
The content of this article is about the analysis (code examples) of the hashlib encryption module in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The hashlib module is a module used to hash encryption of strings. Plain text and cipher text have a one-to-one correspondence; it is used for encryption of usernames, passwords, etc. during registration and login.

1. Function analysis

1. There are 5 encryption algorithms

md5(),sha1(),sha224(),sha256() , sha3840(), sha512(), respectively obtain different encrypted ciphertexts.

2. hashlib.hexdigest(): Get the encrypted ciphertext, hexadecimal, no parameters

3. hashlib.digest(): Get the encrypted ciphertext, binary, no parameters

4. hashlib.copy(): Copy the currently created hash object, no parameters

5. update(str1,encoding('utf-8'')): Update encryption The ciphertext obtained is different from the original ciphertext

6. hash.name: View the encryption algorithm of the currently obtained hash object

7. hash.digest_size: hash secret How many bytes does the key occupy?

8. hash.block_size: The size of the hash data block

9. hashlib.algorithms_guaranteed: View the hash algorithms supported by all platforms

10 , hashlib.algorithms_available: View all hash encryption algorithms

Code examples:

import hashlib

def hash_fun_1(str1):
    #创建一个hahsh对象并对str1加密
    m=hashlib.md5(str1.encode('utf-8'))
    print('获取加密的密文,16进制,无参数',m.hexdigest())
    print('获取加密的密文,二进制,无参数:',m.digest())
    print('获取hash块的大小:',m.block_size)
    print('hash密钥占多少个字节:',m.digest_size)
    print('查看当前获得的hash对象的加密算法',m.name)

    #更新密文
    m.update(str1.encode('utf-8'))
    print('获取加密的密文,16进制,无参数', m.hexdigest())
    print('获取加密的密文,二进制,无参数:', m.digest())
    print('获取hash块的大小:', m.block_size)
    print('hash密钥占多少个字节:', m.digest_size)
    print('查看当前获得的hash对象的加密算法', m.name)

if __name__ == '__main__':
    hash_fun_1('mark')

Result:

获取加密的密文,16进制,无参数 ea82410c7a9991816b5eeeebe195e20a
获取加密的密文,二进制,无参数: b'\xea\x82A\x0cz\x99\x91\x81k^\xee\xeb\xe1\x95\xe2\n'
获取hash块的大小: 64
hash密钥占多少个字节: 16
查看当前获得的hash对象的加密算法 md5
获取加密的密文,16进制,无参数 ac673f4dbac79922838901b5974a419a
获取加密的密文,二进制,无参数: b'\xacg?M\xba\xc7\x99"\x83\x89\x01\xb5\x97JA\x9a'
获取hash块的大小: 64
hash密钥占多少个字节: 16
查看当前获得的hash对象的加密算法 md5

2. Application:

1. There are two ways to create a hash object:

m=hashlib.new('md5',b'cai')#选择md5加密函数加密字符串‘cai’
m=hashlib.md5('cai'.encode('utf-8'))#加密的另一种写法

2. Feature usage: When the string that needs to be encrypted is too large, you can use the same hash object to encrypt it multiple times, update( a) update(b)=update(a b)

Example:

import hashlib

m1=hashlib.md5()
m2=m1.copy()
m1.update('a'.encode('utf-8'))
m1.update('b'.encode('utf-8'))
print(m1.hexdigest())#输出密文
m2.update('ab'.encode('utf-8'))
print(m2.hexdigest())#输出另一个密文

Running result:

187ef4436122d1cc2f40dc2b92f0eba0
187ef4436122d1cc2f40dc2b92f0eba0

3. Hash algorithm encryption

The ciphertext obtained by the encryption algorithm is irreversible, but the relationship between ciphertext and plaintext is one-to-one, which makes decryption possible. Use big data to store the relationship between ciphertext and plaintext. If the database happens to If you have the corresponding ciphertext, you can find the plaintext to complete the decryption. The commonly used decryption website: http://www.cmd5.com/, you can find the corresponding plaintext by entering the ciphertext.

In order to increase the difficulty of cracking, it is generally necessary to encrypt the password multiple times. The hashlib module has a special function.

Code example:

import hashlib
import binascii

#sha256为算法名称,12345678为要加密的密码
#mark指的是杂质,额外添加的东西,使得破解更难
#10 000是迭代次数,可以理解为加密次数
pwd=hashlib.pbkdf2_hmac('sha256',b'12345678',b'mark',10000)
print(binascii.hexlify(pwd).decode('utf-8'))

Result:

129d11e9ba1f3ef4e1393516d434f356363ffe68d7baca37fd1e91f0e87abe36

The above is the detailed content of Analysis of hashlib encryption module in Python (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete