return hashlib.sha512((pwd + salt).encode('utf-8')).hexdigest()
#!/usr/bin/python3
import hashlib, uuid
SEPARATOR = "&"
# generate hash string with salt
def gen_hash(pwd, salt):
return hashlib.sha512(pwd + salt).hexdigest()
# generage hash password string (for safe to file)
def gen_pw_string(pwd):
salt = uuid.uuid4().hex
return salt + SEPARATOR + gen_hash(pwd, salt)
# parse hash password string, split it by salt and password hash
def parse_pw_string(pwstring):
return pwstring.split(SEPARATOR)
# check password by its password hash string
def check_pwd(pwd, pwstring):
salt, _pwhash = parse_pw_string(pwstring)
pwhash = gen_hash(pwd, salt)
return pwhash == _pwhash
### test time!
pwd1 = "123456"
pwd2 = "qwerty"
# genetate hash strings (may be saved to file or DB)
pwstring1 = gen_pw_string(pwd1)
pwstring2 = gen_pw_string(pwd2)
print(pwd1, pwstring1)
print(pwd2, pwstring2)
# check passwords
# must be True
print(check_pwd(pwd1, pwstring1))
print(check_pwd(pwd2, pwstring2))
# must be False
print(check_pwd(pwd1, pwstring2))
print(check_pwd(pwd2, pwstring1))
('123456', 'f2b56ad9006a475e8c4f9b64446c3f5b&46c2d252a2667cc4b5a754ef5816e981570fd4bd9ced3ed1a6f6aaeae8ae83b795d6ffb66b3fe34650469b1c0d537785c2611157d41ebee6e54dc09527600a0c')
('qwerty', 'f2b56ad9006a475e8c4f9b64446c3f5b&3da9c4223c9fa2c66f5d70432401db14a89ddfe6feb99423083d152f98ef199d484b95b09a6535c766f28223c422cd1d862250867c12c7077144564b5c3fbc79')
True
True
False
False
>>> hashlib.sha512(str("pass" + "&" + "123").encode('utf-8')).hexdigest()
'1d64b5bf20105330e5e12345d73950751daf83f2887c67d03182696d12d4619720f109abb8091afedf0a121c4597a6937a567bb53dbcf2a130557e94ebc36b7b'
hashed_password = hashlib.sha512(registr_password1.get() + "&" + salt).hexdigest()
123456&9876543567888