1

how i said in the title, i want the password to be hashed when is saved. Its possible with this?

    def __OnClickSaveLoginButton(self):
    id = self.idEditLine.GetText()
    pwd = self.pwdEditLine.GetText()
    if (len(id) == 0 or len(pwd) == 0):
        self.PopupNotifyMessage("ID and Password required",self.SetIDEditLineFocus)
        return
    file_object  = open("account.cfg", "w")
    file_object.write(id+"\n"+pwd)
    self.PopupNotifyMessage("Saved.",self.SetIDEditLineFocus)
    file_object.close()

2 Answers2

1

You'll want to use the python hashlib. An example could look something like this:

  import hashlib

  def valid_password(userid, password):
      user = get_user(userid)
      pw_hash = hashlib.sha256(password).hexdigest()
      if user.hash == pw_hash:
          return True
      return False

Also I recommend reviewing some password storage best practices noted in this SO

Edit: I used sh256 in this example, but that is more useful as a message digest. A better use would be hashlib.pbkdf2_hmac or another key derivation function. There is a good write up here.

rsiemens
  • 615
  • 6
  • 15
1

If you're going to hash passwords in Python, as nudies mentioned, hashlib.pbkdf2_hmac is correct.

If you want to save the result in Base64, that's a reasonable option, as it turns it into a character string.

Just remember you also have to store the salt and the number of iterations; choose as high a number of iterations as your CPU can stand.

DO NOT request more bytes of output than the native hash function can support; for instance, PBKDF2-HMAC-SHA-512 caps out at 64 bytes for password hashing; the others less.

I have a fully working example at my github repository, including test vectors for all the normal SHA variants, of which the core piece is

import argparse,hashlib,base64,timeit
BinaryOutput = hashlib.pbkdf2_hmac('sha512',args.password, args.salt, args.iterations, args.outputBytes)
BinaryOutput.encode('base64')
Anti-weakpasswords
  • 2,604
  • 20
  • 25