OBJS = $(SRCS:.c=.o)
EXTENSION = pgcrypto
-DATA = pgcrypto--1.0.sql pgcrypto--unpackaged--1.0.sql
+DATA = pgcrypto--1.1.sql pgcrypto--1.0--1.1.sql pgcrypto--unpackaged--1.0.sql
REGRESS = init md5 sha1 hmac-md5 hmac-sha1 blowfish rijndael \
$(CF_TESTS) \
--- /dev/null
+/* contrib/pgcrypto/pgcrypto--1.0--1.1.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION pgcrypto UPDATE TO '1.1'" to load this file. \quit
+
+CREATE FUNCTION gen_random_uuid()
+RETURNS uuid
+AS 'MODULE_PATHNAME', 'pg_random_uuid'
+LANGUAGE C VOLATILE;
-/* contrib/pgcrypto/pgcrypto--1.0.sql */
+/* contrib/pgcrypto/pgcrypto--1.1.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pgcrypto" to load this file. \quit
AS 'MODULE_PATHNAME', 'pg_random_bytes'
LANGUAGE C VOLATILE STRICT;
+CREATE FUNCTION gen_random_uuid()
+RETURNS uuid
+AS 'MODULE_PATHNAME', 'pg_random_uuid'
+LANGUAGE C VOLATILE;
+
--
-- pgp_sym_encrypt(data, key)
--
#include "parser/scansup.h"
#include "utils/builtins.h"
+#include "utils/uuid.h"
#include "px.h"
#include "px-crypt.h"
PG_RETURN_BYTEA_P(res);
}
+/* SQL function: gen_random_uuid() returns uuid */
+PG_FUNCTION_INFO_V1(pg_random_uuid);
+
+Datum
+pg_random_uuid(PG_FUNCTION_ARGS)
+{
+ uint8 *buf = (uint8 *) palloc(UUID_LEN);
+ int err;
+
+ /* generate random bits */
+ err = px_get_pseudo_random_bytes(buf, UUID_LEN);
+ if (err < 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
+ errmsg("Random generator error: %s", px_strerror(err))));
+
+ /*
+ * Set magic numbers for a "version 4" (pseudorandom) UUID, see
+ * http://tools.ietf.org/html/rfc4122#section-4.4
+ */
+ buf[6] = (buf[6] & 0x0f) | 0x40; /* "version" field */
+ buf[8] = (buf[8] & 0x3f) | 0x80; /* "variant" field */
+
+ PG_RETURN_UUID_P((pg_uuid_t *) buf);
+}
+
static void *
find_provider(text *name,
PFN provider_lookup,
# pgcrypto extension
comment = 'cryptographic functions'
-default_version = '1.0'
+default_version = '1.1'
module_pathname = '$libdir/pgcrypto'
relocatable = true
Datum pg_encrypt_iv(PG_FUNCTION_ARGS);
Datum pg_decrypt_iv(PG_FUNCTION_ARGS);
Datum pg_random_bytes(PG_FUNCTION_ARGS);
+Datum pg_random_uuid(PG_FUNCTION_ARGS);
#endif
suited for every application. The <xref
linkend="uuid-ossp"> module
provides functions that implement several standard algorithms.
+ The <xref linkend="pgcrypto"> module also provides a generation
+ function for random UUIDs.
Alternatively, UUIDs could be generated by client applications or
other libraries invoked through a server-side function.
</para>
At most 1024 bytes can be extracted at a time. This is to avoid
draining the randomness generator pool.
</para>
+
+ <indexterm>
+ <primary>gen_random_uuid</primary>
+ </indexterm>
+
+<synopsis>
+gen_random_uuid() returns uuid
+</synopsis>
+ <para>
+ Returns a version 4 (random) UUID.
+ </para>
</sect2>
<sect2>
<ulink url="http://www.ossp.org/pkg/lib/uuid/"></ulink>.
</para>
+ <note>
+ <para>
+ The OSSP UUID library is not well maintained, and is becoming increasingly
+ difficult to port to newer platforms. If you only need randomly-generated
+ (version 4) UUIDs, consider using the <function>gen_random_uuid()</>
+ function from the <xref linkend="pgcrypto"> module instead.
+ </para>
+ </note>
+
<sect2>
<title><literal>uuid-ossp</literal> Functions</title>