https://github.com/tacone/pg-small-hashes
Sample postgres functions to reduce the size of a 16 byte hash, without loosing more entropy than necessary.
https://github.com/tacone/pg-small-hashes
hash postgres postgresql
Last synced: 11 days ago
JSON representation
Sample postgres functions to reduce the size of a 16 byte hash, without loosing more entropy than necessary.
- Host: GitHub
- URL: https://github.com/tacone/pg-small-hashes
- Owner: tacone
- License: mit
- Created: 2022-02-21T22:50:27.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-02-22T01:55:25.000Z (over 4 years ago)
- Last Synced: 2025-02-22T06:14:51.228Z (over 1 year ago)
- Topics: hash, postgres, postgresql
- Language: PLpgSQL
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Postgres Small Hashes
Sample postgres functions to reduce the size of a 16 bytes hash, without losing more entropy than necessary.
Note that this functions are not meant to be used for security purposes, they are meant to be used to reduce the size of a hash to diminuish the stored size or the displayed size.
```sql
-- sample uses to lossy compress MD5s and UUIDs
-- --------------------------------------------
--
-- beware: this compression will not preserve the ordering (that doesn't matter
-- with MD5s and *random* UUIDs of course)
-- 8 bytes hashes, 2^64 possible combinations (~1.8 billions of billions)
select encode(small_hash (md5('hello world!')::bytea), 'base64');
-- result: XgAFB1ZdC1U=
select encode(small_hash(uuid_generate_v4()::text::bytea), 'base64');
-- result: HAkGUAgdBAU=
-- 4 bytes hashes, 2^32 possible combinations (warning! only ~4.2 billions)
select encode(tiny_hash(md5('hello world!')::bytea), 'base64');
-- result: CF0OUg==
select encode(tiny_hash(uuid_generate_v4()::text::bytea), 'base64');
-- result: FhsPUg==
```