C Crypsi (https://github.com/telkomdev/c-crypsi) PostgreSQL Extension
Why not pgcrypto
?. At the time this plugin was created, pgcrypto
did not support AES GCM
yet. So this plugin is made to fulfill AES GCM
encryption needs.
- https://github.com/telkomdev/c-crypsi
- Openssl 1.1.1
- C/C++ https://github.com/telkomdev/c-crypsi
- Golang https://github.com/telkomdev/go-crypsi
- Python https://github.com/telkomdev/pycrypsi
- C# (.NET) https://github.com/telkomdev/NetCrypsi
- Java/JVM https://github.com/telkomdev/jcrypsi
- NodeJs https://github.com/telkomdev/crypsi
- Javascript (React and Browser) https://github.com/telkomdev/crypsi.js
- MySQL https://github.com/telkomdev/crypsi-mysql-udf
Compatible which means you can directly use existing functions to encrypt and decrypt data. For example, the functions in the Crypsi package for NodeJs below are compatible with the functions in pgcrypsi
pgcrypsi
postgres=# select pgcrypsi_aes_128_gcm_encrypt('abc$#128djdyAgbj', 'this is dark') as res;
res
----------------------------------------------------------------------------------
817352e89687f1786b6c66939011f9714087a85a87c9eca95ea172067e33d6839235848fed0a32f9
(1 row)
Decrypt the above encrypted data with the crypsi package for Nodejs
const { aesEncryption } = require('crypsi');
const decryptedData = aesEncryption.decryptWithAes128Gcm('abc$#128djdyAgbj', '817352e89687f1786b6c66939011f9714087a85a87c9eca95ea172067e33d6839235848fed0a32f9');
console.log(decryptedData.toString('utf-8')); // result: this is dark
Clone
$ git clone https://github.com/telkomdev/pgcrypsi.git
Install PostgreSQL Development server and client
$ sudo apt install libpq-dev
$ sudo apt-get install -y postgresql-server-dev-10
Compile extensions, Create and Copy SHARED Library to /usr/lib/postgresql/10/lib/
$ cc -fPIC -c pgcrypsi.c -I /usr/include/postgresql/10/server/
$ cc -shared -o pgcrypsi.so pgcrypsi.o
$ sudo cp pgcrypsi.so /usr/lib/postgresql/10/lib/
To find out what $libdir
is referring to, run the following command:
$ pg_config --pkglibdir
/usr/lib/postgresql/10/lib
Install extensions
$ sudo make USE_PGXS=1 install
Login as superuser
$ sudo --login --user postgres
$ psql
Connect to specific Database
$ \c database_name;
Show installed extensions
$ select extname from pg_extension;
Drop extensions
$ DROP EXTENSION IF EXISTS pgcrypsi;
Create extensions
$ CREATE EXTENSION IF NOT EXISTS pgcrypsi;
- pgcrypsi_aes_128_gcm_encrypt (AES 128 bit encryption function)
- pgcrypsi_aes_192_gcm_encrypt (AES 192 bit encryption function)
- pgcrypsi_aes_256_gcm_encrypt (AES 256 bit encryption function)
- pgcrypsi_aes_128_gcm_decrypt (AES 128 bit decryption function)
- pgcrypsi_aes_192_gcm_decrypt (AES 192 bit decryption function)
- pgcrypsi_aes_256_gcm_decrypt (AES 256 bit decryption function)
- AES 128: key length should be 16 bytes/char
- AES 192: key length should be 24 bytes/char
- AES 256: key length should be 32 bytes/char
postgres=# \i /home/user/pgcrypsi/test.sql
Encrypt
postgres=# select pgcrypsi_aes_256_gcm_encrypt('abc$#128djdyAgbjau&YAnmcbagryt5x', 'this is dark') as res;
res
----------------------------------------------------------------------------------
90fee206d3f41bd92e45e7c876cce4e3f4ed65aeef3cbd05139677bc18d1b393a53848944ef3df05
(1 row)
Decrypt
postgres=# select pgcrypsi_aes_256_gcm_decrypt('abc$#128djdyAgbjau&YAnmcbagryt5x', '90fee206d3f41bd92e45e7c876cce4e3f4ed65aeef3cbd05139677bc18d1b393a53848944ef3df05') as res;
res
--------------
this is dark
(1 row)