import hmac
import hashlib
from datetime import datetime
def generate_code(x):
now = datetime.now()
hour_timestamp = int(datetime(now.year, now.month, now.day, now.hour).timestamp())
secret = b"your_secret_key"
message = f"{x}|{hour_timestamp}".encode()
digester = hmac.new(secret, message, hashlib.sha256)
hash_hex = digester.hexdigest()
numbers = "".join([c for c in hash_hex if c.isdigit()])
return (numbers[-6:].zfill(6))[-4:]
def main():
print("=== 动态验证码生成器 ===")
while True:
x = input("\n请输入识别码(输入q退出): ").strip()
if x.lower() == 'q':
break
if not x:
print("错误:识别码不能为空")
continue
try:
code = generate_code(x)
print(f"--> 验证码: {code} <--")
except Exception as e:
print(f"生成失败: {str(e)}")
input("按回车键继续...")
print("\n程序已退出")
if __name__ == "__main__":
main()