Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f3567ee

Browse files
author
Neil Conway
committed
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.
1 parent ff0c143 commit f3567ee

File tree

7 files changed

+99
-10
lines changed

7 files changed

+99
-10
lines changed

doc/src/sgml/func.sgml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.247 2005/05/11 13:58:50 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.248 2005/05/20 01:29:55 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -2328,6 +2328,17 @@ PostgreSQL documentation
23282328
<entry><literal>5</literal></entry>
23292329
</row>
23302330

2331+
<row>
2332+
<entry><literal><function>md5</function>(<parameter>string</parameter>)</literal></entry>
2333+
<entry><type>text</type></entry>
2334+
<entry>
2335+
Calculates the MD5 hash of <parameter>string</parameter>,
2336+
returning the result in hexadecimal.
2337+
</entry>
2338+
<entry><literal>md5('Th\\000omas'::bytea)</literal></entry>
2339+
<entry><literal>8ab2d3c9689aaf18 b4958c334c82d8b1</literal></entry>
2340+
</row>
2341+
23312342
<row>
23322343
<entry>
23332344
<literal><function>decode</function>(<parameter>string</parameter> <type>text</type>,

src/backend/utils/adt/varlena.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.120 2005/05/01 18:56:18 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/varlena.c,v 1.121 2005/05/20 01:29:55 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2308,15 +2308,12 @@ md5_text(PG_FUNCTION_ARGS)
23082308
{
23092309
text *in_text = PG_GETARG_TEXT_P(0);
23102310
size_t len;
2311-
char *hexsum;
2311+
char hexsum[MD5_HASH_LEN + 1];
23122312
text *result_text;
23132313

23142314
/* Calculate the length of the buffer using varlena metadata */
23152315
len = VARSIZE(in_text) - VARHDRSZ;
23162316

2317-
/* leave room for the terminating '\0' */
2318-
hexsum = (char *) palloc(MD5_HASH_LEN + 1);
2319-
23202317
/* get the hash result */
23212318
if (md5_hash(VARDATA(in_text), len, hexsum) == false)
23222319
ereport(ERROR,
@@ -2327,3 +2324,25 @@ md5_text(PG_FUNCTION_ARGS)
23272324
result_text = PG_STR_GET_TEXT(hexsum);
23282325
PG_RETURN_TEXT_P(result_text);
23292326
}
2327+
2328+
/*
2329+
* Create an md5 hash of a bytea field and return it as a hex string:
2330+
* 16-byte md5 digest is represented in 32 hex characters.
2331+
*/
2332+
Datum
2333+
md5_bytea(PG_FUNCTION_ARGS)
2334+
{
2335+
bytea *in = PG_GETARG_BYTEA_P(0);
2336+
size_t len;
2337+
char hexsum[MD5_HASH_LEN + 1];
2338+
text *result_text;
2339+
2340+
len = VARSIZE(in) - VARHDRSZ;
2341+
if (md5_hash(VARDATA(in), len, hexsum) == false)
2342+
ereport(ERROR,
2343+
(errcode(ERRCODE_OUT_OF_MEMORY),
2344+
errmsg("out of memory")));
2345+
2346+
result_text = PG_STR_GET_TEXT(hexsum);
2347+
PG_RETURN_TEXT_P(result_text);
2348+
}

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-2005, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.268 2005/05/17 21:46:10 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.269 2005/05/20 01:29:55 neilc Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200505171
56+
#define CATALOG_VERSION_NO 200505201
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.362 2005/05/17 21:46:10 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.363 2005/05/20 01:29:55 neilc Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3269,6 +3269,8 @@ DESCR("I/O");
32693269
/* cryptographic */
32703270
DATA(insert OID = 2311 ( md5 PGNSP PGUID 12 f f t f i 1 25 "25" _null_ _null_ _null_ md5_text - _null_ ));
32713271
DESCR("calculates md5 hash");
3272+
DATA(insert OID = 2321 ( md5 PGNSP PGUID 12 f f t f i 1 25 "17" _null_ _null_ _null_ md5_bytea - _null_ ));
3273+
DESCR("calculates md5 hash");
32723274

32733275
/* crosstype operations for date vs. timestamp and timestamptz */
32743276
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_ ));

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-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.255 2005/04/12 04:26:32 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.256 2005/05/20 01:29:55 neilc Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -572,6 +572,7 @@ extern Datum array_to_text(PG_FUNCTION_ARGS);
572572
extern Datum to_hex32(PG_FUNCTION_ARGS);
573573
extern Datum to_hex64(PG_FUNCTION_ARGS);
574574
extern Datum md5_text(PG_FUNCTION_ARGS);
575+
extern Datum md5_bytea(PG_FUNCTION_ARGS);
575576

576577
extern Datum unknownin(PG_FUNCTION_ARGS);
577578
extern Datum unknownout(PG_FUNCTION_ARGS);

src/test/regress/expected/strings.out

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,3 +825,45 @@ select md5('12345678901234567890123456789012345678901234567890123456789012345678
825825
t
826826
(1 row)
827827

828+
select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
829+
TRUE
830+
------
831+
t
832+
(1 row)
833+
834+
select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
835+
TRUE
836+
------
837+
t
838+
(1 row)
839+
840+
select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
841+
TRUE
842+
------
843+
t
844+
(1 row)
845+
846+
select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
847+
TRUE
848+
------
849+
t
850+
(1 row)
851+
852+
select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
853+
TRUE
854+
------
855+
t
856+
(1 row)
857+
858+
select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
859+
TRUE
860+
------
861+
t
862+
(1 row)
863+
864+
select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
865+
TRUE
866+
------
867+
t
868+
(1 row)
869+

src/test/regress/sql/strings.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,17 @@ select md5('abcdefghijklmnopqrstuvwxyz') = 'c3fcd3d76192e4007dfb496cca67e13b' AS
331331
select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
332332

333333
select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890') = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";
334+
335+
select md5(''::bytea) = 'd41d8cd98f00b204e9800998ecf8427e' AS "TRUE";
336+
337+
select md5('a'::bytea) = '0cc175b9c0f1b6a831c399e269772661' AS "TRUE";
338+
339+
select md5('abc'::bytea) = '900150983cd24fb0d6963f7d28e17f72' AS "TRUE";
340+
341+
select md5('message digest'::bytea) = 'f96b697d7cb7938d525a2f31aaf161d0' AS "TRUE";
342+
343+
select md5('abcdefghijklmnopqrstuvwxyz'::bytea) = 'c3fcd3d76192e4007dfb496cca67e13b' AS "TRUE";
344+
345+
select md5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'::bytea) = 'd174ab98d277d9f5a5611c2c9f419d9f' AS "TRUE";
346+
347+
select md5('12345678901234567890123456789012345678901234567890123456789012345678901234567890'::bytea) = '57edf4a22be3c955ac49da2e2107b67a' AS "TRUE";

0 commit comments

Comments
 (0)