From: Roland Häder Date: Tue, 29 Mar 2011 09:09:08 +0000 (+0000) Subject: Added parameter 'key' to encryption methods to allow own keys X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=95639d1f3e08fc874abad5f8adeac719bbdb246f;p=core.git Added parameter 'key' to encryption methods to allow own keys --- diff --git a/inc/classes/interfaces/crypto/class_Cryptable.php b/inc/classes/interfaces/crypto/class_Cryptable.php index d9556834..613f5057 100644 --- a/inc/classes/interfaces/crypto/class_Cryptable.php +++ b/inc/classes/interfaces/crypto/class_Cryptable.php @@ -38,9 +38,10 @@ interface Cryptable extends FrameworkInterface { * Encrypt the string with fixed salt * * @param $str The unencrypted string + * @param $key Optional key, if none provided, a random key will be generated * @return $encrypted Encrypted string */ - function encryptString ($str); + function encryptString ($str, $key = null); /** * Decrypt the string with fixed salt diff --git a/inc/classes/interfaces/streams/crypto/class_EncryptableStream.php b/inc/classes/interfaces/streams/crypto/class_EncryptableStream.php index 81d4861a..d40e1915 100644 --- a/inc/classes/interfaces/streams/crypto/class_EncryptableStream.php +++ b/inc/classes/interfaces/streams/crypto/class_EncryptableStream.php @@ -26,9 +26,10 @@ interface EncryptableStream extends Streamable { * Encrypt the string with fixed salt * * @param $str The unencrypted string + * @param $key Optional key, if none provided, a random key will be generated * @return $encrypted Encrypted string */ - function encryptStream ($str); + function encryptStream ($str, $key = null); /** * Decrypt the string with fixed salt diff --git a/inc/classes/main/crypto/class_CryptoHelper.php b/inc/classes/main/crypto/class_CryptoHelper.php index 4e9cc4a6..5e405606 100644 --- a/inc/classes/main/crypto/class_CryptoHelper.php +++ b/inc/classes/main/crypto/class_CryptoHelper.php @@ -179,11 +179,12 @@ class CryptoHelper extends BaseFrameworkSystem implements Cryptable { * Encrypt the string with fixed salt * * @param $str The unencrypted string + * @param $key Optional key, if none provided, a random key will be generated * @return $encrypted Encrypted string */ - public function encryptString ($str) { + public function encryptString ($str, $key = null) { // Encrypt the string through the stream - $encrypted = $this->cryptoStreamInstance->encryptStream($str); + $encrypted = $this->cryptoStreamInstance->encryptStream($str, $key); // Return the string return $encrypted; diff --git a/inc/classes/main/rng/class_RandomNumberGenerator.php b/inc/classes/main/rng/class_RandomNumberGenerator.php index 6ab821a1..00eb513f 100644 --- a/inc/classes/main/rng/class_RandomNumberGenerator.php +++ b/inc/classes/main/rng/class_RandomNumberGenerator.php @@ -126,7 +126,9 @@ class RandomNumberGenerator extends BaseFrameworkSystem { */ public function randomString ($length = -1) { // Is the number <1, then fix it to default length - if ($length < 1) $length = $this->rndStrLen; + if ($length < 1) { + $length = $this->rndStrLen; + } // END - if // Initialize the string $randomString = ''; @@ -135,7 +137,7 @@ class RandomNumberGenerator extends BaseFrameworkSystem { for ($idx = 0; $idx < $length; $idx++) { // Add a random character and add it to our string $randomString .= chr($this->randomNumber(0, 255)); - } + } // END - for // Return the random string a little mixed up return str_shuffle($randomString); diff --git a/inc/classes/main/streams/crypto/class_McryptStream.php b/inc/classes/main/streams/crypto/class_McryptStream.php index 732ab56c..a0fc4b53 100644 --- a/inc/classes/main/streams/crypto/class_McryptStream.php +++ b/inc/classes/main/streams/crypto/class_McryptStream.php @@ -58,15 +58,19 @@ class McryptStream extends BaseStream implements EncryptableStream { * Encrypt the string with fixed salt * * @param $str The unencrypted string + * @param $key Optional key, if none provided, a random key will be generated * @return $encrypted Encrypted string */ - public function encryptStream ($str) { + public function encryptStream ($str, $key = null) { // Init crypto module $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); - // Generate key - $key = $this->getRngInstance()->generateKey(); + // Generate key, if none provided + if (is_null($key)) { + // None provided + $key = $this->getRngInstance()->generateKey(); + } // END - if // Add some "garbage" to the string switch ($this->getRngInstance()->randomNumber(0, 8)) {