生成一次性密码(具有 N 个符号长度的短信密码)的最简单方法是什么passlib
?
我现在如何创建它:
from secrets import randbelow as secrets_randbelow
def create_secret_code() -> str: # TODO use OTP
secret_code = "".join([str(secrets_randbelow(exclusive_upper_bound=10)) for _ in range(config.SECRET_CODE_LEN)])
print_on_stage(secret_code=secret_code)
return secret_code
显然,它需要检查生成的代码是否已经不在使用中(例如 - 通过 Redis 生成)。
我的代码中也已经有一个passlib
对象来散列和验证密码
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
我找到了这个类,但不知道如何生成具有 N 个符号长度的短信密码
PS我添加了一个fastapi
标签,因为我正在使用fastapi
并且passlib
被用作它的标准加密工具,文档