#!/usr/bin/python2

import hashlib
from jinja2.sandbox import SandboxedEnvironment

# We're using only the hashlib functions for demonstratin purposes
# but any algorithm can be implemented and added to the program.

# Define algorithms we support.

def md5(s):
    return hashlib.md5(s).hexdigest()
def sha1(s):
    return hashlib.sha1(s).hexdigest()
def sha224(s):
    return hashlib.sha224(s).hexdigest()
def sha256(s):
    return hashlib.sha256(s).hexdigest()
def sha384(s):
    return hashlib.sha384(s).hexdigest()
def sha512(s):
    return hashlib.sha512(s).hexdigest()

def hasher(algorithm):
    # These are arbitrary variables and values.
    password = 'toor'
    salt = 'libeclipse'

    # Create sandboxed environment.
    env = SandboxedEnvironment()
    template = env.from_string('{{' + algorithm + '}}')

    # Hash the data with the user-defined algorithm.
    hashed = template.render(md5=md5,
                             sha1=sha1,
                             sha224=sha224,
                             sha256=sha256,
                             sha384=sha384,
                             sha512=sha512,
                             salt=salt,
                             password=password)
    return hashed

if __name__ == "__main__":
    # User enters the algorithm. An example could
    # be sha512(md5(salt) + md5(password))

    algorithm = raw_input('Algorithm: ')
    print hasher(algorithm)