tmp
zhaoqingang
2025-01-15 9f116ea7e8f7d53a22b4dce10de942d564818a01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import asyncio
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位密码以包含所有必要的字符
        raise ValueError("Password length should be at least 3")
 
    # 确保密码中至少包含一个字母和一个数字
    password = [
        random.choice(string.ascii_uppercase),  # 大写字母
        random.choice(string.ascii_lowercase),  # 小写字母
        random.choice(string.digits)  # 数字
    ]
 
    # 添加剩余的随机字符
    characters = string.ascii_letters + string.digits
    password.extend(random.choice(characters) for _ in range(length - 3))
 
    # 打乱密码以确保随机性
    random.shuffle(password)
 
    # 将列表转换为字符串
    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))
    # password = generate_password(10)
    # print(password)
 
    asyncio.run(password_encrypted("123456"))
    asyncio.run(password_decrypted("gAAAAABnhyOiGUnAqK7FW_pHsXifje8WG1cirtF_eu3a44FrMYM3AkSBsWjsJrwpzUlD2GDzzZOS6yYu4Ie5gnkYuy8HVN3FBw=="))