Formatting

This commit is contained in:
Miles Lott 2001-05-06 13:19:42 +00:00
parent f6b3e189cf
commit 32eeb8c49a

View File

@ -23,26 +23,35 @@
/* $Id$ */ /* $Id$ */
class crypto { class crypto
{
var $td = False; // Handle for mcrypt var $td = False; // Handle for mcrypt
var $iv = ""; var $iv = '';
var $key = ""; var $key = '';
function crypto($vars) function crypto($vars)
{ {
global $phpgw, $phpgw_info; global $phpgw, $phpgw_info;
$key = $vars[0]; $key = $vars[0];
$iv = $vars[1]; $iv = $vars[1];
if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt')) { if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt'))
if ($phpgw_info['server']['versions']['mcrypt'] == 'old') { {
if ($phpgw_info['server']['versions']['mcrypt'] == 'old')
{
$this->td = false; $this->td = false;
if (phpversion() > '4.0.2pl1') { if (phpversion() > '4.0.2pl1')
{
$keysize = mcrypt_get_key_size(MCRYPT_TRIPLEDES); $keysize = mcrypt_get_key_size(MCRYPT_TRIPLEDES);
$ivsize = mcrypt_get_iv_size(MCRYPT_TRIPLEDES,MCRYPT_MODE_CBC); $ivsize = mcrypt_get_iv_size(MCRYPT_TRIPLEDES,MCRYPT_MODE_CBC);
} else { }
else
{
$keysize = 8; $keysize = 8;
$ivsize = 8; $ivsize = 8;
} }
} else { }
else
{
// Start up mcrypt // Start up mcrypt
$this->td = mcrypt_module_open (MCRYPT_TRIPLEDES, '', MCRYPT_MODE_CBC, ''); $this->td = mcrypt_module_open (MCRYPT_TRIPLEDES, '', MCRYPT_MODE_CBC, '');
@ -52,17 +61,20 @@
// Hack IV to be the correct size // Hack IV to be the correct size
$x = strlen($iv); $x = strlen($iv);
for ($i = 0; $i < $ivsize; $i++) { for ($i = 0; $i < $ivsize; $i++)
{
$this->iv .= $iv[$i % $x]; $this->iv .= $iv[$i % $x];
} }
// Hack Key to be the correct size // Hack Key to be the correct size
$x = strlen($key); $x = strlen($key);
for ($i = 0; $i < $keysize; $i++) { for ($i = 0; $i < $keysize; $i++)
{
$this->key .= $key[$i % $x]; $this->key .= $key[$i % $x];
} }
if ($phpgw_info['server']['versions']['mcrypt'] != 'old') { if ($phpgw_info['server']['versions']['mcrypt'] != 'old')
{
mcrypt_generic_init ($this->td, $this->key, $this->iv); mcrypt_generic_init ($this->td, $this->key, $this->iv);
} }
} }
@ -73,63 +85,80 @@
{ {
global $phpgw_info; global $phpgw_info;
if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt')) { if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt'))
if ($phpgw_info['server']['versions']['mcrypt'] != 'old') { {
if ($phpgw_info['server']['versions']['mcrypt'] != 'old')
{
mcrypt_generic_end ($this->td); mcrypt_generic_end ($this->td);
} }
} }
} }
function hex2bin($data) function hex2bin($data)
{ {
$len = strlen($data); $len = strlen($data);
return pack('H' . $len, $data); return pack('H' . $len, $data);
} }
function encrypt($data) { function encrypt($data)
{
global $phpgw_info; global $phpgw_info;
$data = serialize($data); $data = serialize($data);
// Disable all encryption if the admin didn't set it up // Disable all encryption if the admin didn't set it up
if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt')) { if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt'))
switch ($phpgw_info['server']['versions']['mcrypt']) { {
switch ($phpgw_info['server']['versions']['mcrypt'])
{
// The old code, only works with mcrypt <= 2.2.x // The old code, only works with mcrypt <= 2.2.x
case 'old': { case 'old':
{
$encrypteddata = mcrypt_cbc(MCRYPT_TripleDES, $this->key, $data, MCRYPT_ENCRYPT); $encrypteddata = mcrypt_cbc(MCRYPT_TripleDES, $this->key, $data, MCRYPT_ENCRYPT);
break; break;
} }
default: { // Handle 2.4 and newer API default:
{ // Handle 2.4 and newer API
$encrypteddata = mcrypt_generic($this->td, $data); $encrypteddata = mcrypt_generic($this->td, $data);
} }
} }
$encrypteddata = bin2hex($encrypteddata); $encrypteddata = bin2hex($encrypteddata);
return $encrypteddata; return $encrypteddata;
} else { // No mcrypt == insecure ! }
else
{ // No mcrypt == insecure !
return $data; return $data;
} }
} }
function decrypt($encrypteddata) { function decrypt($encrypteddata)
{
global $phpgw_info; global $phpgw_info;
// Disable all encryption if the admin didn't set it up // Disable all encryption if the admin didn't set it up
if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt')) { if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt'))
{
$data = $this->hex2bin($encrypteddata); $data = $this->hex2bin($encrypteddata);
switch ($phpgw_info['server']['versions']['mcrypt']) { switch ($phpgw_info['server']['versions']['mcrypt'])
{
// The old code, only works with mcrypt <= 2.2.x // The old code, only works with mcrypt <= 2.2.x
case 'old': { case 'old':
{
$data = mcrypt_cbc(MCRYPT_TripleDES, $this->key, $data, MCRYPT_DECRYPT); $data = mcrypt_cbc(MCRYPT_TripleDES, $this->key, $data, MCRYPT_DECRYPT);
break; break;
} }
default: { // Handle 2.4 and newer API default:
{ // Handle 2.4 and newer API
$data = mdecrypt_generic($this->td, $data); $data = mdecrypt_generic($this->td, $data);
} }
} }
return unserialize($data); return unserialize($data);
} else { }
else
{
return unserialize($encrypteddata); return unserialize($encrypteddata);
} }
} }
} // class crypto } // class crypto
?>