{ "cells": [ { "cell_type": "markdown", "id": "advised-checkout", "metadata": {}, "source": [ "## Hash-based Message Authentication Codes\n", "\n", "[https://en.wikipedia.org/wiki/HMAC]\n", "\n", "In cryptography, an HMAC (sometimes expanded as either keyed-hash\n", "message authentication code or hash-based message authentication code)\n", "is a specific type of message authentication code (MAC) involving a\n", "cryptographic hash function and a secret cryptographic key. As with\n", "any MAC, it may be used to simultaneously verify both the data\n", "integrity and authenticity of a message.\n", "\n", "[C API Documentation](https://doc.libsodium.org/advanced/hmac-sha2)\n", "\n", "pgsodium provides hmacsha512 and hmacsha256, only 512-bit examples are provided below, the 256-bit API is identical but using names like `crypto_auth_hmacsha256_*`.\n" ] }, { "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": "d24f2168-7c64-42e9-b9f6-f6fa4cffe787", "metadata": {}, "source": [ "### `crypto_auth_hmacsha512_keygen()`" ] }, { "cell_type": "code", "execution_count": 4, "id": "dc7110c4-f30b-4c04-a999-1c8f724cab21", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'|\\'\\x88\\xf1\\x88\\x82\\x8b\\xcb\"O\\x1a\\'\\xb8#c\\xa6f\\x1ag\\x05nx5\\xc2\\xe5u8