md5

Started by Joe Conwayover 23 years ago3 messagespatches
Jump to latest
#1Joe Conway
mail@joeconway.com

Attached are two small patches to expose md5 as a user function -- including
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

Attachments:

md5.regress.1.patchtext/plain; name=md5.regress.1.patchDownload+64-0
md5.1.patchtext/plain; name=md5.1.patchDownload+41-0
#2Bruce Momjian
bruce@momjian.us
In reply to: Joe Conway (#1)
Re: md5

Your patch has been added to the PostgreSQL unapplied patches list at:

http://momjian.postgresql.org/cgi-bin/pgpatches

I will try to apply it within the next 48 hours.

---------------------------------------------------------------------------

Joe Conway wrote:

Attached are two small patches to expose md5 as a user function -- including
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

Index: src/test/regress/expected/strings.out
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/test/regress/expected/strings.out,v
retrieving revision 1.19
diff -c -r1.19 strings.out
*** src/test/regress/expected/strings.out	22 Sep 2002 17:27:25 -0000	1.19
--- src/test/regress/expected/strings.out	5 Dec 2002 05:35:41 -0000
***************
*** 777,779 ****
--- 777,825 ----
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: src/test/regress/sql/strings.sql
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/test/regress/sql/strings.sql,v
retrieving revision 1.13
diff -c -r1.13 strings.sql
*** src/test/regress/sql/strings.sql	22 Sep 2002 17:27:25 -0000	1.13
--- src/test/regress/sql/strings.sql	5 Dec 2002 05:35:09 -0000
***************
*** 313,315 ****
--- 313,333 ----
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";
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /opt/src/cvs/pgsql-server/doc/src/sgml/func.sgml,v
retrieving revision 1.133
diff -c -r1.133 func.sgml
*** doc/src/sgml/func.sgml	5 Dec 2002 04:38:29 -0000	1.133
--- doc/src/sgml/func.sgml	5 Dec 2002 05:16:55 -0000
***************
*** 1140,1145 ****
--- 1140,1155 ----
</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>
<entry>
Index: src/backend/utils/adt/varlena.c
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/backend/utils/adt/varlena.c,v
retrieving revision 1.93
diff -c -r1.93 varlena.c
*** src/backend/utils/adt/varlena.c	17 Nov 2002 23:01:30 -0000	1.93
--- src/backend/utils/adt/varlena.c	5 Dec 2002 04:36:11 -0000
***************
*** 23,28 ****
--- 23,29 ----
#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;

***************
*** 1837,1841 ****
--- 1838,1868 ----
} while (ptr > buf && value);
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: src/include/catalog/pg_proc.h
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/include/catalog/pg_proc.h,v
retrieving revision 1.278
diff -c -r1.278 pg_proc.h
*** src/include/catalog/pg_proc.h	5 Dec 2002 04:38:30 -0000	1.278
--- src/include/catalog/pg_proc.h	5 Dec 2002 04:44:54 -0000
***************
*** 3121,3126 ****
--- 3121,3129 ----
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: src/include/utils/builtins.h
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/include/utils/builtins.h,v
retrieving revision 1.205
diff -c -r1.205 builtins.h
*** src/include/utils/builtins.h	8 Nov 2002 17:37:52 -0000	1.205
--- src/include/utils/builtins.h	5 Dec 2002 04:56:37 -0000
***************
*** 482,487 ****
--- 482,488 ----
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);

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#3Bruce Momjian
bruce@momjian.us
In reply to: Joe Conway (#1)
Re: md5

Patch applied. Thanks.

Catversion updated so MD5 is accessible to regression tests, so the
regression tests pass.

---------------------------------------------------------------------------

Joe Conway wrote:

Attached are two small patches to expose md5 as a user function -- including
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

Index: src/test/regress/expected/strings.out
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/test/regress/expected/strings.out,v
retrieving revision 1.19
diff -c -r1.19 strings.out
*** src/test/regress/expected/strings.out	22 Sep 2002 17:27:25 -0000	1.19
--- src/test/regress/expected/strings.out	5 Dec 2002 05:35:41 -0000
***************
*** 777,779 ****
--- 777,825 ----
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: src/test/regress/sql/strings.sql
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/test/regress/sql/strings.sql,v
retrieving revision 1.13
diff -c -r1.13 strings.sql
*** src/test/regress/sql/strings.sql	22 Sep 2002 17:27:25 -0000	1.13
--- src/test/regress/sql/strings.sql	5 Dec 2002 05:35:09 -0000
***************
*** 313,315 ****
--- 313,333 ----
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";
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /opt/src/cvs/pgsql-server/doc/src/sgml/func.sgml,v
retrieving revision 1.133
diff -c -r1.133 func.sgml
*** doc/src/sgml/func.sgml	5 Dec 2002 04:38:29 -0000	1.133
--- doc/src/sgml/func.sgml	5 Dec 2002 05:16:55 -0000
***************
*** 1140,1145 ****
--- 1140,1155 ----
</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>
<entry>
Index: src/backend/utils/adt/varlena.c
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/backend/utils/adt/varlena.c,v
retrieving revision 1.93
diff -c -r1.93 varlena.c
*** src/backend/utils/adt/varlena.c	17 Nov 2002 23:01:30 -0000	1.93
--- src/backend/utils/adt/varlena.c	5 Dec 2002 04:36:11 -0000
***************
*** 23,28 ****
--- 23,29 ----
#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;

***************
*** 1837,1841 ****
--- 1838,1868 ----
} while (ptr > buf && value);
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: src/include/catalog/pg_proc.h
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/include/catalog/pg_proc.h,v
retrieving revision 1.278
diff -c -r1.278 pg_proc.h
*** src/include/catalog/pg_proc.h	5 Dec 2002 04:38:30 -0000	1.278
--- src/include/catalog/pg_proc.h	5 Dec 2002 04:44:54 -0000
***************
*** 3121,3126 ****
--- 3121,3129 ----
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: src/include/utils/builtins.h
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/include/utils/builtins.h,v
retrieving revision 1.205
diff -c -r1.205 builtins.h
*** src/include/utils/builtins.h	8 Nov 2002 17:37:52 -0000	1.205
--- src/include/utils/builtins.h	5 Dec 2002 04:56:37 -0000
***************
*** 482,487 ****
--- 482,488 ----
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);

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073