提交 f3567eea 编写于 作者: N Neil Conway

Implement md5(bytea), update regression tests and documentation. Patch

from Abhijit Menon-Sen, minor editorialization from Neil Conway. Also,
improve md5(text) to allocate a constant-sized buffer on the stack
rather than via palloc.

Catalog version bumped.
上级 ff0c143a
<!--
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.247 2005/05/11 13:58:50 neilc Exp $
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.248 2005/05/20 01:29:55 neilc Exp $
PostgreSQL documentation
-->
......@@ -2328,6 +2328,17 @@ PostgreSQL documentation
<entry><literal>5</literal></entry>
</row>
<row>
<entry><literal><function>md5</function>(<parameter>string</parameter>)</literal></entry>
<entry><type>text</type></entry>
<entry>
Calculates the MD5 hash of <parameter>string</parameter>,
returning the result in hexadecimal.
</entry>
<entry><literal>md5('Th\\000omas'::bytea)</literal></entry>
<entry><literal>8ab2d3c9689aaf18 b4958c334c82d8b1</literal></entry>
</row>
<row>
<entry>
<literal><function>decode</function>(<parameter>string</parameter> <type>text</type>,
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.120 2005/05/01 18:56:18 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.121 2005/05/20 01:29:55 neilc Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -2308,15 +2308,12 @@ md5_text(PG_FUNCTION_ARGS)
{
text *in_text = PG_GETARG_TEXT_P(0);
size_t len;
char *hexsum;
char hexsum[MD5_HASH_LEN + 1];
text *result_text;
/* Calculate the length of the buffer using varlena metadata */
len = VARSIZE(in_text) - VARHDRSZ;
/* leave room for the terminating '\0' */
hexsum = (char *) palloc(MD5_HASH_LEN + 1);
/* get the hash result */
if (md5_hash(VARDATA(in_text), len, hexsum) == false)
ereport(ERROR,
......@@ -2327,3 +2324,25 @@ md5_text(PG_FUNCTION_ARGS)
result_text = PG_STR_GET_TEXT(hexsum);
PG_RETURN_TEXT_P(result_text);
}
/*
* Create an md5 hash of a bytea field and return it as a hex string:
* 16-byte md5 digest is represented in 32 hex characters.
*/
Datum
md5_bytea(PG_FUNCTION_ARGS)
{
bytea *in = PG_GETARG_BYTEA_P(0);
size_t len;
char hexsum[MD5_HASH_LEN + 1];
text *result_text;
len = VARSIZE(in) - VARHDRSZ;
if (md5_hash(VARDATA(in), len, hexsum) == false)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
result_text = PG_STR_GET_TEXT(hexsum);
PG_RETURN_TEXT_P(result_text);
}
......@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.268 2005/05/17 21:46:10 tgl Exp $
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.269 2005/05/20 01:29:55 neilc Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 200505171
#define CATALOG_VERSION_NO 200505201
#endif
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.362 2005/05/17 21:46:10 tgl Exp $
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.363 2005/05/20 01:29:55 neilc Exp $
*
* NOTES
* The script catalog/genbki.sh reads this file and generates .bki
......@@ -3269,6 +3269,8 @@ DESCR("I/O");
/* cryptographic */
DATA(insert OID = 2311 ( md5 PGNSP PGUID 12 f f t f i 1 25 "25" _null_ _null_ _null_ md5_text - _null_ ));
DESCR("calculates md5 hash");
DATA(insert OID = 2321 ( md5 PGNSP PGUID 12 f f t f i 1 25 "17" _null_ _null_ _null_ md5_bytea - _null_ ));
DESCR("calculates md5 hash");
/* crosstype operations for date vs. timestamp and timestamptz */
DATA(insert OID = 2338 ( date_lt_timestamp PGNSP PGUID 12 f f t f i 2 16 "1082 1114" _null_ _null_ _null_ date_lt_timestamp - _null_ ));
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.255 2005/04/12 04:26:32 tgl Exp $
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.256 2005/05/20 01:29:55 neilc Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -572,6 +572,7 @@ extern Datum array_to_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 md5_bytea(PG_FUNCTION_ARGS);
extern Datum unknownin(PG_FUNCTION_ARGS);
extern Datum unknownout(PG_FUNCTION_ARGS);
......
......@@ -825,3 +825,45 @@ select md5('12345678901234567890123456789012345678901234567890123456789012345678
t
(1 row)
select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
TRUE
------
t
(1 row)
select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
TRUE
------
t
(1 row)
select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
TRUE
------
t
(1 row)
select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
TRUE
------
t
(1 row)
select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
TRUE
------
t
(1 row)
select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
TRUE
------
t
(1 row)
select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
TRUE
------
t
(1 row)
......@@ -331,3 +331,17 @@ select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS
select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册