Created
August 10, 2021 22:15
-
-
Save rafaelliu/6f19c86bccfed8687e1acbd0020b4bd8 to your computer and use it in GitHub Desktop.
pycryptodome on Lambda using SAM
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
from Crypto.PublicKey import RSA | |
from Crypto.Cipher import PKCS1_OAEP | |
import binascii | |
import base64 | |
DEFAULT_MESSAGE = b'my test essage' | |
def createKeys(): | |
return RSA.generate(1024) | |
def encrypt(msg, pubKey): | |
encryptor = PKCS1_OAEP.new(pubKey) | |
return encryptor.encrypt(msg) | |
def decrypt(encrypted, keyPair): | |
decryptor = PKCS1_OAEP.new(keyPair) | |
return decryptor.decrypt(encrypted) | |
def lambda_handler(event, context): | |
message = event.get("message", DEFAULT_MESSAGE) | |
print(message) | |
keyPair = createKeys() | |
pubKey = keyPair.publickey() | |
encrypted = encrypt(message, pubKey) | |
decrypted = decrypt(encrypted, keyPair) | |
return { | |
"statusCode": 200, | |
"body": { | |
"message": message, | |
"encrypted": base64.b64encode(encrypted), | |
"decrypted": decrypted | |
}, | |
} |
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
$ tree . | |
. | |
├── README.md | |
├── hello_world | |
│ ├── app.py | |
│ └── requirements.txt | |
├── samconfig.toml | |
└── template.yaml |
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
pycryptodome |
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
version = 0.1 | |
[default] | |
[default.deploy] | |
[default.build.parameters] | |
use_container = true |
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
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: AWS::Serverless-2016-10-31 | |
Description: > | |
pycryptodome-lambda | |
Sample SAM Template for pycryptodome-lambda | |
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst | |
Globals: | |
Function: | |
Timeout: 3 | |
Resources: | |
HelloWorldFunction: | |
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction | |
Properties: | |
CodeUri: hello_world/ | |
Handler: app.lambda_handler | |
Runtime: python3.8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment