ToB企服应用市场:ToB评测及商务社交产业平台

标题: 在自动化测试时,Python常用的几个加密算法,你有用到吗 [打印本页]

作者: 美食家大橙子    时间: 2024-2-15 18:35
标题: 在自动化测试时,Python常用的几个加密算法,你有用到吗
本文分享自华为云社区《『加密算法』| 自动化测试时基于Python常用的几个加密算法实现,你有用到吗?》,作者:虫无涯 。
写在前边

公用数据

  1. # -*- coding:utf-8 -*-
  2. # 作者:虫无涯
  3. # 日期:2023/12/1
  4. # 文件名称:test_pass.py
  5. # 作用:常用的加密算法实现
  6. # 联系:VX(NoamaNelson)
  7. # 博客:https://blog.csdn.net/NoamaNelson
  8. import hashlib
  9. class TestPass():
  10. def __init__(self):
  11. super(TestPass, self).__init__()
  12. self.name = "admin"
  13. self.password = "123456"
  14. if __name__ == "__main__":
  15. test_pass = TestPass()
复制代码
MD5直接加密

  1. X:\Python37\Lib\hashlib.py
复制代码
  1. def test_md5(self):
  2. md = hashlib.md5(self.password.encode())
  3. md5_pass = md.hexdigest()
  4. print(f"密码{self.password}, md5直接加密后为:{md5_pass}")
复制代码
  1. 密码123456, md5直接加密后为:e10adc3949ba59abbe56e057f20f883e
复制代码
用户名和密码组合MD5加密

  1. def test_md5_01(self):
  2. data = (self.name + self.password).lower()
  3. md = hashlib.md5(data.encode())
  4. md5_pass = md.hexdigest()
  5. print(f"密码{self.password},用户名{self.name}, md5组合加密后为:{md5_pass}")
复制代码
  1. 密码123456,用户名admin, md5组合加密后为:a66abb5684c45962d887564f08346e8d
复制代码
密码使用MD5+盐加密

  1. def test_md5_02(self):
  2. s = self.password[:5] # 设置盐
  3. md = hashlib.md5((self.password + s).encode())
  4. md5_pass = md.hexdigest()
  5. print(f"密码{self.password},md5加盐后为:{md5_pass}")
复制代码
  1. 密码123456,md5加盐后为:e363373ddc24b34c5bb9d99abbfd8be5
复制代码
MD5加盐后将密码整体插入盐中

  1. def test_md5_03(self):
  2. s = self.password[:6] # 设置盐
  3. md = hashlib.md5((self.password.join(s)).encode())
  4. md5_pass = md.hexdigest()
  5. print(f"密码{self.password},md5加盐使用json方法为:{md5_pass}")
复制代码
  1. 密码123456,md5加盐使用json方法为:43ec0d3f863b4f7e635e7169ddc18606
复制代码
SHA1加密

  1. def test_sha1(self):
  2. data = self.name + self.password
  3. sha1 = hashlib.sha1()
  4. sha1.update(data.encode("utf-8"))
  5. sha1_pass = sha1.hexdigest()
  6. print(f"密码{self.password},用户名{self.name}, sha1组合加密后为:{sha1_pass}")
复制代码
  1. 密码123456,用户名admin, sha1组合加密后为:cd5ea73cd58f827fa78eef7197b8ee606c99b2e6
复制代码
SHA256加密

  1. def test_sha256(self):
  2. data = self.name + self.password
  3. sha256 = hashlib.sha256()
  4. sha256.update(data.encode("utf-8"))
  5. sha1_pass = sha256.hexdigest()
  6. print(f"密码{self.password},用户名{self.name}, sha256组合加密后为:{sha1_pass}")
复制代码
  1. 密码123456,用户名admin, sha256组合加密后为:ac0e7d037817094e9e0b4441f9bae3209d67b02fa484917065f71b16109a1a78
复制代码
HMAC加密

  1. X:\Python37\Lib\hmac.py
复制代码
  1. def test_hmac(self):
  2. hm = hmac.new(b'029-11111111', bytes(self.password, 'utf-8'), hashlib.md5)
  3. hm.digest()
  4. hmac_pass = hm.hexdigest()
  5. print(f"密码{self.password},用户名{self.name}, hmac加密后为:{hmac_pass}")
复制代码
  1. 密码123456,用户名admin, hmac加密后为:4e32d965d8965df4c7f6aaaf68791e86
复制代码
其他的算法

本文源码
  1. # -*- coding:utf-8 -*-# 作者:虫无涯# 日期:2023/12/1# 文件名称:test_pass.py# 作用:常用的加密算法实现# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport hashlibimport hmacclass TestPass():def __init__(self):super(TestPass, self).__init__()self.name = "admin"self.password = "123456"def test_md5(self):
  2. md = hashlib.md5(self.password.encode())
  3. md5_pass = md.hexdigest()
  4. print(f"密码{self.password}, md5直接加密后为:{md5_pass}")def test_md5_01(self):
  5. data = (self.name + self.password).lower()
  6. md = hashlib.md5(data.encode())
  7. md5_pass = md.hexdigest()
  8. print(f"密码{self.password},用户名{self.name}, md5组合加密后为:{md5_pass}")def test_md5_02(self):
  9. s = self.password[:5] # 设置盐
  10. md = hashlib.md5((self.password + s).encode())
  11. md5_pass = md.hexdigest()
  12. print(f"密码{self.password},md5加盐后为:{md5_pass}")def test_md5_03(self):
  13. s = self.password[:6] # 设置盐
  14. md = hashlib.md5((self.password.join(s)).encode())
  15. md5_pass = md.hexdigest()
  16. print(f"密码{self.password},md5加盐使用json方法为:{md5_pass}")def test_sha1(self):
  17. data = self.name + self.password
  18. sha1 = hashlib.sha1()
  19. sha1.update(data.encode("utf-8"))
  20. sha1_pass = sha1.hexdigest()
  21. print(f"密码{self.password},用户名{self.name}, sha1组合加密后为:{sha1_pass}")def test_sha256(self):
  22. data = self.name + self.password
  23. sha256 = hashlib.sha256()
  24. sha256.update(data.encode("utf-8"))
  25. sha1_pass = sha256.hexdigest()
  26. print(f"密码{self.password},用户名{self.name}, sha256组合加密后为:{sha1_pass}")def test_hmac(self):
  27. hm = hmac.new(b'029-11111111', bytes(self.password, 'utf-8'), hashlib.md5)
  28. hm.digest()
  29. hmac_pass = hm.hexdigest()
  30. print(f"密码{self.password},用户名{self.name}, hmac加密后为:{hmac_pass}")if __name__ == "__main__":test_pass = TestPass()# test_pass.test_md5()# test_pass.test_md5_01()# test_pass.test_md5_02()# test_pass.test_md5_03()# test_pass.test_sha1()# test_pass.test_sha256()test_pass.test_hmac()
复制代码
 
点击关注,第一时间了解华为云新鲜技术~

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4