]> granicus.if.org Git - postgresql/commitdiff
Attached are two small patches to expose md5 as a user function -- including
authorBruce Momjian <bruce@momjian.us>
Fri, 6 Dec 2002 05:20:28 +0000 (05:20 +0000)
committerBruce Momjian <bruce@momjian.us>
Fri, 6 Dec 2002 05:20:28 +0000 (05:20 +0000)
documentation and regression test mods. It seemed small and unobtrusive enough
to not require a specific proposal on the hackers list -- but if not, let me
know and I'll make a pitch. Otherwise, if there are no objections please apply.

Joe Conway

doc/src/sgml/func.sgml
src/backend/utils/adt/varlena.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.h
src/include/utils/builtins.h
src/test/regress/expected/strings.out
src/test/regress/sql/strings.sql

index d3b4e7555e1a0660abd6d1b796580b4d9f1a1662..2262152fd1c3e9504b38af6a6db82d5d351340fb 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.133 2002/12/05 04:38:29 momjian Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.134 2002/12/06 05:20:12 momjian Exp $
 PostgreSQL documentation
 -->
 
@@ -1139,6 +1139,16 @@ PostgreSQL documentation
        <entry><literal>trim</literal></entry>
       </row>
 
+      <row>
+       <entry><function>md5</function>(<parameter>string</parameter> <type>text</type>)</entry>
+       <entry><type>text</type></entry>
+       <entry>
+        Calculates the MD5 hash of given string, returning the result in hex.
+       </entry>
+       <entry><literal>md5('abc')</literal></entry>
+       <entry><literal>900150983cd24fb0d6963f7d28e17f72</literal></entry>
+      </row>
+
       <row>
        <entry><function>pg_client_encoding</function>()</entry>
        <entry><type>name</type></entry>
index 1dd2901bd0ca615622154e2ee9166c2eeab36c70..e98a2dfe0e38b48794b2c8555670f96d3d9d42fd 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.93 2002/11/17 23:01:30 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.94 2002/12/06 05:20:17 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -23,6 +23,7 @@
 #include "utils/builtins.h"
 #include "utils/pg_locale.h"
 
+extern bool md5_hash(const void *buff, size_t len, char *hexsum);
 
 typedef struct varlena unknown;
 
@@ -1839,3 +1840,29 @@ to_hex64(PG_FUNCTION_ARGS)
        result_text = PG_STR_GET_TEXT(ptr);
        PG_RETURN_TEXT_P(result_text);
 }
+
+/*
+ * Create an md5 hash of a text string and return it as hex
+ *
+ * md5 produces a 16 byte (128 bit) hash; double it for hex
+ */
+#define MD5_HASH_LEN  32
+
+Datum
+md5_text(PG_FUNCTION_ARGS)
+{
+       char       *buff = PG_TEXT_GET_STR(PG_GETARG_TEXT_P(0));
+       size_t          len = strlen(buff);
+       char       *hexsum;
+       text       *result_text;
+
+       /* leave room for the terminating '\0' */
+       hexsum = (char *) palloc(MD5_HASH_LEN + 1);
+
+       /* get the hash result */
+       md5_hash((void *) buff, len, hexsum);
+
+       /* convert to text and return it */
+       result_text = PG_STR_GET_TEXT(hexsum);
+       PG_RETURN_TEXT_P(result_text);
+}
index 5f60e7dfcb05a47240b93a22341dbfd1bb11258d..8cb41a9290b80fe485ad08d046daabc34f9b9250 100644 (file)
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: catversion.h,v 1.167 2002/12/04 05:18:35 momjian Exp $
+ * $Id: catversion.h,v 1.168 2002/12/06 05:20:24 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     200212031
+#define CATALOG_VERSION_NO     200212061
 
 #endif
index 2cc2b3519d9f2394f78456a54be59bf09c908789..348646c549097b0b661daf2e47924670058f165c 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: pg_proc.h,v 1.278 2002/12/05 04:38:30 momjian Exp $
+ * $Id: pg_proc.h,v 1.279 2002/12/06 05:20:26 momjian Exp $
  *
  * NOTES
  *       The script catalog/genbki.sh reads this file and generates .bki
@@ -3121,6 +3121,9 @@ DESCR("(internal)");
 DATA(insert OID = 2307 (  opaque_out           PGNSP PGUID 12 f f t f i 1 2275 "2282"  opaque_out - _null_ ));
 DESCR("(internal)");
 
+/* cryptographic */
+DATA(insert OID =  2311 (  md5    PGNSP PGUID 12 f f t f i 1 25 "25"  md5_text - _null_ ));
+DESCR("calculates md5 hash");
 
 /*
  * Symbolic values for provolatile column: these indicate whether the result
index f134bdb49550d21c3325a3e6dd357a0a37d4800c..6a781dab61bd0080fed2f4fb834fb7a9cd6aa350 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: builtins.h,v 1.205 2002/11/08 17:37:52 tgl Exp $
+ * $Id: builtins.h,v 1.206 2002/12/06 05:20:28 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -482,6 +482,7 @@ extern Datum replace_text(PG_FUNCTION_ARGS);
 extern Datum split_text(PG_FUNCTION_ARGS);
 extern Datum to_hex32(PG_FUNCTION_ARGS);
 extern Datum to_hex64(PG_FUNCTION_ARGS);
+extern Datum md5_text(PG_FUNCTION_ARGS);
 
 extern Datum unknownin(PG_FUNCTION_ARGS);
 extern Datum unknownout(PG_FUNCTION_ARGS);
index a73ca1aa84b17a9b5458618dc37ec664eca4877a..bb999785eb12b58c2edfe21aabaeef58d0b334a4 100644 (file)
@@ -777,3 +777,49 @@ select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff"
  ffffffff
 (1 row)
 
+--
+-- MD5 test suite - from IETF RFC 1321
+-- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
+--
+select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
+select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
+ TRUE 
+------
+ t
+(1 row)
+
index c0a18959cd391b5f471207917603838815da1acc..b84d0cb8adb00d36bb9c479a2798e41fde20d137 100644 (file)
@@ -313,3 +313,21 @@ select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
 select to_hex(256*256*256 - 1) AS "ffffff";
 
 select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
+
+--
+-- MD5 test suite - from IETF RFC 1321
+-- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
+--
+select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
+
+select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
+
+select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
+
+select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
+
+select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
+
+select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
+
+select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";