A template for falcon asgi api with pycryptodome. All encryption software is from pycryptodome, and in regards to tls, uvicorn.
The template uses pycryptodome Salsa20 and reads the POST data as a stream.
The template outputs as serialized hex for ease of transport. If you need to optimize the network usage and speed, or because you want the raw plaintext/original in the decrypt api output, you can skip the hex encode there.
Example generating a 32 byte (256 bit) string manually:
cat /dev/urandom | head -n10 | xxd -p | tr -d '\n' | cut -c1-32
Example on_post function in Falcon:
async def on_post(self, req, resp):
"""Handles POST requests for encryption."""
datab = await req.stream.read()
key = b'$my_32byte_keygoeshere'
cipher = Salsa20.new(key)
ciphertext = cipher.nonce + cipher.encrypt(datab)
resp.text = binascii.hexlify(ciphertext)
And then we can set custom routes to different classes etc.
The template has 4 routes, two sets of encrypt and decrypt methods, serialized hex format or raw data.
Here we are changing the decrypt route to a differing URI context, something you may want in some situations.
encrypt = SalfalXResource()
decrypt = SalfalDResource()
app.add_route('/api/encrypt/0', encrypt)
app.add_route('/api/decrypt/c6fbe4491f2011cc1d5', decrypt)
curl -X POST --data 'what up, encrypt this real quick' https://myremotefalconservice:8000/api/encrypt/0
Using "time" to measure the time for the cURL, and some more verbose cURL options, non-https local listener
time curl --request POST --data-binary '@anything.dat' http://localhost:8000/api/encrypt/0 -o anything.dat.asc
time curl -X POST --data-binary '@anything.dat.asc' http://localhost:8000/api/decrypt/0 -o anything.dat
time curl -X POST --data-binary '@anything.dat' http://localhost:8000/api/encrypt/0 -o anything.dat
time curl -X POST --data-binary 'I am encrypting this message.' http://localhost:8000/api/encrypt/0 -o encryptedinput.asc
time curl -X POST --data-binary 'I am encrypting this message.' http://localhost:8000/api/encrypt/1 -o encryptedinput.asc
time curl -X POST --data-binary '73ceea5c2c2c67f359c2896815d452bf413565a9d13d0736cc54aff6bb71e3bb5377f16fe213' http://localhost:8000/api/decrypt/0 | xxd -r -p
uvicorn salsa_falcon:app --log-level=trace --host=0.0.0.0 --port=8000 --ssl-certfile /etc/cert.pem --ssl-keyfile /etc/key.pem