Cryptographic hash functions and HMAC
Installing and Loading
INSTALL crypto FROM community;
LOAD crypto;
Example
-- Calculate the MD5 hash value of 'abcdef'
SELECT crypto_hash('md5', 'abcdef');
┌──────────────────────────────────┐
│ crypto_hash('md5', 'abcdef') │
│ varchar │
├──────────────────────────────────┤
│ e80b5017098950fc58aad83c8c14978e │
└──────────────────────────────────┘
-- Calculate a HMAC
SELECT crypto_hmac('sha2-256', 'secret key', 'secret message');
┌──────────────────────────────────────────────────────────────────┐
│ crypto_hmac('sha2-256', 'secret key', 'secret message') │
│ varchar │
├──────────────────────────────────────────────────────────────────┤
│ 2df792e08cefdc0ea9900c67c93cbe66b98231b829a5dccd3857a03baac35963 │
└──────────────────────────────────────────────────────────────────┘
About crypto
crypto provides two functions:
-
crypto_hashapplies cryptographically secure hash functions and returns the result as a hex encoded value. -
crypto_hmaccalculates the HMAC using a secret key and a specific hash function.
The supported hash functions are:
blake2b-512keccak224keccak256keccak384keccak512md4md5sha1sha2-224sha2-256sha2-384sha2-512sha3-224sha3-256sha3-384sha3-512
Added Functions
| function_name | function_type | description | comment | example |
|---|---|---|---|---|
| crypto_hash | scalar | Apply a cryptographic hash function specified as the first argument to the data supplied as the second argument. | SELECT crypto_hash('md5', 'test'); | |
| crypto_hmac | scalar | Calculate a HMAC value | SELECT crypto_hmac('sha2-256', 'secret key', 'secret message'); |