From 294fac0719fb1d14dd125d2d7af1019f10d4ce81 Mon Sep 17 00:00:00 2001 From: Sebastian Mendel Date: Fri, 25 Jan 2008 09:35:48 +0000 Subject: [PATCH] cleaned up some code --- libraries/blowfish.php | 77 ++++++++++-------------------------------- 1 file changed, 18 insertions(+), 59 deletions(-) diff --git a/libraries/blowfish.php b/libraries/blowfish.php index 1e16d0669..e02ec37cd 100644 --- a/libraries/blowfish.php +++ b/libraries/blowfish.php @@ -19,8 +19,8 @@ // Change for phpMyAdmin by lem9: //class Horde_Cipher_blowfish extends Horde_Cipher { -class Horde_Cipher_blowfish { - +class Horde_Cipher_blowfish +{ /* Pi Array */ var $p = array( 0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, @@ -294,11 +294,6 @@ class Horde_Cipher_blowfish { /* The number of rounds to do */ var $_rounds = 16; - /* Constructor */ - function Cipher_blowfish($params = null) - { - } - /** * Set the key to be used for en/decryption * @@ -358,16 +353,6 @@ class Horde_Cipher_blowfish { } - /** - * Return the size of the blocks that this cipher needs - * - * @return Integer The number of characters per block - */ - function getBlockSize() - { - return 8; - } - /** * Encrypt a block on data. * @@ -481,38 +466,6 @@ class Horde_Cipher_blowfish { } // higher-level functions: - -/** - * String padding - * - * @param string input string - * @param integer length of the result - * @param string the filling string - * @param integer padding mode - * - * @return string the padded string - * - * @access public - */ -function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) { - $str = ''; - $length = $pad_length - strlen($input); - if ($length > 0) { // str_repeat doesn't like negatives - if ($pad_type == STR_PAD_RIGHT) { // STR_PAD_RIGHT == 1 - $str = $input.str_repeat($pad_string, $length); - } elseif ($pad_type == STR_PAD_BOTH) { // STR_PAD_BOTH == 2 - $str = str_repeat($pad_string, floor($length/2)); - $str .= $input; - $str .= str_repeat($pad_string, ceil($length/2)); - } else { // defaults to STR_PAD_LEFT == 0 - $str = str_repeat($pad_string, $length).$input; - } - } else { // if $length is negative or zero we don't need to do anything - $str = $input; - } - return $str; -} - /** * Encryption using blowfish algorithm * @@ -525,15 +478,19 @@ function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) { * * @author lem9 */ -function PMA_blowfish_encrypt($data, $secret) { +function PMA_blowfish_encrypt($data, $secret) +{ $pma_cipher = new Horde_Cipher_blowfish; $encrypt = ''; - for ($i=0; $iencryptBlock($block, $secret); + + $mod = strlen($data) % 8; + + if ($mod > 0) { + $data .= str_repeat("\0", 8 - $mod); + } + + foreach (str_split($data, 8) as $chunk) { + $encrypt .= $pma_cipher->encryptBlock($chunk, $secret); } return base64_encode($encrypt); } @@ -550,12 +507,14 @@ function PMA_blowfish_encrypt($data, $secret) { * * @author lem9 */ -function PMA_blowfish_decrypt($encdata, $secret) { +function PMA_blowfish_decrypt($encdata, $secret) +{ $pma_cipher = new Horde_Cipher_blowfish; $decrypt = ''; $data = base64_decode($encdata); - for ($i=0; $idecryptBlock(substr($data, $i, 8), $secret); + + foreach (str_split($data, 8) as $chunk) { + $decrypt .= $pma_cipher->decryptBlock($chunk, $secret); } return trim($decrypt); }