Skip to content

Commit

Permalink
General code cleanup, variable name changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gilfether committed Jul 19, 2013
1 parent 2d3af13 commit 0405728
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 53 deletions.
13 changes: 8 additions & 5 deletions Cipher.php
Expand Up @@ -73,14 +73,14 @@ abstract class Cipher extends Base
/**
* Constructor
*
* @param string $cipher_name one of the predefined ciphers
* @param string $name one of the predefined ciphers
* @param string $key The key used for encryption
* @param int Optional, the required size of a key for the cipher
* @return void
*/
protected function __construct($cipher_name, $key = "", $required_key_sz = 0)
protected function __construct($name, $key = "", $required_key_sz = 0)
{
$this->cipher_name = $cipher_name;
$this->name($name);
$this->setKey($key, $required_key_sz);
}

Expand Down Expand Up @@ -164,8 +164,11 @@ public function operation($op = 0)
*
* @return string The cipher name
*/
public function name()
public function name($name = "")
{
if($name != "")
$this->cipher_name = $name;

return $this->cipher_name;
}

Expand Down Expand Up @@ -216,7 +219,7 @@ public function keySize()
* byte of the key string
* @return string The next substring of the key
*/
protected function keyNextSubstr($size = 1, $reset = false)
protected function keyChunk($size = 1, $reset = false)
{
if($reset || $this->key_pos >= strlen($this->key))
$this->key_pos = 0;
Expand Down
83 changes: 59 additions & 24 deletions Mode.php
Expand Up @@ -84,10 +84,10 @@ abstract class Mode
* @param string $mode_name The name of phpCrypt's modes, as defined in the mode
* @return void
*/
protected function __construct($cipher, $mode_name)
protected function __construct($mode_name, $cipher)
{
$this->cipher = $cipher;
$this->mode_name = $mode_name;
$this->name($mode_name);
$this->cipher($cipher);
}


Expand Down Expand Up @@ -157,7 +157,7 @@ abstract public function requiresIV();
* PHP_Crypt::IV_RAND, PHP_Crypt::IV_DEV_RAND, PHP_Crypt::IV_DEV_URAND
* @return string The IV that is being used by the mode
*/
public function createIV($src = "")
public function createIV($src = null)
{
// if the mode does not use an IV, lets not waste time
if(!$this->requiresIV())
Expand Down Expand Up @@ -186,31 +186,34 @@ public function createIV($src = "")
}

// now store the IV we created
$this->setIV($iv);

return $iv;
return $this->IV($iv);
}


/**
* Sets an IV for the mode to use
* Sets or Returns an IV for the mode to use
*
* @param string $iv An IV to use for the mode and cipher selected
* @param string $iv Optional, An IV to use for the mode and cipher selected
* @return void
*/
public function setIV($iv)
public function IV($iv = null)
{
// check that the iv is the correct length,
$len = strlen($iv);
if($len != $this->block_size)
if($iv != null)
{
$msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes";
trigger_error($msg, E_USER_WARNING);
// check that the iv is the correct length,
$len = strlen($iv);
if($len != $this->block_size)
{
$msg = "Incorrect IV size. Supplied length: $len bytes, Required: {$this->block_size} bytes";
trigger_error($msg, E_USER_WARNING);
}

$this->clearRegisters();
$this->register = $iv;
$this->iv = $iv;
}

$this->clearRegisters();
$this->register = $iv;
$this->iv = $iv;
return $this->iv;
}


Expand All @@ -231,25 +234,57 @@ public function checkIV()


/**
* Returns the Type of Mode in use
* Sets and returns the name of the mode being used
* If $name parameter is set, sets the mode. If
* $name is not set, returns the current mode in use
*
* @return string The name of the mode
* @param string $name Optional, One of the predefined
* phpCrypt mode constant names
* @return string One of the predefined phpCrypt mode
* constant mode names
*/
public function name()
public function name($name = "")
{
if($name != "")
$this->mode_name = $name;

return $this->mode_name;
}


/**
* Sets the type of padding to be used within the specified Mode
* Sets or Returns the padding type used with the mode
* If the $type parameter is not given, this function
* returns the the padding type only.
*
* @param string $type One of the predefined padding types
* @return void
*/
public function setPadding($type = PHP_Crypt::ZERO)
public function padding($type = "")
{
if($type != "")
$this->padding = $type;

return $this->padding;
}


/**
* Returns or Sets the cipher object being used
* If the $cipher parameter is set with a cipher object,
* the cipher current cipher will be set to this cipher.
* If the $cipher parameter is not set, returns the
* current cipher object being used.
*
* @param object $cipher Optional, An object of type Cipher
* @return object An object of type Cipher
*/
public function cipher($cipher = null)
{
$this->padding = $type;
if(is_object($cipher))
$this->cipher = $cipher;

return $this->cipher;
}


Expand Down
54 changes: 30 additions & 24 deletions phpCrypt.php
Expand Up @@ -223,7 +223,7 @@ public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::
}

// set the default padding
$this->setPadding($padding);
$this->padding($padding);
}


Expand Down Expand Up @@ -301,9 +301,10 @@ public function mode()


/**
* Return the Cipher Type used
* Returns the name of the cipher being used
*
* @return string The name of the cipher
* @return string The name of the cipher currently in use,
* it will be one of the predefined phpCrypt cipher constants
*/
public function cipherName()
{
Expand All @@ -312,9 +313,10 @@ public function cipherName()


/**
* Return the Mode Type used
* Return the name of the mode being used
*
* @return string The name of the mode
* @return string The name of the mode in use, it will
* be one of the predefined phpCrypt mode constants
*/
public function modeName()
{
Expand Down Expand Up @@ -344,6 +346,27 @@ public function cipherKeySize()
}


/**
* Sets the IV to use. Note that you do not need to call
* this function if creating an IV using createIV(). This
* function is used when an IV has already been created
* outside of phpCrypt and needs to be set. Alternatively
* you can just pass the $iv parameter to the encrypt()
* or decrypt() functions
*
* When the $iv parameter is not given, the function will
* return the current IV being used. See createIV() if you
* need to create an IV.
*
* @param string $iv Optional, The IV to use during Encryption/Decryption
* @return void
*/
public function IV($iv = "")
{
return $this->mode->IV($iv);
}


/**
* Creates an IV for the the Cipher selected, if one is required.
* If you already have an IV to use, this function does not need
Expand All @@ -366,32 +389,15 @@ public function createIV($src = self::IV_RAND)
}


/**
* Sets the IV to use. Note that you do not need to call
* this function if creating an IV using createIV(). This
* function is used when an IV has already been created
* outside of phpCrypt and needs to be set. Alternatively
* you can just pass the $iv parameter to the encrypt()
* or decrypt() functions
*
* @param string $iv The IV to use during Encryption/Decryption
* @return void
*/
public function setIV($iv)
{
$this->mode->setIV($iv);
}


/**
* Sets the type of padding to be used within the specified Mode
*
* @param string $type One of the predefined padding types
* @return void
*/
public function setPadding($type)
public function padding($type = "")
{
$this->mode->setPadding($type);
return $this->mode->padding($type);
}
}
?>

0 comments on commit 0405728

Please sign in to comment.