randomstr_r02.diff

application/octet-stream

Filename: randomstr_r02.diff
Type: application/octet-stream
Part: 0
Message: Re: Random strings

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: unified
File+
contrib/randomstr/Makefile 41 0
contrib/randomstr/randomstr.c 107 0
contrib/randomstr/randomstr.h 56 0
contrib/randomstr/randomstr.sql.in 2 0
contrib/randomstr/README.randomstr 111 0
diff -Naur pgsql.virg/contrib/randomstr/Makefile pgsql.dev/contrib/randomstr/Makefile
--- pgsql.virg/contrib/randomstr/Makefile	Thu Jan  1 00:00:00 1970
+++ pgsql.dev/contrib/randomstr/Makefile	Wed Aug  8 03:32:50 2001
@@ -0,0 +1,41 @@
+subdir = contrib/randomstr
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+
+# override libdir to install shlib in contrib not main directory
+libdir := $(libdir)/contrib
+
+# shared library parameters
+NAME= randomstr
+SO_MAJOR_VERSION= 0
+SO_MINOR_VERSION= 1
+
+override CPPFLAGS := -I$(srcdir)/src/include $(CPPFLAGS)
+
+OBJS= randomstr.o
+
+all: all-lib $(NAME).sql
+
+# Shared library stuff
+include $(top_srcdir)/src/Makefile.shlib
+
+
+$(NAME).sql: $(NAME).sql.in
+	sed -e 's:MODULE_PATHNAME:$(libdir)/$(shlib):g' < $< > $@
+
+install: all installdirs install-lib
+
+installdirs:
+	$(mkinstalldirs) $(DESTDIR)$(libdir)
+
+uninstall: uninstall-lib
+
+clean distclean maintainer-clean: clean-lib
+	rm -f $(OBJS) $(NAME).sql
+
+depend dep:
+	$(CC) -MM $(CFLAGS) *.c >depend
+
+ifeq (depend,$(wildcard depend))
+include depend
+endif
diff -Naur pgsql.virg/contrib/randomstr/README.randomstr pgsql.dev/contrib/randomstr/README.randomstr
--- pgsql.virg/contrib/randomstr/README.randomstr	Thu Jan  1 00:00:00 1970
+++ pgsql.dev/contrib/randomstr/README.randomstr	Thu Aug  9 06:33:02 2001
@@ -0,0 +1,111 @@
+/*
+ * randomstr
+ *
+ * Functions for generating "good" random strings for IV's, session keys, etc
+ *
+ * Copyright (c) Joseph Conway <joseph.conway@home.com>, 2001;
+ *
+ * randomstr()
+ * -------------
+ * Generates string with a requested number of random bytes
+ * from either /dev/random or /dev/urandom. The result is
+ * returned as bytea
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without a written agreement
+ * is hereby granted, provided that the above copyright notice and this
+ * paragraph and the following two paragraphs appear in all copies.
+ * 
+ * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
+ * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
+ * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ * 
+ * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ *
+ */
+
+
+Version 0.2 (9 August, 2001):
+  Functions for generating "good" random strings for IV's, session keys, etc
+  Tested under Linux (Red Hat 6.2 and 7.0) and PostgreSQL 7.2devel
+
+Release Notes:
+
+  Version 0.2
+    - removed randomstr_hex and renamed randomstr_bytea to randomstr()
+	- added second parameter which allows random bytes to be sourced from
+		/dev/random as well as /dev/urandom
+
+  Version 0.1
+    - initial release    
+
+Installation:
+  Place these files in a directory called 'randomstr' under 'contrib' in the PostgreSQL source tree. Then run:
+
+    make
+    make install
+
+  You can use randomstr.sql to create the functions in your database of choice, e.g.
+
+    psql -U postgres template1 < randomstr.sql
+
+  installs following functions into database template1:
+
+    randomstr(int, text)
+
+Documentation
+==================================================================
+Name
+
+randomstr(int, text)	- generates random string from /dev/urandom
+							or /dev/random
+
+Synopsis
+
+randomstr_hex(int binlen, text source)
+
+Inputs
+
+  binlen - Requested string length, in bytes; if source is 'random',
+			binlen may not exceed 64 bytes. 'urandom' has no
+			enforced limit.
+  source - May be either 'random' or 'urandom'
+
+Outputs
+
+	result is a random string of binlen bytes converted	to bytea;
+		thus the actual output length varies depending on the
+		number of escaped bytes
+
+Example usage
+
+test=# select randomstr(8,'urandom');
+         randomstr
+----------------------------
+ P"\012\215\266\350\307\223
+(1 row)
+
+Use in conjunction with the encode function to produce hex
+or base64 output:
+
+test=# select encode(randomstr(8,'urandom'),'hex');
+      encode
+------------------
+ 3371feee69847d65
+(1 row)
+
+test=# select encode(randomstr(8,'random'),'base64');
+    encode
+--------------
+ X2nsL+tKGXY=
+(1 row)
+
+==================================================================
+-- Joe Conway
+
diff -Naur pgsql.virg/contrib/randomstr/randomstr.c pgsql.dev/contrib/randomstr/randomstr.c
--- pgsql.virg/contrib/randomstr/randomstr.c	Thu Jan  1 00:00:00 1970
+++ pgsql.dev/contrib/randomstr/randomstr.c	Thu Aug  9 06:19:56 2001
@@ -0,0 +1,107 @@
+/*
+ * randomstr.c
+ *
+ * Functions for generating "good" random strings for IV's, session keys, etc
+ *
+ * Copyright (c) Joseph Conway <joseph.conway@home.com>, 2001;
+ *
+ * randomstr()
+ * -------------
+ * Generates string with a requested number of random bytes
+ * from /dev/urandom. The result is returned as binary (bytea)
+ * or hexidecimal (text)
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without a written agreement
+ * is hereby granted, provided that the above copyright notice and this
+ * paragraph and the following two paragraphs appear in all copies.
+ * 
+ * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
+ * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
+ * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ * 
+ * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ *
+ */
+
+#include "randomstr.h"
+
+PG_FUNCTION_INFO_V1(randomstr);
+Datum
+randomstr(PG_FUNCTION_ARGS)
+{
+	int					reqlen;
+	char				*source;
+	char				*randomstr;
+	int					byte;
+	bytea	   			*result;
+
+	reqlen = PG_GETARG_INT32(0);
+	source = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1))));
+	randomstr = gen_rand_str(reqlen, source);
+
+	/*
+	 * get varlena length
+	 */
+	byte = reqlen + VARHDRSZ;
+	result = (bytea *) palloc(byte);
+
+	/*
+	 * set varlena length and data
+	 */
+	result->vl_len = byte;
+	memcpy(result->vl_dat, randomstr, reqlen);
+
+	PG_RETURN_BYTEA_P(result);
+}
+
+
+/*
+ * gen_rand_str: used to produce a random string of any desired
+ * length for use as an IV or a session key
+ */
+char *
+gen_rand_str(int rand_str_len, char *source)
+{
+	FILE			*ptr = NULL;
+	size_t			total_bytes_read = 0;
+	size_t			bytes_read = 0;
+	char			*rand_str;
+
+	if (!(rand_str_len > 0))
+		elog(ERROR, "gen_rand_str: Requested random string length must be > 0");
+
+	if (strcasecmp(source, "random") == 0)
+	{
+		if (rand_str_len > 64)
+			elog(ERROR, "gen_rand_str: When using 'random' device, random string length must not exceed 64 bytes");
+		ptr = fopen("/dev/random", "r");
+	}
+	else if (strcasecmp(source, "urandom") == 0)
+		ptr = fopen("/dev/urandom", "r");
+	else
+		elog(ERROR, "gen_rand_str: Requested random source must be 'random' or 'urandom'");
+
+	if (ptr == NULL)
+		elog(ERROR, "/dev/%s is not available", source);
+
+	rand_str = palloc(rand_str_len);
+	memset(rand_str, '\0', rand_str_len);
+
+	while (total_bytes_read < rand_str_len)
+	{
+		bytes_read = fread(&rand_str[total_bytes_read], sizeof(char), rand_str_len - total_bytes_read, ptr);
+		if (bytes_read < 0)
+			break;
+		total_bytes_read += bytes_read;
+	}
+	fclose(ptr);
+
+	return rand_str;
+}
diff -Naur pgsql.virg/contrib/randomstr/randomstr.h pgsql.dev/contrib/randomstr/randomstr.h
--- pgsql.virg/contrib/randomstr/randomstr.h	Thu Jan  1 00:00:00 1970
+++ pgsql.dev/contrib/randomstr/randomstr.h	Thu Aug  9 00:30:36 2001
@@ -0,0 +1,56 @@
+/*
+ * randomstr.h
+ *
+ * Functions for generating "good" random strings for IV's, session keys, etc
+ *
+ * Copyright (c) Joseph Conway <joseph.conway@home.com>, 2001;
+ *
+ * randomstr()
+ * -------------
+ * Generates string with a requested number of random bytes
+ * from /dev/urandom. The result is returned as binary (bytea)
+ * or hexidecimal (text)
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without a written agreement
+ * is hereby granted, provided that the above copyright notice and this
+ * paragraph and the following two paragraphs appear in all copies.
+ * 
+ * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
+ * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
+ * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ * 
+ * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ *
+ */
+
+#ifndef RANDOMSTR_H
+#define RANDOMSTR_H
+
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "postgres.h"
+#include "fmgr.h"
+#include "utils/builtins.h"
+
+
+/*
+ * External declarations
+ */
+extern Datum randomstr(PG_FUNCTION_ARGS);
+
+/*
+ * internal
+ */
+char *gen_rand_str(int rand_str_len, char *source);
+
+
+#endif	 /* RANDOMSTR_H */
diff -Naur pgsql.virg/contrib/randomstr/randomstr.sql.in pgsql.dev/contrib/randomstr/randomstr.sql.in
--- pgsql.virg/contrib/randomstr/randomstr.sql.in	Thu Jan  1 00:00:00 1970
+++ pgsql.dev/contrib/randomstr/randomstr.sql.in	Thu Aug  9 00:19:32 2001
@@ -0,0 +1,2 @@
+CREATE FUNCTION randomstr (int, text) RETURNS bytea
+  AS 'MODULE_PATHNAME','randomstr' LANGUAGE 'c' with (isstrict);