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

Commit cb5427e

Browse files
committed
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
1 parent bd0a767 commit cb5427e

File tree

6 files changed

+433
-35
lines changed

6 files changed

+433
-35
lines changed

contrib/pgcrypto/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# $Header: /cvsroot/pgsql/contrib/pgcrypto/Makefile,v 1.1 2000/10/31 13:11:28 petere Exp $
2+
# $Header: /cvsroot/pgsql/contrib/pgcrypto/Makefile,v 1.2 2001/01/24 03:46:16 momjian Exp $
33
#
44

55
subdir = contrib/pgcrypto
@@ -34,7 +34,7 @@ SRCS=krb.c
3434
endif
3535

3636
NAME := pgcrypto
37-
SRCS += pgcrypto.c
37+
SRCS += pgcrypto.c encode.c
3838
OBJS := $(SRCS:.c=.o)
3939
SO_MAJOR_VERSION = 0
4040
SO_MINOR_VERSION = 1

contrib/pgcrypto/README.pgcrypto

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11

22
DESCRIPTION
33

4-
Here is a implementation of crypto hashes for PostgreSQL.
5-
It exports 2 functions to SQL level:
4+
Here are various cryptographic and otherwise useful
5+
functions for PostgreSQL.
6+
7+
encode(data, type)
8+
encodes binary data into ASCII-only representation.
9+
Types supported are 'hex' and 'base64'.
10+
11+
decode(data, type)
12+
decodes the data processed by encode()
613

714
digest(data::text, hash_name::text)
8-
which returns hexadecimal coded hash over data by
15+
which returns cryptographic checksum over data by
916
specified algorithm. eg
1017

11-
> select digest('blah', 'sha1');
18+
> select encode(digest('blah', 'sha1'), 'hex');
1219
5bf1fd927dfb8679496a2e6cf00cbe50c1c87145
1320

1421
digest_exists(hash_name::text)::bool

