forked from ssl-oyamata/pg_vault_encrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sql
More file actions
145 lines (118 loc) · 3.94 KB
/
install.sql
File metadata and controls
145 lines (118 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
CREATE or REPLACE FUNCTION encrypt(cstring)
RETURNS cstring AS :MOD,'encrypt'
LANGUAGE C STRICT;
CREATE or REPLACE FUNCTION decrypt(cstring)
RETURNS cstring AS :MOD,'decrypt'
LANGUAGE C STRICT;
-- VARIABLE TYPE
CREATE or REPLACE FUNCTION cipherin(cstring)
RETURNS cipher AS :MOD,'encryptin'
LANGUAGE C STABLE;
CREATE or REPLACE FUNCTION cipherout(cipher)
RETURNS cstring AS :MOD,'decryptout'
LANGUAGE C STABLE;
CREATE TYPE cipher(
INPUT = cipherin,
OUTPUT = cipherout,
STORAGE = extended,
internallength = VARIABLE
);
-- FIXED TYPE
-- create or replace function encryptCstring(cstring)
-- returns cipher1024 as :MOD,'encrypt'
-- LANGUAGE C STABLE;
--
-- create or replace function decryptCstring(cipher1024)
-- returns cstring as :MOD,'decrypt'
-- LANGUAGE C STABLE;
--
-- CREATE TYPE cipher1024(
-- INPUT = encryptcstring,
-- OUTPUT = decryptcstring,
-- STORAGE = plain,
-- internallength = 1024
-- );
-- Definition of conversion function
CREATE FUNCTION cipher_to_text(cipher) RETURNS text AS
'SELECT textin(cipherout($1))' LANGUAGE sql IMMUTABLE STRICT;
CREATE FUNCTION text_to_cipher(text) RETURNS cipher AS
'SELECT cipherin(textout($1))' LANGUAGE sql IMMUTABLE STRICT;
CREATE FUNCTION bpchar_to_cipher(bpchar) RETURNS cipher AS
'SELECT cipherin(bpcharout($1::text))' LANGUAGE sql IMMUTABLE STRICT;
CREATE FUNCTION varchar_to_cipher(varchar) RETURNS cipher AS
'SELECT cipherin(varcharout($1))' LANGUAGE sql IMMUTABLE STRICT;
-- Cast definition
CREATE CAST (cipher AS text)
WITH FUNCTION cipher_to_text(cipher) AS IMPLICIT;
CREATE CAST (text AS cipher)
WITH FUNCTION text_to_cipher(text) AS IMPLICIT;
CREATE CAST (bpchar AS cipher)
WITH FUNCTION bpchar_to_cipher(bpchar) AS ASSIGNMENT;
CREATE CAST (varchar AS cipher)
WITH FUNCTION varchar_to_cipher(varchar) AS ASSIGNMENT;
--- Comparison operators
CREATE or REPLACE FUNCTION cipher_eq(cipher, cipher) RETURNS bool AS
'SELECT textin(cipherout($1)) = textin(cipherout($2))' LANGUAGE sql IMMUTABLE STRICT;
CREATE OPERATOR = (
leftarg = cipher,
rightarg = cipher,
procedure = cipher_eq,
commutator = =,
RESTRICT = eqsel
);
CREATE or REPLACE FUNCTION cipher_ne(cipher, cipher) RETURNS bool AS
'SELECT textin(cipherout($1)) != textin(cipherout($2))' LANGUAGE sql IMMUTABLE STRICT;
CREATE OPERATOR <> (
leftarg = cipher,
rightarg = cipher,
procedure = cipher_ne,
RESTRICT = neqsel
);
CREATE or REPLACE FUNCTION cipher_lt(cipher, cipher) RETURNS bool AS
'SELECT textin(cipherout($1)) < textin(cipherout($2))' LANGUAGE sql IMMUTABLE STRICT;
CREATE OPERATOR < (
leftarg = cipher,
rightarg = cipher,
procedure = cipher_lt,
RESTRICT = scalarltsel
);
CREATE or REPLACE FUNCTION cipher_gt(cipher, cipher) RETURNS bool AS
'SELECT textin(cipherout($1)) > textin(cipherout($2))' LANGUAGE sql IMMUTABLE STRICT;
CREATE OPERATOR > (
leftarg = cipher,
rightarg = cipher,
procedure = cipher_gt,
RESTRICT = scalargtsel
);
CREATE or REPLACE FUNCTION cipher_le(cipher, cipher) RETURNS bool AS
'SELECT textin(cipherout($1)) <= textin(cipherout($2))' LANGUAGE sql IMMUTABLE STRICT;
CREATE OPERATOR <= (
leftarg = cipher,
rightarg = cipher,
procedure = cipher_le,
RESTRICT = scalargtsel
);
CREATE or REPLACE FUNCTION cipher_ge(cipher, cipher) RETURNS bool AS
'SELECT textin(cipherout($1)) >= textin(cipherout($2))' LANGUAGE sql IMMUTABLE STRICT;
CREATE OPERATOR >= (
leftarg = cipher,
rightarg = cipher,
procedure = cipher_ge,
RESTRICT = scalargtsel
);
CREATE or REPLACE FUNCTION cipher_cmp(cipher, cipher)
returns integer language sql immutable as $$
select case
when textin(cipherout($1)) = textin(cipherout($2)) then 0
when textin(cipherout($1)) < textin(cipherout($2)) then -1
else 1
end
$$;
CREATE OPERATOR CLASS cipher_ops
DEFAULT FOR TYPE cipher USING btree AS
OPERATOR 1 <,
OPERATOR 2 <=,
OPERATOR 3 =,
OPERATOR 4 >=,
OPERATOR 5 >,
FUNCTION 1 cipher_cmp(cipher, cipher);