{ "cells": [ { "cell_type": "markdown", "id": "92712222-d498-4208-b57d-3f5f69c415a9", "metadata": {}, "source": [ "# Generating Random Data" ] }, { "cell_type": "markdown", "id": "0bfd930d-34bc-45bf-af3c-c25f71fce9ca", "metadata": {}, "source": [ "The library provides a set of functions to generate unpredictable data, suitable for creating secret keys.\n", "\n", "- On Windows systems, the RtlGenRandom() function is used.\n", "- On OpenBSD and Bitrig, the arc4random() function is used.\n", "- On recent FreeBSD and Linux kernels, the getrandom system call is used.\n", "- On other Unices, the /dev/urandom device is used." ] }, { "cell_type": "code", "execution_count": null, "id": "06c57255-4cbf-439c-a7f4-8b5112d14c35", "metadata": { "tags": [] }, "outputs": [], "source": [ "%load_ext sql" ] }, { "cell_type": "code", "execution_count": 2, "id": "7ca6b819-33cd-48b8-9944-5af62eb03b8b", "metadata": { "tags": [] }, "outputs": [], "source": [ "%config SqlMagic.feedback=False\n", "%config SqlMagic.displaycon=False\n", "%sql postgresql://postgres@/" ] }, { "cell_type": "code", "execution_count": 3, "id": "12345bd8-f359-41dc-9a84-d17a14302b29", "metadata": {}, "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": "f7fa16e6-ee55-4764-ab79-6f41cf9899b2", "metadata": {}, "source": [ "### `randombytes_random()`" ] }, { "cell_type": "markdown", "id": "244f1478-89b9-4eaf-9e94-fb925f77c6c3", "metadata": {}, "source": [ "Returns a random 32-bit signed integer." ] }, { "cell_type": "code", "execution_count": 4, "id": "1b29c04b-9ac6-4b13-b19f-564fe4a79f12", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
randombytes_random
-790657505
970732090
934314631
-915187547
-227520694
934389461
" ], "text/plain": [ "[(-790657505,),\n", " (970732090,),\n", " (934314631,),\n", " (-915187547,),\n", " (-227520694,),\n", " (934389461,)]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%sql select pgsodium.randombytes_random() from generate_series(0, 5);" ] }, { "cell_type": "markdown", "id": "63fd868c-0ba4-41b6-95c8-e2652ac7df96", "metadata": {}, "source": [ "### `randombytes_uniform(upper_bound interger)`" ] }, { "cell_type": "markdown", "id": "76eaa1c2-2d6e-476f-918c-5e16d0f23596", "metadata": {}, "source": [ "Returns a uniformally distributed random number between zero and the upper bound argument." ] }, { "cell_type": "code", "execution_count": 5, "id": "cca0f89a-3d24-424f-a1d6-c00e8fd4cd29", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
?column?
9
9
12
5
3
12
" ], "text/plain": [ "[(9,), (9,), (12,), (5,), (3,), (12,)]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%sql select pgsodium.randombytes_uniform(10) + 3 from generate_series(0, 5);" ] }, { "cell_type": "markdown", "id": "eb168c4d-416d-456a-8a1e-8dd3fd8d7b84", "metadata": {}, "source": [ "### `randombytes_buf(buffer_size integer)`" ] }, { "cell_type": "markdown", "id": "bc275d05-5a25-4294-94eb-f0a726d6b69d", "metadata": {}, "source": [ "Returns a random buffer of bytes the size of the argument." ] }, { "cell_type": "code", "execution_count": 6, "id": "4aaafd9a-4ff9-4ca2-bf3a-373615897e5b", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
encode
6d995ff6597b8caa05a0
5e7aa28d67c37e3c8cea
1e3c8cdc8d5836817947
7dac87837187143884b3
fdd853c4111a624d6d92
656962919682a665596d
" ], "text/plain": [ "[('6d995ff6597b8caa05a0',),\n", " ('5e7aa28d67c37e3c8cea',),\n", " ('1e3c8cdc8d5836817947',),\n", " ('7dac87837187143884b3',),\n", " ('fdd853c4111a624d6d92',),\n", " ('656962919682a665596d',)]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%sql select encode(pgsodium.randombytes_buf(10), 'hex') from generate_series(0, 5);" ] } ], "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 }