Exemplo n.º 1
0
 /**
  * Derive a key of the specified length based on the inputted secret
  *
  * @param string $secret The secret to base the key on
  * @param int    $length The length of the key to derive
  * @param string $other  Additional data to append to the key
  *
  * @return string The generated key
  */
 public function derive($secret, $length, $other = '')
 {
     $size = Hash::getHashSize($this->hash);
     $len = ceil($length / $size);
     $res = '';
     for ($i = 1; $i <= $len; $i++) {
         $tmp = pack('N', $i);
         $res .= hash($this->hash, $secret . $tmp . $other, true);
     }
     return substr($res, 0, $length);
 }
Exemplo n.º 2
0
 /**
  * Derive a key of the specified length based on the inputted secret
  *
  * @param string $secret The secret to base the key on
  * @param int    $length The length of the key to derive
  * @param string $other  Additional data to append to the key
  *
  * @return string The generated key
  */
 public function derive($secret, $length, $other = '')
 {
     $size = Hash::getHashSize($this->hash);
     $len = ceil($length / $size);
     $res = '';
     $stub = str_repeat(chr(0), max($this->options['pAmt'], 0));
     for ($i = 0; $i < $len; $i++) {
         $tmp = $stub . pack('N', $i);
         $res .= hash($this->hash, $tmp . $secret . $other, true);
     }
     return substr($res, 0, $length);
 }
Exemplo n.º 3
0
 /**
  * Derive a key from the supplied arguments
  *
  * @param string $password   The password to derive from
  * @param string $salt       The salt string to use
  * @param int    $iterations The number of iterations to use
  * @param int    $length     The size of the string to generate
  *
  * @return string The derived key
  */
 public function derive($password, $salt, $iterations, $length)
 {
     $size = Hash::getHashSize($this->hash);
     if ($length > $size) {
         $message = 'Length is too long for hash';
         throw new \InvalidArgumentException($message);
     }
     $tmp = hash($this->hash, $password . $salt, true);
     for ($i = 2; $i <= $iterations; $i++) {
         $tmp = hash($this->hash, $tmp, true);
     }
     return substr($tmp, 0, $length);
 }
Exemplo n.º 4
0
 /**
  * Generate the MAC using the supplied data
  *
  * @param string $data The data to use to generate the MAC with
  * @param string $key  The key to generate the MAC
  * @param int    $size The size of the output to return
  *
  * @return string The generated MAC of the appropriate size
  */
 public function generate($data, $key, $size = 0)
 {
     $hash = $this->options['hash'];
     $outputSize = Hash::getHashSize($hash);
     if ($size == 0) {
         $size = $outputSize;
     }
     if ($size > $outputSize) {
         throw new \OutOfRangeException(sprintf('The size is too big for the hash primitive [%d:%d]', $size, $outputSize));
     }
     $return = hash_hmac($hash, $data, $key, true);
     return substr($return, 0, $size);
 }
Exemplo n.º 5
0
 /**
  * Derive a key from the supplied arguments
  *
  * @param string $password   The password to derive from
  * @param string $salt       The salt string to use
  * @param int    $iterations The number of iterations to use
  * @param int    $length     The size of the string to generate
  *
  * @return string The derived key
  */
 public function derive($password, $salt, $iterations, $length)
 {
     $size = Hash::getHashSize($this->hash);
     $len = ceil($length / $size);
     $result = '';
     for ($i = 1; $i <= $len; $i++) {
         $tmp = hash_hmac($this->hash, $salt . pack('N', $i), $password, true);
         $res = $tmp;
         for ($j = 1; $j < $iterations; $j++) {
             $tmp = hash_hmac($this->hash, $tmp, $password, true);
             $res ^= $tmp;
         }
         $result .= $res;
     }
     return substr($result, 0, $length);
 }
Exemplo n.º 6
0
 public function testIsSecureDefault()
 {
     $this->assertEquals(false, Hash::isSecure('foobarbaz'));
 }