contrib/pgcrypto/encode.c

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
/*
2+
* encode.c
3+
* Various data encoding/decoding things.
4+
*
5+
* Copyright (c) 2001 Marko Kreen
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*
29+
* $Id: encode.c,v 1.1 2001/01/24 03:46:16 momjian Exp $
30+
*/
31+
32+
#include <postgres.h>
33+
#include <fmgr.h>
34+
35+
#include "encode.h"
36+
37+
/*
38+
* NAMEDATALEN is used for hash names
39+
*/
40+
#if NAMEDATALEN < 16
41+
#error "NAMEDATALEN < 16: too small"
42+
#endif
43+
44+
static pg_coding *
45+
find_coding(pg_coding *hbuf, text *name, int silent);
46+
static pg_coding *
47+
pg_find_coding(pg_coding *res, char *name);
48+
49+
50+
/* SQL function: encode(bytea, text) returns text */
51+
PG_FUNCTION_INFO_V1(encode);
52+
53+
Datum
54+
encode(PG_FUNCTION_ARGS)
55+
{
56+
text *arg;
57+
text *name;
58+
uint len, rlen, rlen0;
59+
pg_coding *c, cbuf;
60+
text *res;
61+
62+
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
63+
PG_RETURN_NULL();
64+
65+
name = PG_GETARG_TEXT_P(1);
66+
c = find_coding(&cbuf, name, 0); /* will give error if fails */
67+
68+
arg = PG_GETARG_TEXT_P(0);
69+
len = VARSIZE(arg) - VARHDRSZ;
70+
71+
rlen0 = c->encode_len(len);
72+
73+
res = (text *)palloc(rlen0 + VARHDRSZ);
74+
75+
rlen = c->encode(VARDATA(arg), len, VARDATA(res));
76+
VARATT_SIZEP(res) = rlen + VARHDRSZ;
77+
78+
if (rlen > rlen0)
79+
elog(FATAL, "pg_encode: overflow, encode estimate too small");
80+
81+
PG_FREE_IF_COPY(arg, 0);
82+
PG_FREE_IF_COPY(name, 0);
83+
84+
PG_RETURN_TEXT_P(res);
85+
}
86+
87+
/* SQL function: decode(text, text) returns bytea */
88+
PG_FUNCTION_INFO_V1(decode);
89+
90+
Datum
91+
decode(PG_FUNCTION_ARGS)
92+
{
93+
text *arg;
94+
text *name;
95+
uint len, rlen, rlen0;
96+
pg_coding *c, cbuf;
97+
text *res;
98+
99+
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
100+
PG_RETURN_NULL();
101+
102+
name = PG_GETARG_TEXT_P(1);
103+
c = find_coding(&cbuf, name, 0); /* will give error if fails */
104+
105+
arg = PG_GETARG_TEXT_P(0);
106+
len = VARSIZE(arg) - VARHDRSZ;
107+
108+
rlen0 = c->decode_len(len);
109+
110+
res = (text *)palloc(rlen0 + VARHDRSZ);
111+
112+
rlen = c->decode(VARDATA(arg), len, VARDATA(res));
113+
VARATT_SIZEP(res) = rlen + VARHDRSZ;
114+
115+
if (rlen > rlen0)
116+
elog(FATAL, "pg_decode: overflow, decode estimate too small");
117+
118+
PG_FREE_IF_COPY(arg, 0);
119+
PG_FREE_IF_COPY(name, 0);
120+
121+
PG_RETURN_TEXT_P(res);
122+
}
123+
124+
static pg_coding *
125+
find_coding(pg_coding *dst, text *name, int silent)
126+
{
127+
pg_coding *p;
128+
char buf[NAMEDATALEN];
129+
uint len;
130+
131+
len = VARSIZE(name) - VARHDRSZ;
132+
if (len >= NAMEDATALEN) {
133+
if (silent)
134+
return NULL;
135+
elog(ERROR, "Encoding type does not exist (name too long)");
136+
}
137+
138+
memcpy(buf, VARDATA(name), len);
139+
buf[len] = 0;
140+
141+
p = pg_find_coding(dst, buf);
142+
143+
if (p == NULL && !silent)
144+
elog(ERROR, "Encoding type does not exist: '%s'", buf);
145+
return p;
146+
}
147+
148+
static char *hextbl = "0123456789abcdef";
149+
150+
uint
151+
hex_encode(uint8 *src, uint len, uint8 *dst)
152+
{
153+
uint8 *end = src + len;
154+
while (src < end) {
155+
*dst++ = hextbl[(*src >> 4) & 0xF];
156+
*dst++ = hextbl[*src & 0xF];
157+
src++;
158+
}
159+
return len*2;
160+
}
161+
162+
/* probably should use lookup table */
163+
static uint8
164+
get_hex(char c)
165+
{
166+
uint8 res = 0;
167+
168+
if (c >= '0' && c <= '9')
169+
res = c - '0';
170+
else if (c >= 'a' && c <= 'f')
171+
res = c - 'a' + 10;
172+
else if (c >= 'A' && c <= 'F')
173+
res = c - 'A' + 10;
174+
else
175+
elog(ERROR, "Bad hex code: '%c'", c);
176+
177+
return res;
178+
}
179+
180+
uint
181+
hex_decode(uint8 *src, uint len, uint8 *dst)
182+
{
183+
uint8 *s, *srcend, v1, v2, *p = dst;
184+
185+
srcend = src + len;
186+
s = src; p = dst;
187+
while (s < srcend) {
188+
if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r') {
189+
s++;
190+
continue;
191+
}
192+
v1 = get_hex(*s++) << 4;
193+
if (s >= srcend)
194+
elog(ERROR, "hex_decode: invalid data");
195+
v2 = get_hex(*s++);
196+
*p++ = v1 | v2;
197+
}
198+
199+
return p - dst;
200+
}
201+
202+
203+
static unsigned char _base64[] =
204+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
205+
206+
uint
207+
b64_encode(uint8 *src, uint len, uint8 *dst)
208+
{
209+
uint8 *s, *p, *end = src + len, *lend = dst + 76;
210+
int pos = 2;
211+
unsigned long buf = 0;
212+
213+
s = src; p = dst;
214+
215+
while (s < end) {
216+
buf |= *s << (pos << 3);
217+
pos--;
218+
s++;
219+
220+
/* write it out */
221+
if (pos < 0) {
222+
*p++ = _base64[(buf >> 18) & 0x3f];
223+
*p++ = _base64[(buf >> 12) & 0x3f];
224+
*p++ = _base64[(buf >> 6) & 0x3f];
225+
*p++ = _base64[buf & 0x3f];
226+
227+
pos = 2;
228+
buf = 0;
229+
}
230+
if (p >= lend) {
231+
*p++ = '\n';
232+
lend = p + 76;
233+
}
234+
}
235+
if (pos != 2) {
236+
*p++ = _base64[(buf >> 18) & 0x3f];
237+
*p++ = _base64[(buf >> 12) & 0x3f];
238+
*p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '=';
239+
*p++ = '=';
240+
}
241+
242+
return p - dst;
243+
}
244+
245+
/* probably should use lookup table */
246+
uint
247+
b64_decode(uint8 *src, uint len, uint8 *dst)
248+
{
249+
char *srcend = src + len, *s = src;
250+
uint8 *p = dst;
251+
char c;
252+
uint b = 0;
253+
unsigned long buf = 0;
254+
int pos = 0, end = 0;
255+
256+
while (s < srcend) {
257+
c = *s++;
258+
if (c >= 'A' && c <= 'Z')
259+
b = c - 'A';
260+
else if (c >= 'a' && c <= 'z')
261+
b = c - 'a' + 26;
262+
else if (c >= '0' && c <= '9')
263+
b = c - '0' + 52;
264+
else if (c == '+')
265+
b = 62;
266+
else if (c == '/')
267+
b = 63;
268+
else if (c == '=') {
269+
/* end sequence */
270+
if (!end) {
271+
if (pos == 2) end = 1;
272+
else if (pos == 3) end = 2;
273+
else
274+
elog(ERROR, "base64: unexpected '='");
275+
}
276+
b = 0;
277+
} else if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
278+
continue;
279+
else
280+
elog(ERROR, "base64: Invalid symbol");
281+
282+
/* add it to buffer */
283+
buf = (buf << 6) + b;
284+
pos++;
285+
if (pos == 4) {
286+
*p++ = (buf >> 16) & 255;
287+
if (end == 0 || end > 1)
288+
*p++ = (buf >> 8) & 255;
289+
if (end == 0 || end > 2)
290+
*p++ = buf & 255;
291+
buf = 0;
292+
pos = 0;
293+
}
294+
}
295+
296+
if (pos != 0)
297+
elog(ERROR, "base64: invalid end sequence");
298+
299+
return p - dst;
300+
}
301+
302+
303+
uint
304+
hex_enc_len(uint srclen)
305+
{
306+
return srclen << 1;
307+
}
308+
309+
uint
310+
hex_dec_len(uint srclen)
311+
{
312+
return srclen >> 1;
313+
}
314+
315+
uint
316+
b64_enc_len(uint srclen)
317+
{
318+
return srclen + (srclen / 3) + (srclen / (76 / 2));
319+
}
320+
321+
uint
322+
b64_dec_len(uint srclen)
323+
{
324+
return (srclen * 3) >> 2;
325+
}
326+
327+
static pg_coding
328+
encoding_list [] = {
329+
{ "hex", hex_enc_len, hex_dec_len, hex_encode, hex_decode},
330+
{ "base64", b64_enc_len, b64_dec_len, b64_encode, b64_decode},
331+
{ NULL, NULL, NULL, NULL, NULL}
332+
};
333+
334+
335+
static pg_coding *
336+
pg_find_coding(pg_coding *res, char *name)
337+
{
338+
pg_coding *p;
339+
for (p = encoding_list; p->name; p++) {
340+
if (!strcasecmp(p->name, name))
341+
return p;
342+
}
343+
return NULL;
344+
}
345+

0 commit comments

Comments
 (0)