from hashlib import sha384
def strhash (v):
  # v is a string.
  return sha384 (v.encode()).hexdigest()

# key-value db
# username -> hash of password
users_database = {}

def register (user, password):
  hp = strhash (password)
  if user in users_database:
    print ('Error: user already exists')
  else:
    users_database [user] = strhash (password)

def login (user, given_password):
  if user not in users_database:
    print ('Error: the credentials are not valid')
    return False
  else:
    old_hash_value = users_database [user]
    current_hash_value = strhash (given_password)
    if old_hash_value == current_hash_value:
      print ("It's ok. You are in the system")
      return True
    else: 
      print ('Error: the credentials are not valid')
      return False

def change (user, oldpassword, newpassword):
  if login (user, oldpassword):
    print ('Your password is changed')
    users_database [user] = strhash (newpassword)

# TODO : maybe add some salt
# https://en.wikipedia.org/wiki/Salt_(cryptography)
