zoukankan      html  css  js  c++  java
  • 用户注册和登陆系统,采用hmac加密密码(就地txt存储版本)

    import hashlib
    import pickle, os
    import hmac, random


    def hmac_sha1(key, s):
    return hmac.new(key.encode('utf-8'), s.encode('utf-8'), 'MD5').hexdigest()


    # 获取用户名和密码信息
    if os.path.exists('/Users/lewisliu/user_information.txt'):
    with open('/Users/lewisliu/user_information.txt', 'rb') as f:
    user_information = pickle.load(f)
    else:
    user_information = dict()

    # 注册或者登陆
    while True:
    # 选择模式,注册、登陆、退出
    model = input("pleas input enroll/login/exit:")
    if model == 'enroll':
    name = input("please input your name:")
    # 用户名重复识别
    if name in user_information.keys():
    print("name is occupied!")
    continue
    password = input("please input your password:")
    # 数据库添加用户名和密码
    key = 'liu' # ''.join([chr(random.randint(48, 122)) for i in range(20)])
    user_information[name] = hmac_sha1(key, password)
    # sha1 = hashlib.sha1()
    # sha1.update(password.encode('utf-8'))
    # user_information[name] = sha1.hexdigest()
    elif model == 'login':
    name = input("please input your name:")
    if name not in user_information.keys():
    print("name is not exist!")
    continue
    password = input("please input your password:")
    key = 'liu' # ''.join([chr(random.randint(48, 122)) for i in range(20)])
    password = hmac_sha1(key, password)
    # sha1 = hashlib.sha1()
    # sha1.update(password.encode('utf-8'))
    # password = sha1.hexdigest()
    if user_information[name] == password:
    print("Wellcome!")
    else:
    print("wrong name or password! Exit!")
    with open('/Users/lewisliu/user_information.txt', 'wb') as f:
    pickle.dump(user_information, f)
    exit()
    elif model == 'exit':
    with open('/Users/lewisliu/user_information.txt', 'wb') as f:
    pickle.dump(user_information, f)
    exit()
    else:
    print("model wrong!")
  • 相关阅读:
    vim删除以#,空格开头的行
    Element-ui 中对表单进行验证
    VUE页面实现加载外部HTML方法
    vue-cli2嵌入html
    文字环绕图片
    LocalDate计算两个日期相差天数
    springboot+vue脚手架使用nginx前后端分离
    通过 Netty、ZooKeeper 手撸一个 RPC 服务
    Spring Native 项目,把 Spring 项目编译成原生程序!
    印象笔记吐槽
  • 原文地址:https://www.cnblogs.com/LewisAAA/p/9249969.html
Copyright © 2011-2022 走看看