{ "cells": [ { "cell_type": "markdown", "id": "comic-atmosphere", "metadata": {}, "source": [ "# Secret Key Cryptography" ] }, { "cell_type": "code", "execution_count": 1, "id": "5bb63793-9b18-434c-a198-edb4959fca2f", "metadata": { "tags": [] }, "outputs": [], "source": [ "%load_ext sql" ] }, { "cell_type": "code", "execution_count": 2, "id": "ed24261d-6f7e-4128-ac5e-1fac54d5cd46", "metadata": { "tags": [] }, "outputs": [], "source": [ "%config SqlMagic.feedback=False\n", "%config SqlMagic.displaycon=False\n", "%sql postgresql://postgres@/" ] }, { "cell_type": "code", "execution_count": 3, "id": "9418043b-b04d-41bb-857a-169d69be6c54", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%sql \n", "CREATE EXTENSION IF NOT EXISTS pgsodium;" ] }, { "cell_type": "markdown", "id": "amazing-salon", "metadata": { "tags": [] }, "source": [ "## Authenticated Encryption" ] }, { "cell_type": "markdown", "id": "c9826839-b4e4-4068-86b7-fcfe0abb6f4b", "metadata": { "tags": [] }, "source": [ "### `crypto_secretbox_keygen()`" ] }, { "cell_type": "code", "execution_count": 5, "id": "d0aefe68-b10b-4596-a0bd-223a0063b1b4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b\"\\xb7\\x8e\\x1f\\xefc\\xc8\\xaa\\x9b\\xd45\\xa5\\xf2\\x14'\\n\\xeb\\x82\\x9a\\x945\\x07Z}\\xf33\\x17JA\\x84\\xa7}f\"\n" ] } ], "source": [ "key = %sql select pgsodium.crypto_secretbox_keygen()\n", "key = key[0][0].tobytes()\n", "print(key)" ] }, { "cell_type": "markdown", "id": "6f6f384e-09de-4286-85b8-51c3496ce066", "metadata": {}, "source": [ "### `crypto_secretbox_noncegen()`" ] }, { "cell_type": "code", "execution_count": 6, "id": "9228a21e-4617-416e-bd51-e6fe727eaa73", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\x08\\xa8\\xf4O\\xfe8\\x03\\xd5\\x02\\x8b\\x9e\\xce\\n*\\xe8\\xec\\x02U\\x00x(\\xe0\\x1d\\xea'\n" ] } ], "source": [ "nonce = %sql select pgsodium.crypto_secretbox_noncegen()\n", "nonce = nonce[0][0].tobytes()\n", "print(nonce)" ] }, { "cell_type": "markdown", "id": "536dc335-1b68-41b7-9033-7be0ab4ea492", "metadata": {}, "source": [ "### `crypto_secretbox(message bytea, nonce bytea, key bytea)`" ] }, { "cell_type": "code", "execution_count": 11, "id": "93721b2f-c122-4d4a-bc77-d8aa8bf9a484", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\n!\\x03\\xa1x\\n\\xdd<*\\xa07o\\xde\\xcb,\"t\\xce\\xa1\\xb0\\x13nX\\xf9#q\\xe8\\xedo\\x19~.\\xb0'\n" ] } ], "source": [ "ciphertext = %sql select pgsodium.crypto_secretbox('bob is your uncle', :nonce, :key)\n", "ciphertext = ciphertext[0][0].tobytes()\n", "print(ciphertext)" ] }, { "cell_type": "markdown", "id": "57e7771b-19c2-4cef-b3f7-a83a1682ffeb", "metadata": {}, "source": [ "### `crypto_secretbox_open(ciphertext bytea, nonce bytea, key bytea)`" ] }, { "cell_type": "code", "execution_count": 13, "id": "9a2ead0e-fc04-4c66-9a0f-e4dd0683f2ff", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'bob is your uncle'\n" ] } ], "source": [ "message = %sql select pgsodium.crypto_secretbox_open(:ciphertext, :nonce, :key)\n", "message = message[0][0].tobytes()\n", "print(message)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 5 }