From 32eeb8c49a302cfd38ddb37f9c0d7784d529e826 Mon Sep 17 00:00:00 2001 From: Miles Lott Date: Sun, 6 May 2001 13:19:42 +0000 Subject: [PATCH] Formatting --- phpgwapi/inc/class.crypto.inc.php | 225 +++++++++++++++++------------- 1 file changed, 127 insertions(+), 98 deletions(-) diff --git a/phpgwapi/inc/class.crypto.inc.php b/phpgwapi/inc/class.crypto.inc.php index 2cb3000ecd..f6fdb84f82 100644 --- a/phpgwapi/inc/class.crypto.inc.php +++ b/phpgwapi/inc/class.crypto.inc.php @@ -23,113 +23,142 @@ /* $Id$ */ - class crypto { - var $td = False; // Handle for mcrypt - var $iv = ""; - var $key = ""; - function crypto($vars) - { - 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, ''); + class crypto + { + var $td = False; // Handle for mcrypt + var $iv = ''; + var $key = ''; - $ivsize = mcrypt_enc_get_iv_size($this->td); - $keysize = mcrypt_enc_get_key_size($this->td); - } + function crypto($vars) + { + 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 - $x = strlen($iv); - for ($i = 0; $i < $ivsize; $i++) { - $this->iv .= $iv[$i % $x]; - } - - // Hack Key to be the correct size - $x = strlen($key); + $ivsize = mcrypt_enc_get_iv_size($this->td); + $keysize = mcrypt_enc_get_key_size($this->td); + } - for ($i = 0; $i < $keysize; $i++) { - $this->key .= $key[$i % $x]; - } - if ($phpgw_info['server']['versions']['mcrypt'] != 'old') { - mcrypt_generic_init ($this->td, $this->key, $this->iv); - } - } - // If mcrypt isn't loaded key and iv are not needed - } + // Hack IV to be the correct size + $x = strlen($iv); + for ($i = 0; $i < $ivsize; $i++) + { + $this->iv .= $iv[$i % $x]; + } - function cleanup() - { - global $phpgw_info; + // Hack Key to be the correct size + $x = strlen($key); - if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt')) { - if ($phpgw_info['server']['versions']['mcrypt'] != 'old') { - mcrypt_generic_end ($this->td); - } - } - } - function hex2bin($data) - { - $len = strlen($data); - return pack('H' . $len, $data); - } + for ($i = 0; $i < $keysize; $i++) + { + $this->key .= $key[$i % $x]; + } + if ($phpgw_info['server']['versions']['mcrypt'] != 'old') + { + mcrypt_generic_init ($this->td, $this->key, $this->iv); + } + } + // If mcrypt isn't loaded key and iv are not needed + } - function encrypt($data) { - global $phpgw_info; + function cleanup() + { + 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 - if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt')) { - switch ($phpgw_info['server']['versions']['mcrypt']) { - // The old code, only works with mcrypt <= 2.2.x - 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 hex2bin($data) + { + $len = strlen($data); + return pack('H' . $len, $data); + } - function decrypt($encrypteddata) { - global $phpgw_info; + function encrypt($data) + { + 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); + $data = serialize($data); - 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); - } - } + // Disable all encryption if the admin didn't set it up + if ($phpgw_info['server']['mcrypt_enabled'] && extension_loaded('mcrypt')) + { + switch ($phpgw_info['server']['versions']['mcrypt']) + { + // The old code, only works with mcrypt <= 2.2.x + 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; + } + } - } // 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 +?>