Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Lambda cryptography

09 May 2020 » aws
#!/bin/bash

zip="layer-password.zip"
pfolder="python/lib/python3.7/site-packages"

docker run --rm \
    -v `pwd`:/local \
    python:3.7 bash -c "
        apt update && apt install -y zip
        ls /local
        mkdir /layer
        cd /layer
        mkdir -p $pfolder
        pip3 install -t $pfolder cryptography requests
        zip -r /local/$zip .
    "
import json
from cryptography.fernet import Fernet

thekey = b'_jgsRqpJx_6tl64LC9Zlhhx9ohgyuy-HbX8EvYI0L30='

def chash(string):
    if not type(string) is bytes:
    	string = string.encode()
    	
    cipher_suite = Fernet(thekey)
    cipher_text = cipher_suite.encrypt(string)
    return cipher_text
	

def get_credentials(key):
	cipher_suite = Fernet(thekey)
	return passwords[key][0], cipher_suite.decrypt(passwords[key][1]).decode()

def decrypt(code):
    if type(code) is not bytes:
        code = code.encode()
    cipher_suite = Fernet(thekey)
    return cipher_suite.decrypt(code).decode()

#https://stackoverflow.com/questions/40729276/base64-incorrect-padding-error-using-python
def genkey():
    return base64.urlsafe_b64encode(os.urandom(32))
    

def lambda_handler(event, context):
    # TODO implement
    
    mypass = 'mypass'
    print("mypass", mypass)
    code = chash(mypass)
    print("hash", code)
    decode = decrypt(code)
    print("decode", decode)
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
mypass mypass
hash b'gAAAAABetjeL5QuejPTib-UcPLxOMYvDprXzRPvLmcKZ_boUXO8oykS5YWzoI2YFXGbo4yosTH6sJp9LMF9KGNvF8Ph1ebBYrg=='
decode mypass