RC4: fixed ContinuousBuffer() using MODE_MCRYPT

Fixed multiple calls to ContinuousBuffer() using MODE_MCRYPT
This commit is contained in:
Hans-Jürgen Petrich 2013-01-21 21:01:37 +07:00
parent 45976002c7
commit 376b2759c9

View File

@ -192,6 +192,9 @@ class Crypt_RC4 {
case defined('MCRYPT_RC4'); case defined('MCRYPT_RC4');
$this->mode = MCRYPT_RC4; $this->mode = MCRYPT_RC4;
} }
$this->encryptStream = mcrypt_module_open($this->mode, '', MCRYPT_MODE_STREAM, '');
$this->decryptStream = mcrypt_module_open($this->mode, '', MCRYPT_MODE_STREAM, '');
} }
} }
@ -349,18 +352,11 @@ class Crypt_RC4 {
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) { if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
$keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream'; $keyStream = $mode == CRYPT_RC4_ENCRYPT ? 'encryptStream' : 'decryptStream';
if ($this->$keyStream === false) { if ($this->continuousBuffer) {
$this->$keyStream = mcrypt_module_open($this->mode, '', MCRYPT_MODE_STREAM, ''); return mcrypt_generic($this->$keyStream, $text);
mcrypt_generic_init($this->$keyStream, $this->key, '');
} else if (!$this->continuousBuffer) {
mcrypt_generic_init($this->$keyStream, $this->key, '');
} }
$newText = mcrypt_generic($this->$keyStream, $text); mcrypt_generic_init($this->$keyStream, $this->key, '');
if (!$this->continuousBuffer) { return mcrypt_generic($this->$keyStream, $text);
mcrypt_generic_deinit($this->$keyStream);
}
return $newText;
} }
if ($this->encryptStream === false) { if ($this->encryptStream === false) {
@ -442,6 +438,11 @@ class Crypt_RC4 {
*/ */
function enableContinuousBuffer() function enableContinuousBuffer()
{ {
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_MCRYPT ) {
mcrypt_generic_init($this->encryptStream, $this->key, '');
mcrypt_generic_init($this->decryptStream, $this->key, '');
}
$this->continuousBuffer = true; $this->continuousBuffer = true;
} }
@ -457,7 +458,7 @@ class Crypt_RC4 {
{ {
if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_INTERNAL ) { if ( CRYPT_RC4_MODE == CRYPT_RC4_MODE_INTERNAL ) {
$this->encryptIndex = $this->decryptIndex = array(0, 0); $this->encryptIndex = $this->decryptIndex = array(0, 0);
$this->setKey($this->key); $this->encryptStream = $this->decryptStream = false;
} }
$this->continuousBuffer = false; $this->continuousBuffer = false;
@ -508,24 +509,10 @@ class Crypt_RC4 {
*/ */
function _closeMCrypt() function _closeMCrypt()
{ {
if ( $this->encryptStream !== false ) { mcrypt_module_close($this->encryptStream);
if ( $this->continuousBuffer ) { mcrypt_module_close($this->decryptStream);
mcrypt_generic_deinit($this->encryptStream);
}
mcrypt_module_close($this->encryptStream);
$this->encryptStream = false;
}
if ( $this->decryptStream !== false ) {
if ( $this->continuousBuffer ) {
mcrypt_generic_deinit($this->decryptStream);
}
mcrypt_module_close($this->decryptStream);
$this->decryptStream = false;
}
} }
} }
// vim: ts=4:sw=4:et:
// vim6: fdl=1: