常用代码hashlib模块 --加密代码的使用

参考:https://www.cnblogs.com/alex3714/articles/5161349.html

常用代码模板: 

import hashlib
password = 123
md5_obj = hashlib.md5() # 生成一个md5实例
md5_obj.update(password.encode())
md5_password = md5_obj.hexdigest()
print("passwd:",password,md5_password)

模块讲解说明:

hashlib模块  

用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

import hashlib

 

= 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

'''

def digest(self, *args, **kwargs): # real signature unknown

    """ Return the digest value as a string of binary data. """

    pass

 

def hexdigest(self, *args, **kwargs): # real signature unknown

    """ Return the digest value as a string of hexadecimal digits. """

    pass

 

'''

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())

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当涉及到数据加密时,Python提供了多种库和模块来实现不同类型的加密算法。以下是一些常用的Python数据加密库和模块: 1. hashlib:该模块提供了多种哈希算法,如MD5、SHA1、SHA256等。可以使用其提供的函数对数据进行哈希加密。 2. cryptography:这是一个功能强大的加密库,提供了对称加密、非对称加密、哈希算法等多种加密算法的实现。可以使用该库进行高级的数据加密操作。 3. PyCrypto:这是一个古老但功能强大的加密库,提供了对称加密、非对称加密、哈希算法等多种加密算法的实现。可以使用该库进行数据加密和解密操作。 4. bcrypt:这是一个用于密码哈希的库,主要用于密码存储和验证。它使用Blowfish密码哈希算法来保护密码。 下面是一个使用PyCrypto库进行AES对称加密的示例代码: ```python from Crypto.Cipher import AES from Crypto.Random import get_random_bytes def encrypt_data(key, data): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(data.encode()) return nonce + ciphertext + tag def decrypt_data(key, encrypted_data): nonce = encrypted_data[:16] ciphertext = encrypted_data[16:-16] tag = encrypted_data[-16:] cipher = AES.new(key, AES.MODE_EAX, nonce) decrypted_data = cipher.decrypt_and_verify(ciphertext, tag) return decrypted_data.decode() # 生成随机的16字节密钥 key = get_random_bytes(16) # 加密数据 data = "Hello, World!" encrypted_data = encrypt_data(key, data) print("Encrypted data:", encrypted_data) # 解密数据 decrypted_data = decrypt_data(key, encrypted_data) print("Decrypted data:", decrypted_data) ``` 请注意,这只是一个简单的示例,实际使用时需要根据具体需求选择适合的加密算法和参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值