Skip to content

Commit

Permalink
Merge branch '1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Dec 16, 2023
2 parents a965db9 + b2a73d6 commit 43b8209
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 117 deletions.
20 changes: 7 additions & 13 deletions src/Crypt/AES.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,21 @@
class AES extends Base
{
/**
* Sets the key length
* Turns key lengths, be they valid or invalid, to valid key lengths
*
* Valid key lengths are 128, 192, and 256. If the length is less than 128, it will be rounded up to
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
*
* @see \phpseclib\Crypt\Rijndael:setKeyLength()
* @access public
* @param int $length
* @access private
* @return int
*/
public function setKeyLength($length)
protected function calculateNewKeyLength($length)
{
switch (true) {
case $length <= 128:
$length = 128;
break;
return 128;
case $length <= 192:
$length = 192;
break;
return 192;
default:
$length = 256;
return 256;
}
parent::setKeyLength($length);
}
}
4 changes: 2 additions & 2 deletions src/Crypt/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function setKeyLength($length)
{
// algorithms that have a fixed key length should override this with a method that does nothing
$this->changed = true;
$this->key_length = $length;
$this->key_length = static::calculateNewKeyLength($length);
$this->explicit_key_length = true;
}

Expand Down Expand Up @@ -307,7 +307,7 @@ public function setKey($key)
{
$this->key = $key;
if (!$this->explicit_key_length) {
$this->key_length = strlen($key) << 3;
$this->key_length = static::calculateNewKeyLength(strlen($key) << 3);
}
$this->changed = true;
}
Expand Down
20 changes: 10 additions & 10 deletions src/Crypt/Blowfish.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@
class Blowfish extends Base
{
/**
* Sets the key length.
* Turns key lengths, be they valid or invalid, to valid key lengths
*
* Key lengths can be between 32 and 448 bits.
*
* @access public
* @param int $length
* @access private
* @return int
*/
public function setKeyLength($length)
protected function calculateNewKeyLength($length)
{
if ($length < 32) {
$length = 32;
} elseif ($length > 448) {
$length = 448;
switch (true) {
case $length < 32:
return 32;
case $length > 448:
return 448;
}
parent::setKeyLength($length);
return $length;
}
}
9 changes: 5 additions & 4 deletions src/Crypt/DES.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
class DES extends Base
{
/**
* Dummy method
* Turns key lengths, be they valid or invalid, to valid key lengths
*
* @access public
* @param int $length
* @access private
* @return int
*/
public function setKeyLength($length)
protected function calculateNewKeyLength($length)
{
parent::setKeyLength(64);
return 64;
}
}
25 changes: 12 additions & 13 deletions src/Crypt/RC2.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,22 @@ public function __construct($mode = self::MODE_CBC)
}

/**
* Sets the key length.
* Turns key lengths, be they valid or invalid, to valid key lengths
*
* Valid key lengths are 8 to 1024.
* Calling this function after setting the key has no effect until the next
* \phpseclib\Crypt\RC2::setKey() call.
*
* @access public
* @param int $length in bits
* @param int $length
* @access private
* @return int
*/
public function setKeyLength($length)
protected function calculateNewKeyLength($length)
{
if ($length < 8) {
$length = 8;
} elseif ($length > 1024) {
$length = 1024;
switch (true) {
case $length < 8:
return 8;
case $length > 1024:
return 1024;
}
parent::setKeyLength($length);

return $length;
}

/**
Expand Down
21 changes: 11 additions & 10 deletions src/Crypt/RC4.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,21 @@
class RC4 extends Base
{
/**
* Sets the key length
* Turns key lengths, be they valid or invalid, to valid key lengths
*
* Keys can be between 1 and 256 bytes long.
*
* @access public
* @param int $length
* @access private
* @return int
*/
public function setKeyLength($length)
protected function calculateNewKeyLength($length)
{
if ($length < 8) {
$length = 8;
} elseif ($length > 2048) {
$length = 2048;
switch (true) {
case $length < 8:
return 8;
case $length > 2048:
return 2048;
}
parent::setKeyLength($length);

return $length;
}
}
64 changes: 23 additions & 41 deletions src/Crypt/Rijndael.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,47 +63,6 @@
*/
class Rijndael extends Base
{
/**
* Sets the key length.
*
* Valid key lengths are 128, 160, 192, 224, and 256. If the length is less than 128, it will be rounded up to
* 128. If the length is greater than 128 and invalid, it will be rounded down to the closest valid amount.
*
* Note: phpseclib extends Rijndael (and AES) for using 160- and 224-bit keys but they are officially not defined
* and the most (if not all) implementations are not able using 160/224-bit keys but round/pad them up to
* 192/256 bits as, for example, mcrypt will do.
*
* That said, if you want be compatible with other Rijndael and AES implementations,
* you should not setKeyLength(160) or setKeyLength(224).
*
* Additional: In case of 160- and 224-bit keys, phpseclib will/can, for that reason, not use
* the mcrypt php extension, even if available.
* This results then in slower encryption.
*
* @access public
* @param int $length
*/
public function setKeyLength($length)
{
switch (true) {
case $length <= 128:
$length = 128;
break;
case $length <= 160:
$length = 160;
break;
case $length <= 192:
$length = 192;
break;
case $length <= 224:
$length = 224;
break;
default:
$length = 256;
}
parent::setKeyLength($length);
}

/**
* Sets the block length
*
Expand All @@ -123,4 +82,27 @@ public function setBlockLength($length)
}
$this->cipher->setBlockLength($length);
}

/**
* Turns key lengths, be they valid or invalid, to valid key lengths
*
* @param int $length
* @access private
* @return int
*/
protected function calculateNewKeyLength($length)
{
switch (true) {
case $length <= 128:
return 128;
case $length <= 160:
return 160;
case $length <= 192:
return 192;
case $length <= 224:
return 224;
default:
return 256;
}
}
}
19 changes: 7 additions & 12 deletions src/Crypt/TripleDES.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,21 @@ public function __construct($mode = self::MODE_CBC)
}

/**
* Sets the key length
* Turns key lengths, be they valid or invalid, to valid key lengths
*
* Keys can be between 1 and 256 bytes long.
*
* @access public
* @param int $length
* @access private
* @return int
*/
public function setKeyLength($length)
protected function calculateNewKeyLength($length)
{
switch (true) {
case $length <= 64:
$length = 64;
break;
return 64;
case $length <= 128:
$length = 128;
break;
return 128;
default:
$length = 192;
return 192;
}

parent::setKeyLength($length);
}
}
19 changes: 7 additions & 12 deletions src/Crypt/Twofish.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,21 @@
class Twofish extends Base
{
/**
* Sets the key length
* Turns key lengths, be they valid or invalid, to valid key lengths
*
* Keys can be between 1 and 256 bytes long.
*
* @access public
* @param int $length
* @access private
* @return int
*/
public function setKeyLength($length)
protected function calculateNewKeyLength($length)
{
switch (true) {
case $length <= 128:
$length = 128;
break;
return 128;
case $length <= 192:
$length = 192;
break;
return 192;
default:
$length = 256;
return 256;
}

parent::setKeyLength($length);
}
}

0 comments on commit 43b8209

Please sign in to comment.