# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/12/1
# 文件名称:test_pass.py
# 作用:常用的加密算法实现
import hashlib
class TestPass():
def __init__(self):
super(TestPass, self).__init__()
self.name = "admin"
self.password = "123456"
if __name__ == "__main__":
test_pass = TestPass()
self.name
模拟用户名数据,self.password
模拟密码数据。MD5
是一种常用的单向散列函数,是不可逆的,也就是说无法通过被加密后的结果来确定加密前的内容;hashlib
,这个一般python
安装完都是有的,目录在:X:\Python37\Lib\hashlib.py
def test_md5(self):
md = hashlib.md5(self.password.encode())
md5_pass = md.hexdigest()
print(f"密码{self.password}, md5直接加密后为:{md5_pass}")
密码123456, md5直接加密后为:e10adc3949ba59abbe56e057f20f883e
MD5
加密;md5
加密; def test_md5_01(self):
data = (self.name + self.password).lower()
md = hashlib.md5(data.encode())
md5_pass = md.hexdigest()
print(f"密码{self.password},用户名{self.name}, md5组合加密后为:{md5_pass}")
密码123456,用户名admin, md5组合加密后为:a66abb5684c45962d887564f08346e8d
def test_md5_02(self):
s = self.password[:5] # 设置盐
md = hashlib.md5((self.password + s).encode())
md5_pass = md.hexdigest()
print(f"密码{self.password},md5加盐后为:{md5_pass}")
密码123456,md5加盐后为:e363373ddc24b34c5bb9d99abbfd8be5
join
方式处理; def test_md5_03(self):
s = self.password[:6] # 设置盐
md = hashlib.md5((self.password.join(s)).encode())
md5_pass = md.hexdigest()
print(f"密码{self.password},md5加盐使用json方法为:{md5_pass}")
密码123456,md5加盐使用json方法为:43ec0d3f863b4f7e635e7169ddc18606
hashlib
中;SHA1
加密,实现如下: def test_sha1(self):
data = self.name + self.password
sha1 = hashlib.sha1()
sha1.update(data.encode("utf-8"))
sha1_pass = sha1.hexdigest()
print(f"密码{self.password},用户名{self.name}, sha1组合加密后为:{sha1_pass}")
密码123456,用户名admin, sha1组合加密后为:cd5ea73cd58f827fa78eef7197b8ee606c99b2e6
SHA256
比SHA1
更安全,但是效率慢,结果也会长一些;SHA256
加密,实现如下: def test_sha256(self):
data = self.name + self.password
sha256 = hashlib.sha256()
sha256.update(data.encode("utf-8"))
sha1_pass = sha256.hexdigest()
print(f"密码{self.password},用户名{self.name}, sha256组合加密后为:{sha1_pass}")
密码123456,用户名admin, sha256组合加密后为:ac0e7d037817094e9e0b4441f9bae3209d67b02fa484917065f71b16109a1a78
SHA512
这个就不说了,同理可证。HMAC
是一种基于加密hash
函数和共享密钥的消息认证协议;hmac
库,目录在:X:\Python37\Lib\hmac.py
hash
函数,示例如下: def test_hmac(self):
hm = hmac.new(b'029-11111111', bytes(self.password, 'utf-8'), hashlib.md5)
hm.digest()
hmac_pass = hm.hexdigest()
print(f"密码{self.password},用户名{self.name}, hmac加密后为:{hmac_pass}")
密码123456,用户名admin, hmac加密后为:4e32d965d8965df4c7f6aaaf68791e86
DES
、AES
、RSA
、ECC
等等# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/12/1
# 文件名称:test_pass.py
# 作用:常用的加密算法实现
import hashlib
import hmac
class TestPass():
def __init__(self):
super(TestPass, self).__init__()
self.name = "admin"
self.password = "123456"
def test_md5(self):
md = hashlib.md5(self.password.encode())
md5_pass = md.hexdigest()
print(f"密码{self.password}, md5直接加密后为:{md5_pass}")
def test_md5_01(self):
data = (self.name + self.password).lower()
md = hashlib.md5(data.encode())
md5_pass = md.hexdigest()
print(f"密码{self.password},用户名{self.name}, md5组合加密后为:{md5_pass}")
def test_md5_02(self):
s = self.password[:5] # 设置盐
md = hashlib.md5((self.password + s).encode())
md5_pass = md.hexdigest()
print(f"密码{self.password},md5加盐后为:{md5_pass}")
def test_md5_03(self):
s = self.password[:6] # 设置盐
md = hashlib.md5((self.password.join(s)).encode())
md5_pass = md.hexdigest()
print(f"密码{self.password},md5加盐使用json方法为:{md5_pass}")
def test_sha1(self):
data = self.name + self.password
sha1 = hashlib.sha1()
sha1.update(data.encode("utf-8"))
sha1_pass = sha1.hexdigest()
print(f"密码{self.password},用户名{self.name}, sha1组合加密后为:{sha1_pass}")
def test_sha256(self):
data = self.name + self.password
sha256 = hashlib.sha256()
sha256.update(data.encode("utf-8"))
sha1_pass = sha256.hexdigest()
print(f"密码{self.password},用户名{self.name}, sha256组合加密后为:{sha1_pass}")
def test_hmac(self):
hm = hmac.new(b'029-11111111', bytes(self.password, 'utf-8'), hashlib.md5)
hm.digest()
hmac_pass = hm.hexdigest()
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()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。