我正在嘗試撰寫一個可以像這樣作業的代碼:
代碼的第一個功能是注冊:在這里用戶將輸入他們的用戶名和密碼,然后代碼將創建一個以用戶名命名的文本檔案,它將包括 2 行:
1.username
2.password
import time
def registracia():
print ("To register you'll need to type your username and your password.")
time.sleep (1.5)
userName = input("username: ")
time.sleep (1.5)
passWord = input("password: ")
filename = userName ".txt"
file = open(filename, " w")
file.write (userName "\n")
file.write (passWord)
file.close
if __name__ == "__main__":
registracia()
然后是代碼的第二個功能,即登錄。我對登錄的想法是它會要求輸入用戶名,然后它會打開以用戶名命名的檔案并讀取第二行,讀取該行后它會要求輸入密碼,然后使用 if 檢查是否輸入等于第二行。
我的問題是 readline 只從用戶名中讀取 1 個字母,至少我認為它是這樣做的。
import time
print ("To login you'll need to type your username first")
def login():
#time.sleep (1.5)
userName = input ("username: ")
filename = userName ".txt"
file = open(filename, "r")
#time.sleep (1.5)
realPassWord = file.readline(2)
print (realPassWord)
passWord = input ("password: ")
if passWord == realPassWord:
print ("successfuly logged in ")
elif passWord != realPassWord:
print ("incorrect password")
login()
login()
我還添加了一個輸入和輸出示例,以查看發生了什么:
登記:
輸入:
username: abc321
password: cba123
輸出:
abc321.txt(文本檔案):
abc321
cba123
登錄:
輸入:
abc321
輸出(輸出不應該是真正可見的,輸出是代碼對密碼輸入的期望,但我添加了列印,realPassWord所以我可以看到代碼期望的內容):
ab
期望的輸出:
cba123
uj5u.com熱心網友回復:
代碼的邏輯看起來沒問題。我想修復一些拼寫錯誤或語法錯誤。我沒有解決樣式指南問題。為此,我建議遵循PEP8 編碼指南,尤其是在命名變數時。請在 Python 中使用snake_case而不是camelCase。也不要忘記file.close()用 () - 括號呼叫。
import time
def registracia():
print("To register you'll need to type your username and your password.")
time.sleep(1.5)
userName = input("username: ")
time.sleep(1.5)
passWord = input("password: ")
filename = userName ".txt"
file = open(filename, "w ")
file.write(userName "\n" passWord)
file.close()
if __name__ == "__main__":
registracia()
對于登錄部分,問題在于,file.readline(2)它只讀取前兩個位元組。因此,您需要使用將檔案內容拆分為行的東西。在您的情況下,您可以使用file.read().splitlines()[1].
import time
print ("To login you'll need to type your username first")
def login():
#time.sleep (1.5)
userName = input("username: ")
filename = userName ".txt"
file = open(filename, "r")
#time.sleep (1.5)
realPassWord = file.read().splitlines()[1]
print(realPassWord)
passWord = input("password: ")
if passWord == realPassWord:
print("successfuly logged in ")
elif passWord != realPassWord:
print("incorrect password")
login()
login()
只是一個說明:
我永遠不會建議將密碼作為純文本存盤到文本檔案中。如果你認真考慮使用它,那么一定要對你的密碼進行哈希處理,甚至可能將它們存盤到資料庫中。
uj5u.com熱心網友回復:
您的代碼的核心問題是您如何從檔案中讀取。
我還要指出一些其他的事情。
您將用戶名存盤在檔案中,但您從不使用它。以純文本形式在檔案中包含密碼和用戶名似乎不是很好的安全性。
密碼當前也在輸入時回顯到終端。
Python 有一些你可能想看的功能:
Python 具有密碼輸入功能,可以提示用戶輸入密碼而不回顯:
- https://docs.python.org/3/library/getpass.html
散列存盤在檔案中的密碼值:
- https://docs.python.org/3/library/hashlib.html#hashlib.scrypt
使用 UUID 作為檔案名而不是用戶名:
- https://docs.python.org/3/library/uuid.html
Pathlib 用于簡化檔案的讀寫:
- https://docs.python.org/3/library/pathlib.html#pathlib.Path.read_bytes
- https://docs.python.org/3/library/pathlib.html#pathlib.Path.write_bytes
密碼比較與“恒定時間比較”以降低定時攻擊的風險
- https://docs.python.org/3/library/secrets.html#secrets.compare_digest
以下內容并不詳盡,可能不適合您的情況。這只是使用這些的一個例子:
from getpass import getpass
from hashlib import md5, scrypt
from pathlib import Path
from secrets import token_bytes, compare_digest
import time
import uuid
SALT_SIZE = 64
def filename_uuid(filename):
_hash = md5()
_hash.update(filename.encode('utf-8'))
uuid_name = uuid.UUID(_hash.hexdigest())
return Path(f"{uuid_name}.txt")
def password_hash(password, salt):
hash_value = scrypt(password, salt=salt, n=16384, r=8, p=1)
return salt hash_value
def check_password(password, stored_value):
salt = stored_value[:SALT_SIZE]
_password = password_hash(password, salt)
return compare_digest(_password, stored_value)
def registracia():
print("To register type your username and your password.")
username = input("username: ")
password = getpass().encode('utf8')
salt = token_bytes(SALT_SIZE)
token = password_hash(password, salt)
filename = filename_uuid(username)
filename.write_bytes(token)
def login(attempts=0):
print("To login you'll need to type your username first")
username = input("username: ")
filename = filename_uuid(username)
try:
token = filename.read_bytes().strip()
except FileNotFoundError:
token = b""
password = getpass().encode('utf8')
if check_password(password, token):
print("successfully logged in ")
else:
print("incorrect username/password combination")
if attempts < 1:
login(attempts 1)
if __name__ == "__main__":
registracia()
login()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524313.html
標籤:Python文件
上一篇:為什么Node.js中的Node.jsfs.statSync出生時間關閉?
下一篇:關于我的代碼,編譯錯誤“執行緒“主”java.util.NoSuchElementException中的例外”是什么意思?
