#!/usr/bin/env python
#
# Created as part of the StratusLab project (http://stratuslab.eu),
# co-funded by the European Commission under the Grant Agreement
# INFSO-RI-261552."
#
# Copyright (c) 2010, SixSq Sarl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import crypt
import getpass
import hashlib
import re

sys.path.append('/var/lib/stratuslab/python')

from stratuslab.Runnable import Runnable

# initialize console logging
import stratuslab.api.LogUtil as LogUtil

LogUtil.get_console_logger()


class MainProgram(Runnable):
    """A command-line program to hash a username and password."""

    def __init__(self):
        self.manifestFile = None
        self.args = None
        super(MainProgram, self).__init__()

    def parse(self):
        self.parser.usage = '''%prog [options]'''

        self.parser.description = '''
Hashes a given username and password in crypt and MD5 formats.  The
values are requested from the standard input.
'''

        self.options, self.args = self.parser.parse_args()

    def checkOptions(self):
        pass

    def doWork(self):
        username, password = self.get_input()
        print self.hash_password(password)
        print self.crypt_password(username, password)

    def get_input(self):
        username = raw_input('username:')
        password1 = getpass.getpass('password:')
        password2 = getpass.getpass('retype password:')

        if password1 != password2:
            raise ValueError("Passwords did not match")

        return username, password1

    def remove_invalid_salt_chars(self, string):
        invalid_salt_chars = re.compile(r"[^\./a-zA-Z0-9]")
        return ''.join(invalid_salt_chars.split(string))

    def crypt_password(self, username, password):
        salt = self.remove_invalid_salt_chars(username + "..")
        salt = salt[:2]
        return "CRYPT:" + crypt.crypt(password, salt)

    def hash_password(self, password):
        m = hashlib.md5()
        m.update(password)
        return "MD5:" + m.hexdigest()


if __name__ == '__main__':
    try:
        MainProgram()
    except KeyboardInterrupt:
        print '\n\nExecution interrupted by the user... goodbye!'
