 
 /* Example usage:
 
-udf=# create table base (c_comment varchar);   
+udf=# CREATE TABLE base (c_comment VARCHAR);
 CREATE TABLE
 
-udf=# insert into base values
+udf=# INSERT INTO base VALUES
 udf-# ('deposits eat slyly ironic, even instructions. express foxes detect slyly. blithely even accounts abov'),
 udf-# ('ainst the ironic, express theodolites. express, even pinto beans among the exp'),
-udf-# ('ckages. requests sleep slyly. quickly even pinto beans promise above the slyly regular pinto beans.'),  
-udf-# ('platelets. regular deposits detect asymptotes. blithely unusual packages nag slyly at the fluf'),  
+udf-# ('ckages. requests sleep slyly. quickly even pinto beans promise above the slyly regular pinto beans.'),
+udf-# ('platelets. regular deposits detect asymptotes. blithely unusual packages nag slyly at the fluf'),
 udf-# ('nag. furiously careful packages are slyly at the accounts. furiously regular in');
 INSERT 0 5
 
-# NOTE: Passphrase must be exactly 32 characters
+udf=# CREATE TABLE encrypted AS SELECT c_comment, f_encrypt_str(c_comment,'PassphrasePassphrasePassphrase32') AS c_comment_encrypted FROM base;
+SELECT
 
-udf=# create table encrypted as select c_comment, f_encrypt_str(c_comment,'PassphrasePassphrasePassphrase32') as c_comment_encrypted from base;
+udf=# CREATE TABLE decrypted AS SELECT c_comment, f_decrypt_str(c_comment_encrypted,'PassphrasePassphrasePassphrase32') AS c_comment_decrypted FROM encrypted;
 SELECT
-udf=# create table decrypted as select c_comment, f_decrypt_str(c_comment_encrypted,'PassphrasePassphrasePassphrase32') as c_comment_decrypted from encrypted;
-SELECT
-udf=# select * from decrypted;
-                                               c_comment                                               |                                          c_comment_decrypted                                          
+
+udf=# SELECT * FROM decrypted;
+                                               c_comment                                               |                                          c_comment_decrypted
 -------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------
+ platelets. regular deposits detect asymptotes. blithely unusual packages nag slyly at the fluf        | platelets. regular deposits detect asymptotes. blithely unusual packages nag slyly at the fluf
+ deposits eat slyly ironic, even instructions. express foxes detect slyly. blithely even accounts abov | deposits eat slyly ironic, even instructions. express foxes detect slyly. blithely even accounts abov
  nag. furiously careful packages are slyly at the accounts. furiously regular in                       | nag. furiously careful packages are slyly at the accounts. furiously regular in
  ckages. requests sleep slyly. quickly even pinto beans promise above the slyly regular pinto beans.   | ckages. requests sleep slyly. quickly even pinto beans promise above the slyly regular pinto beans.
- platelets. regular deposits detect asymptotes. blithely unusual packages nag slyly at the fluf        | platelets. regular deposits detect asymptotes. blithely unusual packages nag slyly at the fluf
- deposits eat slyly ironic, even instructions. express foxes detect slyly. blithely even accounts abov | deposits eat slyly ironic, even instructions. express foxes detect slyly. blithely even accounts abov
  ainst the ironic, express theodolites. express, even pinto beans among the exp                        | ainst the ironic, express theodolites. express, even pinto beans among the exp
 (5 rows)
 
-udf=# create table decryption_failure as select c_comment, f_decrypt_str(c_comment_encrypted,'PassphrasePassphrasePassphrase12') as c_comment_decrypted from encrypted;
+
+udf=# CREATE TABLE decryption_failure AS SELECT c_comment, f_decrypt_str(c_comment_encrypted,'PassphrasePassphrasePassphrase12') AS c_comment_decrypted FROM encrypted;
 SELECT
-udf=# select * from decryption_failure ;
-                                               c_comment                                               |                                          c_comment_decrypted                                          
+
+udf=# SELECT * FROM decryption_failure ;
+                                               c_comment                                               |                                          c_comment_decrypted
 -------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------
- deposits eat slyly ironic, even instructions. express foxes detect slyly. blithely even accounts abov | MC\x1FZ??9??U$G\x04-T^\x17N????2-\r\x06^\x1F`Ap.??\x11)??i\x0C\x0F\x1B:r\x03uKM3VD\x12KZ%^\x12??Pd?P\x07\x02
- ainst the ironic, express theodolites. express, even pinto beans among the exp                        | HO\x01F?????v\x0CaV\x0F7DRCC??5*\x0CEA     =Tt.\x10%+\r\x0B??V8\x7FE7Z
- ckages. requests sleep slyly. quickly even pinto beans promise above the slyly regular pinto beans.   | JM\x0ER??|_hKJ7[\x0B[R?5\x06
- nag. furiously careful packages are slyly at the accounts. furiously regular in                       | GG\x08\x1B??u??^aHJ4V\x11\J??\u008A.;\x0C\x1C\x11\x18n\x11c(
+ platelets. regular deposits detect asymptotes. blithely unusual packages nag slyly at the fluf        | YJ\x0EA|Ɯ^$J\x057^\x06D\x0Bڊ(~\x0C\x08^ =]k?^$҃:\x1D\x0BX7x\rv\x02I/VD\x12OX`\x16\x16љ
+ deposits eat slyly ironic, even instructions. express foxes detect slyly. blithely even accounts abov | MC\x1FZڛ9ӑU$G\x04-T^\x17NȊߊ2-\r\x06^\x1F`Ap.Э\x11)مi\x0C\x0F\x1B:r\x03uKM3VD\x12KZ%^\x12ёPd?P\x07\x02
+ nag. furiously careful packages are slyly at the accounts. furiously regular in                       | GG\x08\x1BΜuɜ^aHJ4V\x11\Jي\u008A.;\x0C\x1C\x11\x18n\x11c(
                                                                                                        : "/\x1D\x18WvlXv\x02K
- platelets. regular deposits detect asymptotes. blithely unusual packages nag slyly at the fluf        | YJ\x0EA|??^$J\x057^\x06D\x0B??(~\x0C\x08^ =]k?^$??:\x1D\x0BX7x\rv\x02I/VD\x12OX`\x16\x16??
+ ckages. requests sleep slyly. quickly even pinto beans promise above the slyly regular pinto beans.   | JM\x0ERʚ|_hKJ7[\x0B[R?5\x06
+ ainst the ironic, express theodolites. express, even pinto beans among the exp                        | HO\x01F䂉ҝv\x0CaV\x0F7DRCCۀ5*\x0CEA     =Tt.\x10%+\r\x0BܠV8\x7FE7Z
 (5 rows)
 
 */

*/

CREATE LIBRARY pyaes
language plpythonu 
from 'https://s3.amazonaws.com/udf-bucket/pyaes.zip';

create or replace function f_encrypt_str(a varchar(256),key char(32))
returns varchar(max)
STABLE
AS $$
    import pyaes

Purpose: f_encrypt_str encrypts a 256 character string with AES256 encryption using a 32 character key.
f_decrypt_str decrypts this encrypted string and returns the original plaintext.  

Internal dependencies: base64

External dependencies: pyaes (https://github.com/ricmoo/pyaes)

