Skip to content

Commit e87e82d

Browse files
committed
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 Conway
1 parent 88ae9cd commit e87e82d

File tree

7 files changed

+111
-6
lines changed

7 files changed

+111
-6
lines changed

doc/src/sgml/func.sgml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.133 2002/12/05 04:38:29 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.134 2002/12/06 05:20:12 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -1139,6 +1139,16 @@ PostgreSQL documentation
11391139
<entry><literal>trim</literal></entry>
11401140
</row>
11411141

1142+
<row>
1143+
<entry><function>md5</function>(<parameter>string</parameter> <type>text</type>)</entry>
1144+
<entry><type>text</type></entry>
1145+
<entry>
1146+
Calculates the MD5 hash of given string, returning the result in hex.
1147+
</entry>
1148+
<entry><literal>md5('abc')</literal></entry>
1149+
<entry><literal>900150983cd24fb0d6963f7d28e17f72</literal></entry>
1150+
</row>
1151+
11421152
<row>
11431153
<entry><function>pg_client_encoding</function>()</entry>
11441154
<entry><type>name</type></entry>

src/backend/utils/adt/varlena.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.93 2002/11/17 23:01:30 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.94 2002/12/06 05:20:17 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -23,6 +23,7 @@
2323
#include "utils/builtins.h"
2424
#include "utils/pg_locale.h"
2525

26+
extern bool md5_hash(const void *buff, size_t len, char *hexsum);
2627

2728
typedef struct varlena unknown;
2829

@@ -1839,3 +1840,29 @@ to_hex64(PG_FUNCTION_ARGS)
18391840
result_text = PG_STR_GET_TEXT(ptr);
18401841
PG_RETURN_TEXT_P(result_text);
18411842
}
1843+
1844+
/*
1845+
* Create an md5 hash of a text string and return it as hex
1846+
*
1847+
* md5 produces a 16 byte (128 bit) hash; double it for hex
1848+
*/
1849+
#define MD5_HASH_LEN 32
1850+
1851+
Datum
1852+
md5_text(PG_FUNCTION_ARGS)
1853+
{
1854+
char *buff = PG_TEXT_GET_STR(PG_GETARG_TEXT_P(0));
1855+
size_t len = strlen(buff);
1856+
char *hexsum;
1857+
text *result_text;
1858+
1859+
/* leave room for the terminating '\0' */
1860+
hexsum = (char *) palloc(MD5_HASH_LEN + 1);
1861+
1862+
/* get the hash result */
1863+
md5_hash((void *) buff, len, hexsum);
1864+
1865+
/* convert to text and return it */
1866+
result_text = PG_STR_GET_TEXT(hexsum);
1867+
PG_RETURN_TEXT_P(result_text);
1868+
}

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $Id: catversion.h,v 1.167 2002/12/04 05:18:35 momjian Exp $
40+
* $Id: catversion.h,v 1.168 2002/12/06 05:20:24 momjian Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200212031
56+
#define CATALOG_VERSION_NO 200212061
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_proc.h,v 1.278 2002/12/05 04:38:30 momjian Exp $
10+
* $Id: pg_proc.h,v 1.279 2002/12/06 05:20:26 momjian Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3121,6 +3121,9 @@ DESCR("(internal)");
31213121
DATA(insert OID = 2307 ( opaque_out PGNSP PGUID 12 f f t f i 1 2275 "2282" opaque_out - _null_ ));
31223122
DESCR("(internal)");
31233123

3124+
/* cryptographic */
3125+
DATA(insert OID = 2311 ( md5 PGNSP PGUID 12 f f t f i 1 25 "25" md5_text - _null_ ));
3126+
DESCR("calculates md5 hash");
31243127

31253128
/*
31263129
* Symbolic values for provolatile column: these indicate whether the result

src/include/utils/builtins.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: builtins.h,v 1.205 2002/11/08 17:37:52 tgl Exp $
10+
* $Id: builtins.h,v 1.206 2002/12/06 05:20:28 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -482,6 +482,7 @@ extern Datum replace_text(PG_FUNCTION_ARGS);
482482
extern Datum split_text(PG_FUNCTION_ARGS);
483483
extern Datum to_hex32(PG_FUNCTION_ARGS);
484484
extern Datum to_hex64(PG_FUNCTION_ARGS);
485+
extern Datum md5_text(PG_FUNCTION_ARGS);
485486

486487
extern Datum unknownin(PG_FUNCTION_ARGS);
487488
extern Datum unknownout(PG_FUNCTION_ARGS);

src/test/regress/expected/strings.out

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,49 @@ select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff"
777777
ffffffff
778778
(1 row)
779779

780+
--
781+
-- MD5 test suite - from IETF RFC 1321
782+
-- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
783+
--
784+
select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
785+
TRUE
786+
------
787+
t
788+
(1 row)
789+
790+
select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
791+
TRUE
792+
------
793+
t
794+
(1 row)
795+
796+
select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
797+
TRUE
798+
------
799+
t
800+
(1 row)
801+
802+
select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
803+
TRUE
804+
------
805+
t
806+
(1 row)
807+
808+
select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
809+
TRUE
810+
------
811+
t
812+
(1 row)
813+
814+
select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
815+
TRUE
816+
------
817+
t
818+
(1 row)
819+
820+
select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
821+
TRUE
822+
------
823+
t
824+
(1 row)
825+

src/test/regress/sql/strings.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,21 @@ select split_part('@joeuser@mydatabase@','@',2) AS "joeuser";
313313
select to_hex(256*256*256 - 1) AS "ffffff";
314314

315315
select to_hex(256::bigint*256::bigint*256::bigint*256::bigint - 1) AS "ffffffff";
316+
317+
--
318+
-- MD5 test suite - from IETF RFC 1321
319+
-- (see: ftp://ftp.rfc-editor.org/in-notes/rfc1321.txt)
320+
--
321+
select md5('') = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
322+
323+
select md5('a') = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
324+
325+
select md5('abc') = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
326+
327+
select md5('message digest') = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
328+
329+
select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
330+
331+
select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
332+
333+
select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy