Search Postgresql Archives

bytea field, a c function and pgcrypto driving me mad

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi chaps,

I think I'm going to struggle to describe this, but hopefully someone can squint and see where I'm going wrong.

I've got a c function called "ftest", all it does is take some text and prepend "abcdefghijklmnopqr" onto it. I use it to pass a key into pgp_sym_encrypt/decrypt working on a bytea field in a table. The problem is that once the string I pass to "ftest" is longer than 10 characters it stops working when I use it with the bytea column and pgp_sym_decrypt, but it appears to work fine on it's own.

1) The source is here:

http://privatepaste.com/890Bj3FGW0

2) I created a little makefile, as follows:

MODULES = testf
PGXS := $(shell pg_config --pgxs)
include $(PGXS)

3) Then I did make, make install and created the function in the database:

CREATE OR REPLACE FUNCTION
  testf( TEXT )
RETURNS
  TEXT
AS
  'testf.so', 'testf'
LANGUAGE
  C
STRICT
IMMUTABLE;

REVOKE ALL ON FUNCTION testf( TEXT ) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION testf( TEXT ) TO admins;

4) I created a table mytest as follows:

CREATE TABLE mytest(
  username TEXT PRIMARY KEY,
  password BYTEA NOT NULL
);

5) Now with a 10 character string passed to ftest this works:

TEST=# insert into mytest (username,password) values ('short_user', pgp_sym_encrypt('testword', testf('short_user')));
INSERT 0 1
TEST=# select pgp_sym_decrypt(password, testf('short_user')) from mytest where username = 'short_user';
 pgp_sym_decrypt
-----------------
 testword
(1 row)

6) However if the I make the string longer, the decryption fails:

TEST=# insert into mytest (username,password) values ('longer_user', pgp_sym_encrypt('testword', testf('longer_user')));
INSERT 0 1
TEST=# select pgp_sym_decrypt(password, testf('longer_user')) from mytest where username = 'longer_user';
ERROR:  Wrong key or corrupt data

But the C function appears to be working on it's own:

TEST=# select testf('longer_user');
             testf
-------------------------------
 abcdefghijklmnopqrlonger_user
(1 row)

7) But, if I insert the data into the table without using my function it works:

TEST=# insert into mytest (username,password) values ('longer_user', pgp_sym_encrypt('testword', 'abcdefghijklmnopqrlonger_user'));
INSERT 0 1
TEST=# select pgp_sym_decrypt(password, testf('longer_user')) from mytest where username = 'longer_user';
 pgp_sym_decrypt
-----------------
 testword
(1 row)


So it appears that my function is only working in conjunction with pgp_sym_encrypt on an insert when the text value I pass into it is less than 10 characters long.

It's driving me nuts, can anyone see what I'm doing wrong?

Thanks
Glyn




-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux