Formatting

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

View File

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