Skip to content

Commit

Permalink
First cut adding random data generation function.
Browse files Browse the repository at this point in the history
  • Loading branch information
michelp committed Apr 16, 2017
1 parent 73c16ba commit 0225515
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#http://blog.pgxn.org/post/4783001135/extension-makefiles pg makefiles
EXTENSION = pgsodium
PG_CONFIG ?= pg_config
DATA = $(wildcard *--*.sql)
PGXS := $(shell $(PG_CONFIG) --pgxs)
MODULE_big = pgsodium
OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
#TESTS = $(wildcard test/sql/*.sql)
#REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
#REGRESS_OPTS = --inputdir=test --load-language=plpgsql
include $(PGXS)

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# pgsodium
# pgsodium

Postgres extension to expose libsodium functions to SQL.
3 changes: 3 additions & 0 deletions pgsodium--0.0.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pgsodium" to load this file. \quit

5 changes: 5 additions & 0 deletions pgsodium.control
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# pgsodium extension
comment = 'Postgres extension for libsodium functions'
default_version = '0.0.1'
relocatable = true
requires = ''
36 changes: 36 additions & 0 deletions src/pgsodium.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "pgsodium.h"
#include <sodium.h>
PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(pgsodium_randombytes_random);
Datum
pgsodium_randombytes_random(PG_FUNCTION_ARGS)
{
PG_RETURN_UINT32(randombytes_random());
}

PG_FUNCTION_INFO_V1(pgsodium_randombytes_uniform);
Datum
pgsodium_randombytes_uniform(PG_FUNCTION_ARGS)
{
uint32_t upper_bound = PG_GETARG_UINT32(0);
PG_RETURN_UINT32(randombytes_uniform(upper_bound));
}

PG_FUNCTION_INFO_V1(pgsodium_randombytes_buf);
Datum
pgsodium_randombytes_buf(PG_FUNCTION_ARGS)
{
size_t size = PG_GETARG_UINT32(0);
bytea *ret = (bytea *) palloc(VARHDRSZ + size);
SET_VARSIZE(ret, VARHDRSZ + size);
randombytes_buf(VARDATA(ret), size);
PG_RETURN_BYTEA_P(ret);
}

void _PG_init(void) {
if (sodium_init() == -1) {
elog(ERROR, "_PG_init: sodium_init() failed cannot initialize pgsodium");
return;
}
}
14 changes: 14 additions & 0 deletions src/pgsodium.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef PGSODIUM_H
#define PGSODIUM_H

#include "postgres.h"
#include "utils/builtins.h"
#include "libpq/pqformat.h"

void _PG_init(void);

Datum pgsodium_randombytes_random(PG_FUNCTION_ARGS);
Datum pgsodium_randombytes_uniform(PG_FUNCTION_ARGS);
Datum pgsodium_randombytes_buf(PG_FUNCTION_ARGS);

#endif // PGSODIUM_H

0 comments on commit 0225515

Please sign in to comment.