<?php
// Copyright (c) 2010 by Paul M. Foster <hide@address.com>
// Licensed under PostgreSQL License (see LICENSE file)
class blowfish
{
private $td, $iv, $live;
private $key;
function __construct($key)
{
$this->iv = 'unah3pat';
$this->live = false;
$this->td = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
if ($this->td !== false) {
$this->key = md5($key);
$this->live = true;
}
}
function encrypt($cleartext)
{
if ($this->live) {
mcrypt_generic_init($this->td, $this->key, $this->iv);
return mcrypt_generic($this->td, $cleartext);
}
else
return NULL;
}
function decrypt($encrypted_data)
{
if ($this->live) {
mcrypt_generic_init($this->td, $this->key, $this->iv);
return rtrim(mdecrypt_generic($this->td, $encrypted_data), "\0");
}
else
return NULL;
}
function destruct()
{
if ($this->live) {
unset($this->key);
mcrypt_generic_deinit($this->td);
mcrypt_module_close($this->td);
}
}
};