#!/usr/bin/env python3

"""
Doorlockd -- Binary Kitchen's smart door opener

Copyright (c) Binary Kitchen e.V., 2018

Author:
  Ralf Ramsauer <ralf@binary-kitchen.de>

This work is licensed under the terms of the GNU GPL, version 2.  See
the LICENSE file in the top-level directory.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.
"""

import getpass
import hashlib
import uuid
import sys

if len(sys.argv) != 3:
    print('Usage: %s db username' % sys.argv[0])
    quit(-1)

username = sys.argv[2]
try:
    password = getpass.getpass()
except Exception as error:
    print('ERROR', error)
    quit(-1)

salt = uuid.uuid4().hex
password = hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt

with open(sys.argv[1], 'a') as file:
    file.write('%s %s\n' % (username, password))