zhaoqingang
2025-02-24 f13239560d9a6888d3bf95699ed5fc10395b50ff
app/utils/password_handle.py
@@ -2,6 +2,10 @@
import random
import string
from cryptography.fernet import Fernet
from app.config.config import settings
cipher_suite = Fernet(settings.PASSWORD_KEY.encode("utf-8"))
async def generate_password(length=10):
    if length < 6:  # 至少需要3位密码以包含所有必要的字符
@@ -24,8 +28,22 @@
    # 将列表转换为字符串
    return ''.join(password)
async def password_encrypted(password):
    hash_pwd = cipher_suite.encrypt(password.encode("utf-8")).decode("utf-8")
    # print(hash_pwd)
    return hash_pwd
async def password_decrypted(hash_password):
    pwd = cipher_suite.decrypt(hash_password).decode("utf-8")
    # print(pwd)
    return pwd
if __name__ == "__main__":
    # 生成一个10位的密码
    asyncio.run(generate_password(10))
    # asyncio.run(generate_password(10))
    # password = generate_password(10)
    # print(password)
    # print(password)
    asyncio.run(password_encrypted("zhaoqg123456"))
    asyncio.run(password_decrypted("gAAAAABnh2Y-OYrpdm2QuW24j4QL3pxjkKZsHu37Vzl_mh8SrG5Roa5TThcBxcj7hqPq7NrA8OaL0WdsmpcpYDfpZCofBVjbbA=="))