gfc64 0.1.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +60 -0
- data/README.md +485 -46
- data/lib/gfc64/active_record.rb +8 -1
- data/lib/gfc64/config.rb +54 -0
- data/lib/gfc64/ff1.rb +204 -0
- data/lib/gfc64/format.rb +257 -0
- data/lib/gfc64/keys.rb +44 -0
- data/lib/gfc64/model.rb +403 -0
- data/lib/gfc64.rb +81 -25
- data/sql/gfc64_postgres.sql +203 -0
- metadata +16 -7
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
-- gfc64 for Postgres: FF1 (NIST SP 800-38G) in PL/pgSQL on pgcrypto, Crockford
|
|
2
|
+
-- base32, and an INSERT trigger that fills a code column from the row's
|
|
3
|
+
-- primary key. Produces the same codes as the gem (GFC64::FF1 +
|
|
4
|
+
-- GFC64::Format::Crockford32, and has_code's default tweak "table.column").
|
|
5
|
+
--
|
|
6
|
+
-- Needs only CREATE EXTENSION pgcrypto, so it runs on Amazon RDS / Aurora.
|
|
7
|
+
-- Experimental: see "Postgres (database-side codes)" in the README.
|
|
8
|
+
|
|
9
|
+
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
|
10
|
+
|
|
11
|
+
-- ---- byte helpers ----------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
-- Big-endian bytes of a non-negative integer, padded to len bytes.
|
|
14
|
+
CREATE OR REPLACE FUNCTION gfc64_num_to_bytes(n numeric, len int) RETURNS bytea
|
|
15
|
+
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
16
|
+
DECLARE
|
|
17
|
+
out bytea;
|
|
18
|
+
BEGIN
|
|
19
|
+
IF len = 0 THEN RETURN ''::bytea; END IF;
|
|
20
|
+
out := decode(repeat('00', len), 'hex');
|
|
21
|
+
FOR i IN REVERSE len - 1..0 LOOP
|
|
22
|
+
out := set_byte(out, i, mod(n, 256)::int);
|
|
23
|
+
n := div(n, 256);
|
|
24
|
+
END LOOP;
|
|
25
|
+
RETURN out;
|
|
26
|
+
END $$;
|
|
27
|
+
|
|
28
|
+
CREATE OR REPLACE FUNCTION gfc64_bytes_to_num(b bytea) RETURNS numeric
|
|
29
|
+
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
30
|
+
DECLARE
|
|
31
|
+
n numeric := 0;
|
|
32
|
+
BEGIN
|
|
33
|
+
FOR i IN 0..length(b) - 1 LOOP
|
|
34
|
+
n := n * 256 + get_byte(b, i);
|
|
35
|
+
END LOOP;
|
|
36
|
+
RETURN n;
|
|
37
|
+
END $$;
|
|
38
|
+
|
|
39
|
+
CREATE OR REPLACE FUNCTION gfc64_xor16(a bytea, b bytea) RETURNS bytea
|
|
40
|
+
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
41
|
+
DECLARE
|
|
42
|
+
out bytea := a;
|
|
43
|
+
BEGIN
|
|
44
|
+
FOR i IN 0..15 LOOP
|
|
45
|
+
out := set_byte(out, i, get_byte(a, i) # get_byte(b, i));
|
|
46
|
+
END LOOP;
|
|
47
|
+
RETURN out;
|
|
48
|
+
END $$;
|
|
49
|
+
|
|
50
|
+
-- One AES block (the key's length picks AES-128/192/256).
|
|
51
|
+
CREATE OR REPLACE FUNCTION gfc64_aes(key bytea, block bytea) RETURNS bytea
|
|
52
|
+
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
53
|
+
SELECT encrypt(block, key, 'aes-ecb/pad:none')
|
|
54
|
+
$$;
|
|
55
|
+
|
|
56
|
+
-- ---- FF1 -------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
-- Encrypts (or decrypts) x, an integer of n digits in base radix, into another
|
|
59
|
+
-- of the same size. Digits are kept as one number per half, so NUM/STR from the
|
|
60
|
+
-- standard are just the numbers themselves.
|
|
61
|
+
CREATE OR REPLACE FUNCTION gfc64_ff1(key bytea, tweak bytea, radix int, n int, x numeric, decrypting boolean DEFAULT false)
|
|
62
|
+
RETURNS numeric
|
|
63
|
+
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
64
|
+
DECLARE
|
|
65
|
+
u int := n / 2;
|
|
66
|
+
v int := n - u;
|
|
67
|
+
t int := length(tweak);
|
|
68
|
+
rv numeric := power(radix::numeric, v);
|
|
69
|
+
a numeric := div(x, rv);
|
|
70
|
+
b numeric := mod(x, rv);
|
|
71
|
+
bb int := 0; -- b in the standard: bytes for radix^v - 1
|
|
72
|
+
d int;
|
|
73
|
+
p bytea;
|
|
74
|
+
pad bytea;
|
|
75
|
+
q bytea;
|
|
76
|
+
r bytea;
|
|
77
|
+
s bytea;
|
|
78
|
+
y numeric;
|
|
79
|
+
m int;
|
|
80
|
+
rm numeric;
|
|
81
|
+
c numeric;
|
|
82
|
+
tmp numeric;
|
|
83
|
+
i int;
|
|
84
|
+
j int;
|
|
85
|
+
BEGIN
|
|
86
|
+
IF radix < 2 OR radix > 65536 THEN RAISE EXCEPTION 'radix must be 2 to 65536'; END IF;
|
|
87
|
+
IF n < 2 OR power(radix::numeric, n) < 1000000 THEN RAISE EXCEPTION 'radix^length must be at least 1,000,000'; END IF;
|
|
88
|
+
IF x < 0 OR x >= power(radix::numeric, n) THEN RAISE EXCEPTION 'value out of range'; END IF;
|
|
89
|
+
|
|
90
|
+
tmp := rv - 1;
|
|
91
|
+
WHILE tmp > 0 LOOP bb := bb + 1; tmp := div(tmp, 256); END LOOP;
|
|
92
|
+
d := 4 * ((bb + 3) / 4) + 4;
|
|
93
|
+
p := '\x010201'::bytea || gfc64_num_to_bytes(radix, 3) || '\x0a'::bytea
|
|
94
|
+
|| gfc64_num_to_bytes(u % 256, 1) || gfc64_num_to_bytes(n, 4) || gfc64_num_to_bytes(t, 4);
|
|
95
|
+
pad := decode(repeat('00', (((-t - bb - 1) % 16) + 16) % 16), 'hex');
|
|
96
|
+
|
|
97
|
+
FOR k IN 0..9 LOOP
|
|
98
|
+
i := CASE WHEN decrypting THEN 9 - k ELSE k END;
|
|
99
|
+
q := tweak || pad || gfc64_num_to_bytes(i, 1)
|
|
100
|
+
|| gfc64_num_to_bytes(CASE WHEN decrypting THEN a ELSE b END, bb);
|
|
101
|
+
-- R = PRF(P || Q): CBC-MAC with a zero IV.
|
|
102
|
+
r := decode(repeat('00', 16), 'hex');
|
|
103
|
+
j := 0;
|
|
104
|
+
WHILE j < length(p || q) LOOP
|
|
105
|
+
r := gfc64_aes(key, gfc64_xor16(r, substring(p || q FROM j + 1 FOR 16)));
|
|
106
|
+
j := j + 16;
|
|
107
|
+
END LOOP;
|
|
108
|
+
s := r;
|
|
109
|
+
j := 1;
|
|
110
|
+
WHILE length(s) < d LOOP
|
|
111
|
+
s := s || gfc64_aes(key, gfc64_xor16(r, gfc64_num_to_bytes(j, 16)));
|
|
112
|
+
j := j + 1;
|
|
113
|
+
END LOOP;
|
|
114
|
+
y := gfc64_bytes_to_num(substring(s FROM 1 FOR d));
|
|
115
|
+
m := CASE WHEN i % 2 = 0 THEN u ELSE v END;
|
|
116
|
+
rm := power(radix::numeric, m);
|
|
117
|
+
IF decrypting THEN
|
|
118
|
+
c := mod(mod(b - y, rm) + rm, rm);
|
|
119
|
+
b := a;
|
|
120
|
+
a := c;
|
|
121
|
+
ELSE
|
|
122
|
+
c := mod(a + y, rm);
|
|
123
|
+
a := b;
|
|
124
|
+
b := c;
|
|
125
|
+
END IF;
|
|
126
|
+
END LOOP;
|
|
127
|
+
RETURN a * rv + b;
|
|
128
|
+
END $$;
|
|
129
|
+
|
|
130
|
+
-- ---- Crockford base32 -------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
CREATE OR REPLACE FUNCTION gfc64_crockford_encode(x numeric, len int, grp int DEFAULT 0) RETURNS text
|
|
133
|
+
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
134
|
+
DECLARE
|
|
135
|
+
alphabet text := '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
|
136
|
+
out text := '';
|
|
137
|
+
BEGIN
|
|
138
|
+
FOR i IN 1..len LOOP
|
|
139
|
+
out := substr(alphabet, mod(x, 32)::int + 1, 1) || out;
|
|
140
|
+
x := div(x, 32);
|
|
141
|
+
END LOOP;
|
|
142
|
+
IF x > 0 THEN RAISE EXCEPTION 'value too large for % characters', len; END IF;
|
|
143
|
+
IF grp > 0 THEN
|
|
144
|
+
out := array_to_string(ARRAY(SELECT substr(out, g, grp) FROM generate_series(1, len, grp) AS g), '-');
|
|
145
|
+
END IF;
|
|
146
|
+
RETURN out;
|
|
147
|
+
END $$;
|
|
148
|
+
|
|
149
|
+
-- Forgiving: any case, O as 0, I/L as 1, spaces and hyphens ignored.
|
|
150
|
+
-- NULL when it isn't a code of exactly len characters.
|
|
151
|
+
CREATE OR REPLACE FUNCTION gfc64_crockford_decode(s text, len int) RETURNS numeric
|
|
152
|
+
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
153
|
+
DECLARE
|
|
154
|
+
alphabet text := '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
|
155
|
+
t text := translate(upper(s), 'OIL -', '011');
|
|
156
|
+
n numeric := 0;
|
|
157
|
+
pos int;
|
|
158
|
+
BEGIN
|
|
159
|
+
IF length(t) <> len THEN RETURN NULL; END IF;
|
|
160
|
+
FOR i IN 1..len LOOP
|
|
161
|
+
pos := strpos(alphabet, substr(t, i, 1));
|
|
162
|
+
IF pos = 0 THEN RETURN NULL; END IF;
|
|
163
|
+
n := n * 32 + (pos - 1);
|
|
164
|
+
END LOOP;
|
|
165
|
+
RETURN n;
|
|
166
|
+
END $$;
|
|
167
|
+
|
|
168
|
+
-- ---- key + trigger ------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
-- Keys live in a table that only the owner (the migration role) can read.
|
|
171
|
+
CREATE TABLE IF NOT EXISTS gfc64_keys (name text PRIMARY KEY, key bytea NOT NULL CHECK (length(key) IN (16, 24, 32)));
|
|
172
|
+
REVOKE ALL ON gfc64_keys FROM PUBLIC;
|
|
173
|
+
|
|
174
|
+
-- BEFORE INSERT: column defaults (the id's nextval) are already applied, so
|
|
175
|
+
-- NEW.<pk> is the row's real id. Fills <column> when it's NULL.
|
|
176
|
+
-- Arguments: column, key name, tweak, prefix, code length, group size, pk column.
|
|
177
|
+
-- SECURITY DEFINER so it can read gfc64_keys though the app's role can't.
|
|
178
|
+
CREATE OR REPLACE FUNCTION gfc64_assign_code() RETURNS trigger
|
|
179
|
+
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public AS $$
|
|
180
|
+
DECLARE
|
|
181
|
+
col text := TG_ARGV[0];
|
|
182
|
+
key_name text := TG_ARGV[1];
|
|
183
|
+
tweak text := TG_ARGV[2];
|
|
184
|
+
prefix text := coalesce(TG_ARGV[3], '');
|
|
185
|
+
len int := coalesce(TG_ARGV[4], '8')::int;
|
|
186
|
+
grp int := coalesce(TG_ARGV[5], '4')::int;
|
|
187
|
+
pk text := coalesce(TG_ARGV[6], 'id');
|
|
188
|
+
row_json jsonb := to_jsonb(NEW);
|
|
189
|
+
k bytea;
|
|
190
|
+
BEGIN
|
|
191
|
+
IF row_json ->> col IS NOT NULL THEN RETURN NEW; END IF;
|
|
192
|
+
SELECT key INTO STRICT k FROM gfc64_keys WHERE name = key_name;
|
|
193
|
+
NEW := jsonb_populate_record(NEW, jsonb_build_object(col,
|
|
194
|
+
prefix || gfc64_crockford_encode(
|
|
195
|
+
gfc64_ff1(k, convert_to(tweak, 'UTF8'), 32, len, (row_json ->> pk)::numeric), len, grp)));
|
|
196
|
+
RETURN NEW;
|
|
197
|
+
END $$;
|
|
198
|
+
REVOKE ALL ON FUNCTION gfc64_assign_code() FROM PUBLIC;
|
|
199
|
+
|
|
200
|
+
-- Example:
|
|
201
|
+
-- INSERT INTO gfc64_keys VALUES ('default', decode('<64 hex chars>', 'hex'));
|
|
202
|
+
-- CREATE TRIGGER invoices_number BEFORE INSERT ON invoices FOR EACH ROW
|
|
203
|
+
-- EXECUTE FUNCTION gfc64_assign_code('number', 'default', 'invoices.number', 'APP-', '8', '4');
|
metadata
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gfc64
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abe Voelker
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-11-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
|
-
description: Hides sequential primary key counts without resorting to UUIDs or GUIDs
|
|
13
|
+
description: 'Hides sequential primary key counts without resorting to UUIDs or GUIDs:
|
|
14
|
+
a Feistel cipher over 16-64 bit integers, NIST SP 800-38G FF1, and short-code formatters
|
|
15
|
+
(Crockford base32, base58).'
|
|
14
16
|
email: abe@abevoelker.com
|
|
15
17
|
executables: []
|
|
16
18
|
extensions: []
|
|
17
19
|
extra_rdoc_files: []
|
|
18
20
|
files:
|
|
21
|
+
- CHANGELOG.md
|
|
19
22
|
- README.md
|
|
20
23
|
- lib/gfc64.rb
|
|
21
24
|
- lib/gfc64/active_record.rb
|
|
25
|
+
- lib/gfc64/config.rb
|
|
26
|
+
- lib/gfc64/ff1.rb
|
|
27
|
+
- lib/gfc64/format.rb
|
|
28
|
+
- lib/gfc64/keys.rb
|
|
29
|
+
- lib/gfc64/model.rb
|
|
30
|
+
- sql/gfc64_postgres.sql
|
|
22
31
|
homepage: https://github.com/abevoelker/gfc64
|
|
23
32
|
licenses:
|
|
24
33
|
- MIT
|
|
@@ -27,7 +36,7 @@ metadata:
|
|
|
27
36
|
documentation_uri: https://rubydoc.info/github/abevoelker/gfc64
|
|
28
37
|
source_code_uri: https://github.com/abevoelker/gfc64
|
|
29
38
|
bug_tracker_uri: https://github.com/abevoelker/gfc64/issues
|
|
30
|
-
post_install_message:
|
|
39
|
+
post_install_message:
|
|
31
40
|
rdoc_options: []
|
|
32
41
|
require_paths:
|
|
33
42
|
- lib
|
|
@@ -42,8 +51,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
42
51
|
- !ruby/object:Gem::Version
|
|
43
52
|
version: '0'
|
|
44
53
|
requirements: []
|
|
45
|
-
rubygems_version: 3.4.
|
|
46
|
-
signing_key:
|
|
54
|
+
rubygems_version: 3.4.19
|
|
55
|
+
signing_key:
|
|
47
56
|
specification_version: 4
|
|
48
57
|
summary: Generalized Feistel Cipher for 64-bit integers
|
|
49
58
|
test_files: []
|