Exemple #1
0
 /**
  * Generate a config string from an array.
  *
  * @param array $config Array of configuration options.
  * @return string Configuration string.
  * @throws InvalidArgumentException Throws an InvalidArgumentException if
  *     any passed-in configuration options are invalid.
  */
 public static function genConfig(array $config = array())
 {
     $defaults = array('salt' => Utilities::encode64(Utilities::genRandomBytes(1)));
     $config = array_merge($defaults, array_change_key_case($config, CASE_LOWER));
     $string = '*1';
     if (self::validateOptions($config)) {
         $string = $config['salt'];
     }
     return $string;
 }
Exemple #2
0
 /**
  * Generate a config string from an array.
  *
  * @param array $config Array of configuration options.
  * @return string Configuration string.
  * @throws InvalidArgumentException Throws an InvalidArgumentException if
  *     any passed-in configuration options are invalid.
  */
 public static function genConfig(array $config = array())
 {
     $defaults = array('rounds' => 80000, 'salt' => Utilities::encode64(Utilities::genRandomBytes(12)));
     $config = array_merge($defaults, array_change_key_case($config, CASE_LOWER));
     $string = '*1';
     if (self::validateOptions($config)) {
         $rounds = '';
         if ($config['rounds'] != 5000) {
             $rounds = sprintf('rounds=%d$', $config['rounds']);
         }
         $string = sprintf('$5$%s%s', $rounds, $config['salt']);
     }
     return $string;
 }
Exemple #3
0
 /**
  * Generate a config string from an array.
  *
  * @param array $config Array of configuration options.
  * @return string Configuration string.
  * @throws InvalidArgumentException Throws an InvalidArgumentException if
  *     any passed-in configuration options are invalid.
  */
 public static function genConfig(array $config = array())
 {
     $defaults = array('rounds' => 5001, 'salt' => Utilities::encode64(Utilities::genRandomBytes(3)));
     $config = array_merge($defaults, array_change_key_case($config, CASE_LOWER));
     $string = '*1';
     if (self::validateOptions($config)) {
         // Rounds needs to be odd in order to avoid exposing weak DES keys
         if ($config['rounds'] % 2 == 0) {
             --$config['rounds'];
         }
         $string = sprintf('_%s%s', Utilities::encodeInt24($config['rounds']), $config['salt']);
     }
     return $string;
 }
Exemple #4
0
 /**
  * Generate a password hash using a config string.
  *
  * @param string $password Password string.
  * @param string $config Configuration string.
  * @return string Returns the hash string on success. On failure, one of
  *     *0 or *1 is returned.
  */
 public static function genHash($password, $config)
 {
     $hash = $config == '*0' ? '*1' : '*0';
     $config = self::parseConfig($config);
     if (is_array($config)) {
         $rounds = 1 << $config['rounds'];
         $checksum = md5($config['salt'] . $password, true);
         do {
             $checksum = md5($checksum . $password, true);
         } while (--$rounds);
         $hash = self::genConfig($config) . Utilities::encode64($checksum);
     }
     return $hash;
 }
Exemple #5
0
 /**
  * Generate a password hash using a config string.
  *
  * @param string $password Password string.
  * @param string $config Configuration string.
  * @return string Returns the hash string on success. On failure, one of
  *     *0 or *1 is returned.
  */
 public static function genHash($password, $config)
 {
     $hash = $config == '*0' ? '*1' : '*0';
     $config = self::parseConfig($config);
     if (is_array($config)) {
         $rounds = $config['rounds'];
         $checksum = hash_hmac('sha1', $config['salt'] . '$sha1$' . $rounds--, $password, true);
         if ($rounds) {
             do {
                 $checksum = hash_hmac('sha1', $checksum, $password, true);
             } while (--$rounds);
         }
         $tmp = '';
         foreach (array(2, 1, 0, 5, 4, 3, 8, 7, 6, 11, 10, 9, 14, 13, 12, 17, 16, 15, 0, 19, 18) as $offset) {
             $tmp .= $checksum[$offset];
         }
         $checksum = Utilities::encode64($tmp);
         $hash = self::genConfig($config) . '$' . $checksum;
     }
     return $hash;
 }