/**
* First parameter seperator
*/
- private $firstParameter = '?';
+ const FIRST_PARAMETER_SEPERATOR = '?';
/**
* Seperator for more paraemters
*/
- private $parameterSeperator = '&';
+ const EXTRA_PARAMETER_SEPERATOR = '&';
/**
* Protected constructor
} // END - if
// Default parameter seperator is &
- $seperator = $this->parameterSeperator;
+ $seperator = self::EXTRA_PARAMETER_SEPERATOR;
// Is there a question mark in?
- $linkArray = explode($this->firstParameter, $this->getLinkBase());
+ $linkArray = explode(self::FIRST_PARAMETER_SEPERATOR, $this->getLinkBase());
if (count($linkArray) == 0) {
// No question mark
- $seperator = $this->firstParameter;
+ $seperator = self::FIRST_PARAMETER_SEPERATOR;
} // END - if
// Prepare action
/**
* File header indicator
*/
- private $fileHeader = '@head';
+ const FILE_IO_FILE_HEADER_ID = '@head';
/**
* Data block indicator
*/
- private $dataBlock = '@data';
+ const FILE_IO_DATA_BLOCK_ID = '@data';
/**
* Seperator #1
*/
- private $chunker = ':';
+ const FILE_IO_CHUNKER = ':';
/**
* Seperator #2
*/
- private $seperator = '^';
+ const FILE_IO_SEPERATOR = '^';
/**
* Protected constructor
// Write a header information for validation purposes
$fileInstance->writeToFile(sprintf("%s%s%s%s%s%s%s%s%s\n",
- $this->fileHeader,
- $this->seperator,
+ self::FILE_IO_FILE_HEADER_ID,
+ self::FILE_IO_SEPERATOR,
$dataArray[0],
- $this->chunker,
+ self::FILE_IO_CHUNKER,
time(),
- $this->chunker,
+ self::FILE_IO_CHUNKER,
strlen($dataArray[1]),
- $this->chunker,
+ self::FILE_IO_CHUNKER,
md5($dataArray[1])
));
// Save it to the stream
$fileInstance->writeToFile(sprintf("%s%s%s%s%s\n",
- $this->dataBlock,
- $this->seperator,
+ self::FILE_IO_DATA_BLOCK_ID,
+ self::FILE_IO_SEPERATOR,
$line,
- $this->chunker,
+ self::FILE_IO_CHUNKER,
md5($line)
));
$rawLine = rtrim($rawLine);
// Analyze this line
- if (substr($rawLine, 0, 5) == $this->fileHeader) {
+ if (substr($rawLine, 0, 5) == self::FILE_IO_FILE_HEADER_ID) {
// Header found, so let's extract it
- $header = explode($this->seperator, $rawLine);
+ $header = explode(self::FILE_IO_SEPERATOR, $rawLine);
$header = trim($header[1]);
// Now we must convert it again into an array
- $header = explode($this->chunker, $header);
+ $header = explode(self::FILE_IO_CHUNKER, $header);
// Is the header (maybe) valid?
if (count($header) != 4) {
// Throw an exception
throw new InvalidArrayCountException(array($this, 'header', count($header), 4), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
} // END - if
- } elseif (substr($rawLine, 0, 5) == $this->dataBlock) {
+ } elseif (substr($rawLine, 0, 5) == self::FILE_IO_DATA_BLOCK_ID) {
// Is a data line!
- $data = explode($this->seperator, $rawLine);
+ $data = explode(self::FILE_IO_SEPERATOR, $rawLine);
$data = $data[1];
// First element is the data, second the MD5 checksum
- $data = explode($this->chunker, $data);
+ $data = explode(self::FILE_IO_CHUNKER, $data);
// Validate the read line
if (count($data) == 2) {
/**
* Seperator on many places
*/
- private $seperator = '|';
+ const DATA_PAYLOAD_SEPERATOR = '|';
/**
* Protected constructor
$key = $this->getRngInstance()->generateKey();
} // END - if
- // Add some "garbage" to the string
+ // Add some "payload" to the string
switch ($this->getRngInstance()->randomNumber(0, 8)) {
case 0:
- $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+ $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . crc32($this->getRngInstance()->randomString(20));
break;
case 1:
- $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+ $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . md5($this->getRngInstance()->randomString(20));
break;
case 2:
- $garbageString = crc32($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
+ $payloadString = crc32($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . sha1($this->getRngInstance()->randomString(20));
break;
case 3:
- $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+ $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . crc32($this->getRngInstance()->randomString(20));
break;
case 4:
- $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+ $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . md5($this->getRngInstance()->randomString(20));
break;
case 5:
- $garbageString = md5($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
+ $payloadString = md5($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . sha1($this->getRngInstance()->randomString(20));
break;
case 6:
- $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . crc32($this->getRngInstance()->randomString(20));
+ $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . crc32($this->getRngInstance()->randomString(20));
break;
case 7:
- $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . md5($this->getRngInstance()->randomString(20));
+ $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . md5($this->getRngInstance()->randomString(20));
break;
case 8:
- $garbageString = sha1($this->getRngInstance()->randomString(10)) . $this->seperator . base64_encode($str) . $this->seperator . sha1($this->getRngInstance()->randomString(20));
+ $payloadString = sha1($this->getRngInstance()->randomString(10)) . self::DATA_PAYLOAD_SEPERATOR . base64_encode($str) . self::DATA_PAYLOAD_SEPERATOR . sha1($this->getRngInstance()->randomString(20));
break;
}
// Encrypt the string
- $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $garbageString, MCRYPT_MODE_ECB, $iv);
+ $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $payloadString, MCRYPT_MODE_ECB, $iv);
// Return the string
return $encrypted;
* Decrypt the string with fixed salt
*
* @param $encrypted Encrypted string
+ * @param $key Optional key, if none provided, a random key will be generated
* @return $str The unencrypted string
*/
- public function decryptStream ($encrypted) {
+ public function decryptStream ($encrypted, $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();
+ // Shall we use a default key or custom?
+ if (is_null($key) {
+ // Generate (default) key
+ $key = $this->getRngInstance()->generateKey();
+ } // END - if
// Decrypt the string
- $garbageString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
+ $payloadString = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB, $iv);
// Get the real string out
- $strArray = explode($this->seperator, $garbageString);
+ $strArray = explode(self::DATA_PAYLOAD_SEPERATOR, $payloadString);
// Does the element count match?
assert(count($strArray) == 3);