{ "cells": [ { "cell_type": "markdown", "id": "40f1de36-e79b-448b-84ae-b3c5d3cabf4e", "metadata": { "tags": [] }, "source": [ "# Generic and Short Hashing" ] }, { "cell_type": "markdown", "id": "80a1d42e-5c83-4388-b549-1fe2460b9f53", "metadata": {}, "source": [ "libsodium provides functions for \"generic\" and \"short\" hashing. \n", "\n", "Generic hashing is suitable for cryptographic purposes using the BLAKE2b algorithm." ] }, { "cell_type": "code", "execution_count": 1, "id": "7f249352-fbd2-4a00-b94a-6e836366e111", "metadata": { "tags": [] }, "outputs": [], "source": [ "%load_ext sql" ] }, { "cell_type": "code", "execution_count": 2, "id": "eb8fa7ea-e634-42a1-8519-9add7844c019", "metadata": { "tags": [] }, "outputs": [], "source": [ "%config SqlMagic.feedback=False\n", "%config SqlMagic.displaycon=False\n", "%sql postgresql://postgres@/" ] }, { "cell_type": "code", "execution_count": 3, "id": "6a6fc4d4-f0fd-4431-8640-85a524f5cd32", "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": "857d49ef-f874-474f-a076-a020ed22b2ae", "metadata": {}, "source": [ "### `crypto_generichash_keygen()`" ] }, { "cell_type": "code", "execution_count": 4, "id": "01f03749-e98e-41d2-a816-71a6f96c64b7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\xf2\\xcc\\xc18\\x8f)\\x93\\x0b^\\x12\\x13\\xa3q\\x9e\\x83\\x03\\xb7\\xb0\\xe9z+/\\x1b\\xd4\\xae\\x1fO\\xa4pj\\xab\\xef'\n" ] } ], "source": [ "k = %sql select pgsodium.crypto_generichash_keygen()\n", "generichash_key = k[0][0].tobytes()\n", "print(generichash_key)" ] }, { "cell_type": "markdown", "id": "bc5faade-d3de-47d7-949d-14ffd6c46b73", "metadata": {}, "source": [ "### `crypto_generichash(message bytea, key bytea = NULL)`" ] }, { "cell_type": "code", "execution_count": 5, "id": "923831fc-1557-4f43-9b3e-340bdeb5dc70", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\x9dJc\\xe5\\xdc\\x1dw\\xed\\x99\\xb6\\xf7V\\x92\\x0e\\xdb\\x89\\xdf\\xda\\xd2|J!\\xf2\\xa9j\\x85\\x82K\\x8f\\xdb_\\xe1'\n" ] } ], "source": [ "signature = %sql select pgsodium.crypto_generichash('this is a message')\n", "print(signature[0][0].tobytes())" ] }, { "cell_type": "code", "execution_count": 6, "id": "4fa44307-63ef-4e96-bf15-991320969774", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'Y\\xc6\"\\x97\\xa5\\x16\\xa4\\xa3\\xaay8\\xe4\\xad)XS\\xa1~UDO)X\\x0bl\\x82\\xa0\\x87\\xba|\\x1al'\n" ] } ], "source": [ "signature = %sql select pgsodium.crypto_generichash('this is a message', :generichash_key)\n", "print(signature[0][0].tobytes())" ] }, { "cell_type": "markdown", "id": "a6d1a008-a5ea-4c80-8c3c-ed67d565b0aa", "metadata": {}, "source": [ "## Short Hashing" ] }, { "cell_type": "markdown", "id": "991bfd3c-8398-4694-b60b-12417d9b474d", "metadata": {}, "source": [ "Many applications and programming language implementations were recently found to be vulnerable to denial-of-service (DoS) attacks when a hash function with weak security guarantees, such as MurmurHash3, was used to construct a hash table.\n", "\n", "To address this, Sodium provides the crypto_shorthash() function, which outputs short but unpredictable (without knowing the secret key) values suitable for picking a list in a hash table for a given key.\n", "\n", "This function is optimized for short inputs.\n", "\n", "The output of this function is only 64 bits. Therefore, it should not be considered collision-resistant.\n", "\n", "Use cases:\n", "- Hash tables\n", "- Probabilistic data structures, such as Bloom filters\n", "- Integrity checking in interactive protocols" ] }, { "cell_type": "markdown", "id": "0fc90170-fcca-4417-9d02-2b8faf49eb1d", "metadata": {}, "source": [ "### `crypto_shorthash_keygen()`" ] }, { "cell_type": "code", "execution_count": 7, "id": "6086244d-8a17-4c36-8405-01c172e02b14", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'%\\x9a\\xedN\\xad\\xa3\\xf5php\\xa5\\x93\\rd\\xe3\\xa2'\n" ] } ], "source": [ "k = %sql select pgsodium.crypto_shorthash_keygen()\n", "shorthash_key = k[0][0].tobytes()\n", "print(shorthash_key)" ] }, { "cell_type": "markdown", "id": "e0f52ba7-8fa7-4fcb-bd43-003cd5420aa7", "metadata": {}, "source": [ "### `crypto_shorthash(message bytea, key bytea)`" ] }, { "cell_type": "code", "execution_count": 8, "id": "ad0367eb-6dfc-4c0e-b609-e5726b69c57c", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'\\xb1\\xfd\\xa4VAjg\\xcc'\n" ] } ], "source": [ "short_signature = %sql select pgsodium.crypto_shorthash('this is a message', :shorthash_key)\n", "print(short_signature[0][0].tobytes())" ] } ], "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 }