From: Neil Conway Date: Fri, 20 May 2005 01:29:56 +0000 (+0000) Subject: Implement md5(bytea), update regression tests and documentation. Patch X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=a15667759d1e3c8486664976640c2ef0f0bcc373;p=users%2Fbernd%2Fpostgres.git 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. --- diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 27cbab9291..cb7f3ac3d2 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -2328,6 +2328,17 @@ PostgreSQL documentation 5 + + md5(string) + text + + Calculates the MD5 hash of string, + returning the result in hexadecimal. + + md5('Th\\000omas'::bytea) + 8ab2d3c9689aaf18 b4958c334c82d8b1 + + decode(string text, diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index e37192fa64..f9b50d0267 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -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); +} diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 4d558d9ef1..7d0e65aceb 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 200505171 +#define CATALOG_VERSION_NO 200505201 #endif diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index d14fcbc90b..aec5b180ef 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -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_ )); diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index dc2600390e..b1acc675d0 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -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); diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out index 3421b87522..ab4cd6a797 100644 --- a/src/test/regress/expected/strings.out +++ b/src/test/regress/expected/strings.out @@ -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) + diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql index d2368d6ba5..a59b39cf99 100644 --- a/src/test/regress/sql/strings.sql +++ b/src/test/regress/sql/strings.sql @@ -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";