hash_bcrpy_util.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # -*- coding: utf-8 -*-
  2. import hashlib
  3. import os
  4. from typing import Any
  5. from passlib.context import CryptContext
  6. from cryptography.hazmat.backends.openssl import backend
  7. from cryptography.hazmat.primitives import padding
  8. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  9. from itsdangerous import URLSafeSerializer
  10. from app.core.logger import log
  11. # 密码加密配置
  12. PwdContext = CryptContext(
  13. schemes=["bcrypt"],
  14. deprecated="auto",
  15. bcrypt__rounds=12 # 设置加密轮数,增加安全性
  16. )
  17. class PwdUtil:
  18. """
  19. 密码工具类,提供密码加密和验证功能
  20. """
  21. @classmethod
  22. def verify_password(cls, plain_password: str, password_hash: str) -> bool:
  23. """
  24. 校验密码是否匹配
  25. 参数:
  26. - plain_password (str): 明文密码。
  27. - password_hash (str): 加密后的密码哈希值。
  28. 返回:
  29. - bool: 密码是否匹配。
  30. """
  31. return PwdContext.verify(plain_password, password_hash)
  32. @classmethod
  33. def set_password_hash(cls, password: str) -> str:
  34. """
  35. 对密码进行加密
  36. 参数:
  37. - password (str): 明文密码。
  38. 返回:
  39. - str: 加密后的密码哈希值。
  40. """
  41. return PwdContext.hash(password)
  42. @classmethod
  43. def check_password_strength(cls, password: str) -> str | None:
  44. """
  45. 检查密码强度
  46. 参数:
  47. - password (str): 明文密码。
  48. 返回:
  49. - str | None: 如果密码强度不够返回提示信息,否则返回None。
  50. """
  51. if len(password) < 6:
  52. return "密码长度至少6位"
  53. if not any(c.isupper() for c in password):
  54. return "密码需要包含大写字母"
  55. if not any(c.islower() for c in password):
  56. return "密码需要包含小写字母"
  57. if not any(c.isdigit() for c in password):
  58. return "密码需要包含数字"
  59. return None
  60. class AESCipher:
  61. """AES 加密器"""
  62. def __init__(self, key: bytes | str) -> None:
  63. """
  64. 初始化 AES 加密器。
  65. 参数:
  66. - key (bytes | str): 密钥,16/24/32 bytes 或 16 进制字符串。
  67. 返回:
  68. - None
  69. """
  70. self.key = key if isinstance(key, bytes) else bytes.fromhex(key)
  71. def encrypt(self, plaintext: bytes | str) -> bytes:
  72. """
  73. AES 加密。
  74. 参数:
  75. - plaintext (bytes | str): 加密前的明文。
  76. 返回:
  77. - bytes: 加密后的密文(前16字节为随机IV)。
  78. """
  79. if not isinstance(plaintext, bytes):
  80. plaintext = str(plaintext).encode('utf-8')
  81. iv = os.urandom(16)
  82. cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv), backend=backend)
  83. encryptor = cipher.encryptor()
  84. padder = padding.PKCS7(cipher.algorithm.block_size).padder() # type: ignore
  85. padded_plaintext = padder.update(plaintext) + padder.finalize()
  86. ciphertext = encryptor.update(padded_plaintext) + encryptor.finalize()
  87. return iv + ciphertext
  88. def decrypt(self, ciphertext: bytes | str) -> str:
  89. """
  90. AES 解密。
  91. 参数:
  92. - ciphertext (bytes | str): 解密前的密文,bytes 或 16 进制字符串。
  93. 返回:
  94. - str: 解密后的明文。
  95. """
  96. ciphertext = ciphertext if isinstance(ciphertext, bytes) else bytes.fromhex(ciphertext)
  97. iv = ciphertext[:16]
  98. ciphertext = ciphertext[16:]
  99. cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv), backend=backend)
  100. decryptor = cipher.decryptor()
  101. unpadder = padding.PKCS7(cipher.algorithm.block_size).unpadder() # type: ignore
  102. padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
  103. plaintext = unpadder.update(padded_plaintext) + unpadder.finalize()
  104. return plaintext.decode('utf-8')
  105. class Md5Cipher:
  106. """MD5 加密器"""
  107. @staticmethod
  108. def encrypt(plaintext: bytes | str) -> str:
  109. """
  110. MD5 加密。
  111. 参数:
  112. - plaintext (bytes | str): 加密前的明文。
  113. 返回:
  114. - str: MD5 十六进制摘要。
  115. """
  116. md5 = hashlib.md5()
  117. if not isinstance(plaintext, bytes):
  118. plaintext = str(plaintext).encode('utf-8')
  119. md5.update(plaintext)
  120. return md5.hexdigest()
  121. class ItsDCipher:
  122. """ItsDangerous 加密器"""
  123. def __init__(self, key: bytes | str) -> None:
  124. """
  125. 初始化 ItsDangerous 加密器。
  126. 参数:
  127. - key (bytes | str): 密钥,16/24/32 bytes 或 16 进制字符串。
  128. 返回:
  129. - None
  130. """
  131. self.key = key if isinstance(key, bytes) else bytes.fromhex(key)
  132. def encrypt(self, plaintext: Any) -> str:
  133. """
  134. ItsDangerous 加密。
  135. 参数:
  136. - plaintext (Any): 加密前的明文。
  137. 返回:
  138. - str: 加密后的密文(URL安全)。
  139. 异常:
  140. - Exception: 加密失败时使用 MD5 作为降级,错误已记录。
  141. """
  142. serializer = URLSafeSerializer(self.key)
  143. try:
  144. ciphertext = serializer.dumps(plaintext)
  145. except Exception as e:
  146. log.error(f'ItsDangerous encrypt failed: {e}')
  147. ciphertext = Md5Cipher.encrypt(plaintext)
  148. return ciphertext
  149. def decrypt(self, ciphertext: str) -> Any:
  150. """
  151. ItsDangerous 解密。
  152. 参数:
  153. - ciphertext (str): 解密前的密文。
  154. 返回:
  155. - Any: 解密后的明文;失败时返回原密文。
  156. 异常:
  157. - Exception: 解密失败时记录错误并返回原密文。
  158. """
  159. serializer = URLSafeSerializer(self.key)
  160. try:
  161. plaintext = serializer.loads(ciphertext)
  162. except Exception as e:
  163. log.error(f'ItsDangerous decrypt failed: {e}')
  164. plaintext = ciphertext
  165. return plaintext