Created
April 2, 2018 14:03
-
-
Save awnumar/36f3d1bee75427407f2ddd6fe8ce146a to your computer and use it in GitHub Desktop.
Patern-matching strings in python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment