1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

I would like to do a interface change in pgcrypto. (Good

timing, I know :))  At the moment the digest() function returns
hexadecimal coded hash, but I want it to return pure binary.  I
have also included functions encode() and decode() which support
'base64' and 'hex' encodings, so if anyone needs digest() in hex
he can do encode(digest(...), 'hex').

Main reason for it is "to do one thing and do it well" :)

Another reason is if someone needs really lot of digesting, in
the end he wants to store the binary not the hexadecimal result.
It is really silly to convert it to hex then back to binary
again.  As I said if someone needs hex he can get it.

Well, and the real reason that I am doing encrypt()/decrypt()
functions and _they_ return binary.  For testing I like to see
it in hex occasionally, but it is really wrong to let them
return hex.  Only now it caught my eye that hex-coding in
digest() is wrong.  When doing digest() I thought about 'common
case' but hacking with psql is probably _not_ the common case :)

Marko Kreen
This commit is contained in:
Bruce Momjian
2001-01-24 03:46:16 +00:00
parent bd0a767eab
commit cb5427ee47
6 changed files with 433 additions and 35 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: pgcrypto.c,v 1.3 2001/01/09 16:07:13 momjian Exp $
* $Id: pgcrypto.c,v 1.4 2001/01/24 03:46:16 momjian Exp $
*/
#include <postgres.h>
@ -34,11 +34,6 @@
#include "pgcrypto.h"
/*
* maximum length of digest for internal buffers
*/
#define MAX_DIGEST_LENGTH 128
/*
* NAMEDATALEN is used for hash names
*/
@ -52,8 +47,6 @@ Datum digest(PG_FUNCTION_ARGS);
Datum digest_exists(PG_FUNCTION_ARGS);
/* private stuff */
static char *
to_hex(uint8 *src, uint len, char *dst);
static pg_digest *
find_digest(pg_digest *hbuf, text *name, int silent);
@ -66,7 +59,6 @@ digest(PG_FUNCTION_ARGS)
{
text *arg;
text *name;
uint8 *p, buf[MAX_DIGEST_LENGTH];
uint len, hlen;
pg_digest *h, _hbuf;
text *res;
@ -78,17 +70,14 @@ digest(PG_FUNCTION_ARGS)
h = find_digest(&_hbuf, name, 0); /* will give error if fails */
hlen = h->length(h);
if (hlen > MAX_DIGEST_LENGTH)
elog(ERROR, "Hash length overflow: %d", hlen);
res = (text *)palloc(hlen*2 + VARHDRSZ);
VARATT_SIZEP(res) = hlen*2 + VARHDRSZ;
res = (text *)palloc(hlen + VARHDRSZ);
VARATT_SIZEP(res) = hlen + VARHDRSZ;
arg = PG_GETARG_TEXT_P(0);
len = VARSIZE(arg) - VARHDRSZ;
p = h->digest(h, VARDATA(arg), len, buf);
to_hex(p, hlen, VARDATA(res));
h->digest(h, VARDATA(arg), len, VARDATA(res));
PG_FREE_IF_COPY(arg, 0);
PG_FREE_IF_COPY(name, 0);
@ -143,17 +132,3 @@ find_digest(pg_digest *hbuf, text *name, int silent)
return p;
}
static unsigned char *hextbl = "0123456789abcdef";
/* dumps binary to hex... Note that it does not null-terminate */
static char *
to_hex(uint8 *buf, uint len, char *dst)
{
uint i;
for (i = 0; i < len; i++) {
dst[i*2] = hextbl[(buf[i] >> 4) & 0xF];
dst[i*2 + 1] = hextbl[buf[i] & 0xF];
}
return dst;
}