提交 e87e82d2 编写于 作者: B Bruce Momjian

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
上级 88ae9cd4
<!--
$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>
......
......@@ -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);
}
......@@ -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
......@@ -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
......
......@@ -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);
......
......@@ -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)
......@@ -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";
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册