Exemplo n.º 1
0
 /**
  * Generate the SIP-2-4 hash of the specified string. No double hashing is
  * employed for this algorithm, though.
  *
  * @see \Fine47\MicroRouter\Interfaces\Hasher::generate()
  */
 public function generate($data, $asRaw = false)
 {
     if (!is_string($data)) {
         $data = serialize($data);
     }
     $data = sip_hash($this->getSalt(), $data);
     if (!$asRaw) {
         $data = String::bin2hex($data);
     }
     return $data;
 }
Exemplo n.º 2
0
 /**
  * @see \Fine47\MicroRouter\Interfaces\Encryptor::encryptNumber()
  */
 public function encryptNumber($number, $randomize = true)
 {
     $iv = $this->getIV();
     // The data is comprised of the number, a tab character and random data.
     $string = $number . "\t";
     // Compute the length of the IV.
     $keySize = strlen($iv);
     // Fill with random characters or a filler?
     if (true === $randomize) {
         // Randomize the output.
         while (0 < strlen($string) % $keySize) {
             $string .= self::randChar();
         }
     } else {
         // Is there a salt to be added?
         if (is_string($randomize) && !empty($randomize)) {
             $string .= $randomize;
         }
         // Static filler.
         $staticFiller = chr(self::CHAR_FILL);
         // Fill with a constant character to properly pad the string.
         while (0 < strlen($string) % $keySize) {
             $string .= $staticFiller;
         }
     }
     // 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.
     $string = String::bin2hex($string);
     return $string;
 }