Merge pull request #63 from petrich/patch-3

Rijndael: little code optimizing
This commit is contained in:
terrafrost 2013-01-25 21:52:40 -08:00
commit 111544428e

View File

@ -765,8 +765,13 @@ class Crypt_Rijndael {
case CRYPT_RIJNDAEL_MODE_CFB: case CRYPT_RIJNDAEL_MODE_CFB:
// cfb loosely routines inspired by openssl's: // cfb loosely routines inspired by openssl's:
// http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1 // http://cvs.openssl.org/fileview?f=openssl/crypto/modes/cfb128.c&v=1.3.2.2.2.1
$iv = $this->encryptIV; if ($this->continuousBuffer) {
$pos = $this->continuousBuffer === true ? $buffer['pos'] : 0; $iv = &$this->encryptIV;
$pos = &$buffer['pos'];
} else {
$iv = $this->encryptIV;
$pos = 0;
}
$len = strlen($plaintext); $len = strlen($plaintext);
$i = 0; $i = 0;
if ($pos) { if ($pos) {
@ -793,15 +798,10 @@ class Crypt_Rijndael {
} }
if ($len) { if ($len) {
$iv = $this->_encryptBlock($iv); $iv = $this->_encryptBlock($iv);
//$block = substr($iv, $pos, $len) ^ substr($plaintext, $i, $len); $block = $iv ^ substr($plaintext, $i);
$block = substr($iv, $pos) ^ substr($plaintext, $i);
$iv = substr_replace($iv, $block, 0, $len); $iv = substr_replace($iv, $block, 0, $len);
$ciphertext.= $block; $ciphertext.= $block;
$pos+= $len; $pos = $len;
}
if($this->continuousBuffer) {
$this->encryptIV = $iv;
$buffer['pos'] = $pos;
} }
break; break;
case CRYPT_RIJNDAEL_MODE_OFB: case CRYPT_RIJNDAEL_MODE_OFB:
@ -896,8 +896,13 @@ class Crypt_Rijndael {
} }
break; break;
case CRYPT_RIJNDAEL_MODE_CFB: case CRYPT_RIJNDAEL_MODE_CFB:
$iv = $this->decryptIV; if ($this->continuousBuffer) {
$pos = $this->continuousBuffer === true ? $buffer['pos'] : 0; $iv = &$this->decryptIV;
$pos = &$buffer['pos'];
} else {
$iv = $this->decryptIV;
$pos = 0;
}
$len = strlen($ciphertext); $len = strlen($ciphertext);
$i = 0; $i = 0;
if ($pos) { if ($pos) {
@ -926,13 +931,9 @@ class Crypt_Rijndael {
} }
if ($len) { if ($len) {
$iv = $this->_encryptBlock($iv); $iv = $this->_encryptBlock($iv);
$plaintext.= substr($iv, $pos) ^ substr($ciphertext, $i); $plaintext.= $iv ^ substr($ciphertext, $i);
$iv = substr_replace($iv, substr($ciphertext, $i, $len), 0, $len); $iv = substr_replace($iv, substr($ciphertext, $i), 0, $len);
$pos+= $len; $pos = $len;
}
if ($this->continuousBuffer) {
$this->decryptIV = $iv;
$buffer['pos'] = $pos;
} }
break; break;
case CRYPT_RIJNDAEL_MODE_OFB: case CRYPT_RIJNDAEL_MODE_OFB:
@ -1499,4 +1500,4 @@ class Crypt_Rijndael {
} }
// vim: ts=4:sw=4:et: // vim: ts=4:sw=4:et:
// vim6: fdl=1: // vim6: fdl=1: