Feistel cipher, shorter string and hex to int

From: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
To: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Feistel cipher, shorter string and hex to int
Date: 2009-07-06 16:45:35
Message-ID: 20090706184535.21c9e26f@dawn.webthatworks.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, 02 May 2009 11:26:28 +0200
"Daniel Verite" <daniel(at)manitou-mail(dot)org> wrote:

> Note that it returns a bigint because we don't have unsigned
> integers in PG. If you're OK with getting negative values, the
> return type can be changed to int.
> Otherwise if you need a positive result that fits in 32 bits, it's
> possible to tweak the code to use 15 bits blocks instead of 16,
> but then the input will have to be less than 2^30.

I need shorter values (because they should be easier to type.
To be sure to modify the function in a sensible way I really would
appreciate some pointer.
Still if it return

To further shrink the length of the result I was planning to to_hex
(actually it would be nice to have a fast to_35base [0-9a-z])... but
I wasn't able to find a way to convert back an hex string to an int.
x'fff' seems to work just for literals.

CREATE OR REPLACE FUNCTION pseudo_encrypt(value int) returns
bigint AS $$
DECLARE
l1 int;
l2 int;
r1 int;
r2 int;
i int:=0;
BEGIN
l1:= (value >> 16) & 65535;
-- modifying here seems trivial
r1:= value&65535;
-- l1:= (value >> 15) & B'111111111111111'::int;
-- r1:= value & B'111111111111111'::int;
WHILE i<3 LOOP
l2:=r1;
r2:=l1 # ((((1366.0*r1+150889)%714025)/714025.0)*32767)::int;
-- but what about this? where does it come from?
/*
r2:=l1 #
((((1366.0*r1+150889)%714025)/714025.0)*B'11111111111111'::int)::int;
*/ -- ??
l1:=l2; r1:=r2; i:=i+1;
END LOOP;
return ((l1::bigint<<16) + r1);
-- modifying here seems trivial
END;
$$ LANGUAGE plpgsql strict immutable;

Anything else to suggest or copy from?

--
Ivan Sergio Borgonovo
http://www.webthatworks.it

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stuart McGraw 2009-07-06 17:06:51 Windows installer for pg-8.4 confusion
Previous Message nha 2009-07-06 16:12:22 Re: Problem search on text arrays, using the overlaps (&&) operator