知我者,谓我心忧。不知我者,谓我何求。
需求就是对普通的字符串进行加密,例如使用用户信息等私人信息的时候生成token,这种情况,为了不泄露个人信息,所以选择了简单的字符串加密,此方法不可逆。
1、第一个版本简单的封装一些调用方法
import hmac
import hashlib
class EncryptString:
PARAM_TYPE_ERR = 'Wrong parameter type.'
def __init__(self, origin_str: str):
"""
Encrypt strings.
origin_str: The original string that needs to be encrypted.
"""
if isinstance(origin_str, str):
self.origin_str = origin_str.encode('utf-8')
else:
raise TypeError(self.PARAM_TYPE_ERR)
def md5_str(self):
md5_res = hashlib.md5()
md5_res.update(self.origin_str)
return md5_res.hexdigest()
def sha1_str(self):
sha1_res = hashlib.sha1()
sha1_res.update(self.origin_str)
return sha1_res.hexdigest()
def sha224_str(self):
sha224_res = hashlib.sha224()
sha224_res.update(self.origin_str)
return sha224_res.hexdigest()
def sha256_str(self):
sha256_res = hashlib.sha256()
sha256_res.update(self.origin_str)
return sha256_res.hexdigest()
def sha384_str(self):
sha384_res = hashlib.sha384()
sha384_res.update(self.origin_str)
return sha384_res.hexdigest()
def sha512_str(self):
sha512_res = hashlib.sha512()
sha512_res.update(self.origin_str)
return sha512_res.hexdigest()
def blake2b_str(self):
blake2b_res = hashlib.blake2b()
blake2b_res.update(self.origin_str)
return blake2b_res.hexdigest()
def blake2s_str(self):
blake2s_res = hashlib.blake2s()
blake2s_res.update(self.origin_str)
return blake2s_res.hexdigest()
def sha3_224_str(self):
sha3_224_res = hashlib.sha3_224()
sha3_224_res.update(self.origin_str)
return sha3_224_res.hexdigest()
def sha3_256_str(self):
sha3_256_res = hashlib.sha3_256()
sha3_256_res.update(self.origin_str)
return sha3_256_res.hexdigest()
def sha3_384_str(self):
sha3_384_res = hashlib.sha3_384()
sha3_384_res.update(self.origin_str)
return sha3_384_res.hexdigest()
def sha3_512_str(self):
sha3_512_res = hashlib.sha3_512()
sha3_512_res.update(self.origin_str)
return sha3_512_res.hexdigest()
def shake_128_str(self, length: int):
if not isinstance(length, int):
raise TypeError(self.PARAM_TYPE_ERR)
else:
shake_128_res = hashlib.shake_128()
shake_128_res.update(self.origin_str)
return shake_128_res.hexdigest(length)
def shake_256_str(self, length: int):
if not isinstance(length, int):
raise TypeError(self.PARAM_TYPE_ERR)
else:
shake_256_res = hashlib.shake_256()
shake_256_res.update(self.origin_str)
return shake_256_res.hexdigest(length)
def hmac_str(self, key: str):
if not isinstance(key, str):
raise TypeError(self.PARAM_TYPE_ERR)
else:
hmac_res = hmac.new(key.encode('utf-8'), self.origin_str, digestmod='md5')
return hmac_res.hexdigest()
2、第二个版本直接用传入参数的来控制使用什么算法加密
import hmac
import hashlib
class EncryptString:
PARAM_TYPE_ERR = 'Wrong parameter type.'
MISS_PARAM = 'The parameter (length) of the algorithm needs to be greater than 0.'
SUPPORTED_ALGORITHMS = 'Supported algorithms are: {}'
__always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
'blake2b', 'blake2s',
'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
'shake_128', 'shake_256')
def __init__(self, origin_str: (str, bytes)):
"""
Encrypt strings.
origin_str: The original string that needs to be encrypted.
"""
if isinstance(origin_str, str):
self.origin_str = origin_str.encode('utf-8')
elif isinstance(origin_str, bytes):
self.origin_str = origin_str
else:
raise TypeError(self.PARAM_TYPE_ERR)
def hashlib_str(self, algorithms: str, length: int = 10):
"""
:param algorithms: For example:('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
:param length: default 10, algorithms 'shake_128' and 'shake_256' need param length.
:return: Encrypted string.
"""
if not isinstance(algorithms, str):
raise TypeError(self.PARAM_TYPE_ERR)
else:
if algorithms == 'md5':
md5_res = hashlib.md5()
md5_res.update(self.origin_str)
return md5_res.hexdigest()
elif algorithms == 'sha1':
sha1_res = hashlib.sha1()
sha1_res.update(self.origin_str)
return sha1_res.hexdigest()
elif algorithms == 'sha224':
sha224_res = hashlib.sha224()
sha224_res.update(self.origin_str)
return sha224_res.hexdigest()
elif algorithms == 'sha256':
sha256_res = hashlib.sha256()
sha256_res.update(self.origin_str)
return sha256_res.hexdigest()
elif algorithms == 'sha384':
sha384_res = hashlib.sha384()
sha384_res.update(self.origin_str)
return sha384_res.hexdigest()
elif algorithms == 'sha512':
sha512_res = hashlib.sha512()
sha512_res.update(self.origin_str)
return sha512_res.hexdigest()
elif algorithms == 'blake2b':
blake2b_res = hashlib.blake2b()
blake2b_res.update(self.origin_str)
return blake2b_res.hexdigest()
elif algorithms == 'blake2s':
blake2s_res = hashlib.blake2s()
blake2s_res.update(self.origin_str)
return blake2s_res.hexdigest()
elif algorithms == 'sha3_224':
sha3_224_res = hashlib.sha3_224()
sha3_224_res.update(self.origin_str)
return sha3_224_res.hexdigest()
elif algorithms == 'sha3_256':
sha3_256_res = hashlib.sha3_256()
sha3_256_res.update(self.origin_str)
return sha3_256_res.hexdigest()
elif algorithms == 'sha3_384':
sha3_384_res = hashlib.sha3_384()
sha3_384_res.update(self.origin_str)
return sha3_384_res.hexdigest()
elif algorithms == 'sha3_512':
sha3_512_res = hashlib.sha3_512()
sha3_512_res.update(self.origin_str)
return sha3_512_res.hexdigest()
elif algorithms == 'shake_128':
if not isinstance(length, int):
raise TypeError(self.PARAM_TYPE_ERR)
else:
shake_128_res = hashlib.shake_128()
shake_128_res.update(self.origin_str)
return shake_128_res.hexdigest(length)
elif algorithms == 'shake_256':
if not isinstance(length, int):
raise TypeError(self.PARAM_TYPE_ERR)
else:
shake_256_res = hashlib.shake_256()
shake_256_res.update(self.origin_str)
return shake_256_res.hexdigest(length)
else:
return self.SUPPORTED_ALGORITHMS.format(self.__always_supported)
def hmac_str(self, key: str, algorithms: str, length: int = 10):
"""
:param key: secret key
:param algorithms: For example:('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
:param length: default 10, algorithms 'shake_128' and 'shake_256' need param length.
:return: Encrypted string.
"""
if not isinstance(key, str) and not isinstance(algorithms, str):
raise TypeError(self.PARAM_TYPE_ERR)
else:
encrypt_str = EncryptString(self.origin_str)
if algorithms in ['shake_128', 'shake_256']:
return encrypt_str.hashlib_str(algorithms, length)
else:
hmac_res = hmac.new(key.encode('utf-8'), self.origin_str, digestmod=algorithms).hexdigest()
return hmac_res
个人感觉第二个版本更好一点,如果还有其他的加密也可以直接新增,也可以继承后新增其他方法。
如果感觉本文对您有帮助可以点个赞哦
本文为学习笔记,转载请标明出处
本文仅供交流学习,请勿用于非法途径
仅是个人意见,如有想法,欢迎留言
网友评论