Task:

The script must read the text from the in.txt file, prompt the user for the password that must be entered by him from the keyboard. Further, the source text is hashed using the md5 algorithm and is written to the new file.

Comment to the code written:

"The solution is still illogical. Write just so that the password entered by the user is encrypted and written to the file."

import hashlib as hl f = open('C:\\abc\in.txt', 'w') p = 'Zolotie Kupola' if p == input('Enter the password: ', ): h = hl.md5(b'C:\\abc\in.txt') f.write(str(h.hexdigest())) else: print('Uncorrect password') f.close() 
  • Probably the idea of ​​the task is to password-protect the file, the commentator noticed the illogicality in coaxing the password in the code. I think it is worth writing a hash to the file and comparing it with the hash of the entered password. - Sarck
  • Specify, so what is your question? or do you need to write down the decision for you and get credit for you too? - Kromster
  • @Kromster What was my decision illogical? - Vincent Powers

1 answer 1

Let's take the steps:

 import hashlib as hl # Скрипт должен считывать текст из файла in.txt with open(r'C:\abc\in.txt', encoding='utf-8') as f: text = f.read() # ...запрашивать у пользователя пароль, который должен быть введен им с клавиатуры password = input('Enter the password: ') # Содержимое файла + введенный пароль data = text + password # В хеш попадает байтовая строка h = hl.md5(data.encode('utf-8')) # Сохранение в новый файл with open(r'C:\abc\out.txt', 'w', encoding='utf-8') as f: f.write(h.hexdigest()) 

Ps. You can use getpass to enter secret information.

To do this, import and use instead of input :

 from getpass import getpass password = getpass('Enter the password: ') 
  • one
    Instead of input you can use getpass to enter a password, then it will not even be shown on the screen. - Avernial
  • @Avernial, thanks, added an example with getpass - gil9red