Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Treat the following as pseudo-code..</p> <pre><code>try: from hashlib import sha as hasher except ImportError: # You could probably exclude the try/except bit, # but older Python distros dont have hashlib. try: import sha as hasher except ImportError: import md5 as hasher def hash_password(password): """Returns the hashed version of a string """ return hasher.new( str(password) ).hexdigest() def load_auth_file(path): """Loads a comma-seperated file. Important: make sure the username doesn't contain any commas! """ # Open the file, or return an empty auth list. try: f = open(path) except IOError: print "Warning: auth file not found" return {} ret = {} for line in f.readlines(): split_line = line.split(",") if len(split_line) &gt; 2: print "Warning: Malformed line:" print split_line continue # skip it.. else: username, password = split_line ret[username] = password #end if #end for return ret def main(): auth_file = "/home/blah/.myauth.txt" u = raw_input("Username:") p = raw_input("Password:") # getpass is probably better.. if auth_file.has_key(u.strip()): if auth_file[u] == hash_password(p): # The hash matches the stored one print "Welcome, sir!" </code></pre> <p>Instead of using a comma-separated file, I would recommend using SQLite3 (which could be used for other settings and such.</p> <p>Also, remember that this isn't very secure - if the application is local, evil users could probably just replace the <code>~/.myauth.txt</code> file.. Local application auth is difficult to do well. You'll have to encrypt any data it reads using the users password, and generally be very careful.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload