Hiding raw_input() password input

假如想象 提交于 2019-12-18 03:51:35

问题


I want to hide my password but I don't know how. I have seen show="*" and also getpass but I don't know how to place them into this code. I'm using Python 2.7.3 and coding on a 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()

回答1:


getpass hides the input, just replace raw_input after importing the module getpass, like this:

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



回答2:


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's an example of how you could do it.




回答3:


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

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.

The result would look something like:

:admin:$1$dqx/Wdy5$QQrH98XjvFBOm6vqu3qN/1::Administrator:admin:changeme@example.com:

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



来源:https://stackoverflow.com/questions/18290340/hiding-raw-input-password-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!