【问题标题】:How should I go about coding this password Manager?我应该如何编写此密码管理器?
【发布时间】:2023-01-19 22:58:32
【问题描述】:

我是编程新手,最近一直在尝试学习/理解 OOP。我决定将密码管理器作为一个项目来帮助我学习,因为我已经使用 FP 完成了它。但是,我对自己正在做的事情有些迷茫,非常感谢您的帮助。我迷失了创建用户帐户并从中创建对象的过程。然后在登录功能中使用它来访问用户密码集合并创建新密码或编辑现有密码(如果帐户的凭据与用户字典中的凭据匹配)。但我不知道如何诚实地做到这一点。

关于我的代码风格的任何 cmets,比如它的可读性以及它是否遵循惯例也会有所帮助。

`import random
import string
import secrets

class User():
    def __init__(self, username, credentials, password_dict) -> None:
        self.credentials = {}
        self.password_dict = {}
        
    def passGen(self, passDicts):
        n = int(input('Define password length. Longer passwords are safer.'))
        source = string.ascii_letters + string.digits
        password = ''.join((random.choice(source) for i in range(n)))
        print('Password has been generated!')
        print('Would you like to save this password? Type y or n: ')
        yon = input()
        if yon == 'y':
            site = input('Please enter the site password is to be used:')
            self.password_dict[site] = password
        if yon == 'n':
            print('Okay')
            main()
        return self.password_dict
    #^Generates a new password for a given site and saves it to the users password (dictionary) collection
    def Credentials(self, credentials):
        username = str(input('Enter username: '))
        password = str(input('Enter password for your account'))
        if username in credentials:
            print('')    
        self.credentials[username] = password
       
def main():
    choice = input()
    if choice == '1':
        login()
    if choice == '2':
    `

#^^^ 上面的 main() 用于显示菜单并将用户带到他们保存的密码。选择 1 是
登录和选择 2 是退出。这会将用户带到一个菜单,用户可以在其中检查密码、将密码字典下载为文本文件或编辑指定站点的现有密码。

【问题讨论】:

  • 仅供参考,您应该使用 secrets.choice 而不是 random.choice 来生成密码
  • 你能问一个具体的重点问题并说明你到底被困在什么地方吗? “我迷失了创建用户帐户并从中创建对象的过程“并不是很具体。
  • 另外一个常见的设计选择是将密码管理类与 UI 分开(即向控制台打印消息和从控制台打印消息)

标签: python python-3.x


【解决方案1】:

我将其格式化如下:

import random
import string
import secrets

class User():
    def __init__(self, username, credentials, password_dict) -> None:
        self.credentials = {}
        self.password_dict = {}
        

    def Credentials(self, credentials): # save username and password
        username = str(input('Enter username: '))
        password = str(input('Enter password for your account'))

        if username in credentials:
            print('')    
        self.credentials[username] = password

    def passGen(self, passDicts): # random password generator
        n = int(input('Define password length. Longer passwords are safer.'))

        source = string.ascii_letters + string.digits
        password = ''.join((random.choice(source) for i in range(n)))
        print('Password has been generated!')

        print('Would you like to save this password? Type y or n: ')
        yon = input()

        if yon == 'y':
            site = input('Please enter the site password is to be used:')
            self.password_dict[site] = password
        if yon == 'n':
            print('Okay')
            main()

        return self.password_dict
    
   
def main():
    choice = input()
    if choice == '1':
        login()
    if choice == '2':

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2015-03-22
    • 2014-10-21
    • 2012-05-19
    • 2020-03-21
    • 2011-04-22
    相关资源
    最近更新 更多