simple-hash.py 826 B

123456789101112131415161718192021222324252627282930313233343536
  1. import random
  2. import string
  3. def gen_salt (size = 10):
  4. chars = string.ascii_letters + string.punctuation
  5. return ''.join(random.choice(chars) for x in range(size))
  6. def hash_pwd (string, salt=None):
  7. msg = []
  8. if salt == None:
  9. salt = gen_salt(20)
  10. for i, c in enumerate(string):
  11. char_s = ord(salt[len(string) - i])
  12. char_string = ord(c)
  13. msg.append(chr((char_s + char_string) % 127))
  14. res = ''.join(msg)
  15. return (res + salt, salt)
  16. if __name__ == '__main__':
  17. print('Password')
  18. message = input('> ')
  19. hashed_password, salt = hash_pwd(message)
  20. print(f'Encryption: {hashed_password}')
  21. print('Enter password again')
  22. second_password = input('> ')
  23. if (hash_pwd(second_password, salt)[0] == hashed_password):
  24. print('Passwords match! Great!')
  25. else:
  26. print('Passwords don\'t match.')