Python 隐藏 raw_input() 密码输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18290340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:24:33  来源:igfitidea点击:

Hiding raw_input() password input

pythonmaskingraw-input

提问by Steven Sharman

I want to hide my password but I don't know how. I have seen show="*"and also getpassbut I don't know how to place them into this code. I'm using Python 2.7.3 and coding on a Raspberry Pi.

我想隐藏我的密码,但我不知道如何。我已经看到了show="*"getpass但我不知道如何将它们放入此代码中。我正在使用 Python 2.7.3 并在 Raspberry Pi 上编码。

ans = True

while ans:
    print("""
                   -------------
                  | 1. Shutdown | 
                  | 2. Items    |
                   -------------
    """)

    ans=raw_input("""

             Please Enter A Number: """)

    if ans == "1":

        exit()
    elif ans == "2":


        pa=raw_input("""

             Please Enter Password: """)

        if pa == "zombiekiller":

            print("""
                   ----------------
                  | 1. Pi password |
                  | 2. Shutdown    |
                   ----------------
            """)

            pe=raw_input ("""

             Please Enter A Number: """)

            if pe == "1":
                print ("""

             Pi's Password Is Adminofpi""")
                import time
                time.sleep(1)
                exit()

            elif pe == "2":
                exit()

            else:
                print("""

             You Have Entered An Inccoredt Option. Terminating Programm""")
                import time
                time.sleep(1)
                exit()

        else:
                print("""

             You Have Entered An Inccorect Password. Terminating Programm""")
                import time
                time.sleep(1)
                exit()

回答by Stephen Whitworth

Use the hashlib library of Python to take the MD5 hash of the input, and compare it against a hashed version of your password in the script. Here'san example of how you could do it.

使用 Python 的 hashlib 库获取输入的 MD5 哈希值,并将其与脚本中密码的哈希版本进行比较。这是一个如何做到这一点的示例。

回答by tamasgal

getpasshides the input, just replace raw_inputafter importing the module getpass, like this:

getpass隐藏输入,只需raw_input在导入模块后替换getpass,如下所示:

import getpass
.
.
.
pa = getpass.getpass()

回答by gabor_the_kid

Never store userId and password in a source file, that's security vulnerability!

永远不要将用户 ID 和密码存储在源文件中,这是安全漏洞!

Store them in a text file and encrypt them with some symmetric key cryptography (at least MD5, or the currently suggested min standard, the SHA-3) to encode the password.

将它们存储在文本文件中,并使用一些对称密钥密码术(至少是 MD5,或当前建议的最小标准,SHA-3)对它们进行加密以对密码进行编码。

The result would look something like:

结果将类似于:

:admin:$1$dqx/Wdy5$QQrH98XjvFBOm6vqu3qN/1::Administrator:admin:[email protected]:

:admin:$1$dqx/Wdy5$QQrH98XjvFBOm6vqu3qN/1::Administrator:admin:[email protected]:

In your code block your read the file and use the same algorithm to decrypt the password.

在您的代码块中,您读取文件并使用相同的算法来解密密码。