Example #1
0
 /**
  * @see \Fine47\MicroRouter\Interfaces\Encryptor::encryptString()
  */
 public function encryptString($data, $asBase64 = false, $randomize = true)
 {
     $iv = $this->getIV();
     // Compute the length of the IV.
     $keySize = strlen($iv);
     // Size of original data.
     $dataSize = strlen($data);
     // Static filler.
     $staticFiller = chr(self::CHAR_FILL);
     // The data that will be encoded is the following:
     //
     // 4-byte random characters
     // TAB character
     // 4-byte length (of the original data)
     // TAB character
     // 4-byte random characters
     // TAB character
     // original data passed in $data
     // TAB character
     // padding necessary for the encryption (optional)
     // Generate 4-byte random characters.
     $string = true === $randomize ? self::randChars(4) : str_repeat($staticFiller, 4);
     // Tab character separator.
     $string .= "\t";
     // Add length of the original data.
     $string .= pack('L*', $dataSize) . "\t";
     // Generate additional 8-byte random characters.
     $string .= true === $randomize ? self::randChars(4) : str_repeat($staticFiller, 4);
     // Tab character separator.
     $string .= "\t";
     // Add original data.
     $string .= $data . "\t";
     // Is there a salt to be added?
     if (is_string($randomize) && !empty($randomize)) {
         $string .= $randomize;
         $randomize = true;
     }
     // Now, add any necessary padding so overall data may be encrypted.
     while (0 < strlen($string) % $keySize) {
         $string .= !$randomize ? $staticFiller : self::randChar();
     }
     // Prepare encryption buffers.
     mcrypt_generic_init($this->module, $this->getSecret(), $iv);
     // Encrypt string.
     $string = mcrypt_generic($this->module, $string);
     // Free resources.
     mcrypt_generic_deinit($this->module);
     // Turn it into hex if needed.
     if (true === $asBase64) {
         $string = Base64::encode($string);
     }
     return $string;
 }
Example #2
0
 /**
  * Retrieves the flash data as an encoded string. If there's no data in flash,
  * FALSE is returned.
  *
  * @return string|FALSE encoded flash data, FALSE if empty
  */
 public function toString()
 {
     if (empty($this->newFlash)) {
         // No flash data.
         return false;
     }
     // Return encoded flash data.
     return Base64::encode(Json::encode($this->newFlash));
 }