diff options
author | Bruce Momjian | 2001-01-24 03:46:16 +0000 |
---|---|---|
committer | Bruce Momjian | 2001-01-24 03:46:16 +0000 |
commit | cb5427ee4770bb224a3f884a83614ceee85f36ab (patch) | |
tree | 105903f1043e5e35f2216649d5a70523484561bb /contrib/pgcrypto/pgcrypto.c | |
parent | bd0a767eab5f4d4963bf12c05cb9b5a135172b14 (diff) |
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
Diffstat (limited to 'contrib/pgcrypto/pgcrypto.c')
-rw-r--r-- | contrib/pgcrypto/pgcrypto.c | 33 |
1 files changed, 4 insertions, 29 deletions
diff --git a/contrib/pgcrypto/pgcrypto.c b/contrib/pgcrypto/pgcrypto.c index f9075dee58d..397f5d9896e 100644 --- a/contrib/pgcrypto/pgcrypto.c +++ b/contrib/pgcrypto/pgcrypto.c @@ -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> @@ -35,11 +35,6 @@ #include "pgcrypto.h" /* - * maximum length of digest for internal buffers - */ -#define MAX_DIGEST_LENGTH 128 - -/* * NAMEDATALEN is used for hash names */ #if NAMEDATALEN < 16 @@ -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; -} - |