]> git.mxchange.org Git - core.git/commitdiff
Added parameter 'key' to encryption methods to allow own keys
authorRoland Häder <roland@mxchange.org>
Tue, 29 Mar 2011 09:09:08 +0000 (09:09 +0000)
committerRoland Häder <roland@mxchange.org>
Tue, 29 Mar 2011 09:09:08 +0000 (09:09 +0000)
inc/classes/interfaces/crypto/class_Cryptable.php
inc/classes/interfaces/streams/crypto/class_EncryptableStream.php
inc/classes/main/crypto/class_CryptoHelper.php
inc/classes/main/rng/class_RandomNumberGenerator.php
inc/classes/main/streams/crypto/class_McryptStream.php

index d9556834e1807a232e20c4dee795379b73c49a29..613f5057707dcd97590b998614fe5b7a0bd0689a 100644 (file)
@@ -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
index 81d4861a5033587a6c09dd9e3e78b0a57f3b21d4..d40e19158bc36c414c78694c549104bc86ddd727 100644 (file)
@@ -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
index 4e9cc4a6146cca4512bda09697bc15fdf4893766..5e40560622673b8b4d4e6d83a300eb14e7f3eb86 100644 (file)
@@ -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;
index 6ab821a164e14709fee5181a684bea9372e2c8d5..00eb513fe4f2e9158128fbcaddefc79fad9ba7ff 100644 (file)
@@ -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);
index 732ab56c838d6e9eddd92f3640e17743b7fc78f1..a0fc4b53a149a8efee6b3a7b351d14d941c8d2ce 100644 (file)
@@ -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)) {