https://github.com/jackc/pg-extid
https://github.com/jackc/pg-extid
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jackc/pg-extid
- Owner: jackc
- License: mit
- Created: 2022-09-04T02:42:15.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-26T21:24:54.000Z (over 3 years ago)
- Last Synced: 2025-04-11T20:46:07.587Z (about 1 year ago)
- Language: Go
- Size: 5.86 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pg-extid
It can be valuable to internally use a serial integer as an ID without revealing that ID to the outside world. pg-extid
uses AES-128 to convert to and from an external ID that cannot feasibly be decoded without the secret key.
This prevents outsiders from quantifying the usage of your application by observing the rate of increase of IDs as well
as provides protection against brute force crawling of all resources.
## Installation
Run `structure.sql` in your database.
## Example Usage
```sql
insert into extid_types (prefix) values ('user');
-- => INSERT 0 1
select encode_extid('user', 123);
-- => user_d1001653c9461a3e97106d386d0bb4df
select decode_extid('user_d1001653c9461a3e97106d386d0bb4df');
-- => 123
```
## Performance
Encoding an ID takes ~10µs and decoding an external ID takes ~15µs on a 2019 MacBook Pro.
```sql
select encode_extid('user', n) from generate_series(1, 100000) n;
-- Time: 1070.191 ms (00:01.070)
select decode_extid(encode_extid('user', n)) from generate_series(1, 100000) n;
-- Time: 2508.360 ms (00:02.508)
```
## Other Implementations
* [Go](https://github.com/jackc/go-extid)
* [Ruby](https://github.com/jackc/ruby-extid)