Save passwords with Python?

Good evening, I have a question regarding Python and password storage. I sometimes program small systems on my phone using Python with the Pyto app. The problem is that I want to code a password system, but I have no idea how to do it. Can anyone help me?

Best regards

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
CompilerGuru
8 months ago

Passwords may not be stored in plain text. They are usually stored as hash values. A hash value is an encrypted form of the password that can no longer be recalculated. Hashening the password with SHA256 works in Python as follows:

import hashlib
hash=hashlib.sha256("hier_das_Passwort".encode()).hexdigest()

The hash value is stored in the variable hash, which can then be stored in e.g. a file. It is important, however, that a modification of this file is excluded or is automatically recognized by program. This could be solved by encrypting the content to which access is to be accessed with the password (e.g. with Fernet) and decrypting it when registering.

If you want to check if the entered password is correct, you will proceed as follows:

eingegeben="..."
hash=open("datei_mit_dem_hash", "rb").read()

if(hashlib.sha256(eingegeben.encode()).hexdigest()==hash):
    #Das wird ausgeführt wenn das Passwort richtig ist.
else:
    #Das wird ausgeführt wenn das Passwort falsch ist.

The principle behind this is the following: You cannot calculate the hash of the correct password, but you can calculate the hash of the input password. Thus you have the hash value of the correct password and that of the input password. If both agree, the password was correct.

cleanercode
8 months ago
Reply to  CompilerGuru

I personally lack some salt in this solution:(