I've decided to try using onBeforeWrite. here is my code:
function onBeforeWrite(){
$modes = mcrypt_list_modes();
/* Open the cipher */
$td = mcrypt_module_open('rijndael-128', '', 'ecb', '');
/* Create the IV and determine the keysize length, use MCRYPT_RAND
* on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
/* Create key */
$key = substr(md5('very secret key'), 0, $ks);
/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);
$toEncrypt = $this->FirstName;
$encrypted = mcrypt_generic($td, $toEncrypt);
$this->setField("FirstName", $encrypted);
/* Terminate encryption handler */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
parent::onBeforeWrite();
}
The data encrypts but won't write to the DB. is this because SilverStripe is escaping any non standard alphanumeric characters? If so how do I override
Thanks