try something like this:
def hashing(plaintext, salt="", sha="512"):
""" Returns the hashed and encrypted hexdigest of a plaintext and salt"""
app = webapp2.get_app()
# Hashing
if sha == "1":
phrase = hashlib.sha1()
elif sha == "256":
phrase = hashlib.sha256()
else:
phrase = hashlib.sha512()
phrase.update("%s@%s" % (plaintext, salt))
phrase_digest = phrase.hexdigest()
# Encryption (PyCrypto)
# wow... it's so secure :)
try:
from Crypto.Cipher import AES
mode = AES.MODE_CBC
# We can not generate random initialization vector because is difficult to retrieve them later without knowing
# a priori the hash to match. We take 16 bytes from the hexdigest to make the vectors different for each hashed
# plaintext.
iv = phrase_digest[:16]
encryptor = AES.new(app.config.get('aes_key'), mode, iv)
ciphertext = [encryptor.encrypt(chunk) for chunk in chunks(phrase_digest, 16)]
return ''.join(ciphertext)
except Exception, e:
logging.error("CRYPTO is not running: {}".format(e))
raise