lithium\util\String::hash PHP Method

hash() public static method

Uses PHP's hashing functions to create a hash of the string provided, using the options specified. The default hash algorithm is SHA-512.
public static hash ( string $string, array $options = [] ) : string
$string string The string to hash.
$options array Supported options: - `'type'` _string_: Any valid hashing algorithm. See the `hash_algos()` function to determine which are available on your system. - `'salt'` _string_: A _salt_ value which, if specified, will be prepended to the string. - `'key'` _string_: If specified `hash_hmac()` will be used to hash the string, instead of `hash()`, with `'key'` being used as the message key. - `'raw'` _boolean_: If `true`, outputs the raw binary result of the hash operation. Defaults to `false`.
return string Returns a hashed string.
    public static function hash($string, array $options = array())
    {
        $defaults = array('type' => 'sha512', 'salt' => false, 'key' => false, 'raw' => false);
        $options += $defaults;
        if ($options['salt']) {
            $string = $options['salt'] . $string;
        }
        if ($options['key']) {
            return hash_hmac($options['type'], $string, $options['key'], $options['raw']);
        }
        return hash($options['type'], $string, $options['raw']);
    }

Usage Example

Beispiel #1
0
 /**
  * Generate hashed and salted token from `'prefix'` and `md5` hashed `$email` value
  * @param $email string User email that will be used as base for secret token
  * @param array $options Supported options:
  *        - `'prefix'` _string|int_ If not passed this method will generate random int from
  *          `100000` to `999999`. Hashed email will be prefixed with value of this option.
  *          Example: `'prefix_value' . md5($email)`
  *        - All other options are same as `lithium\util\String::hash()`
  * @return string Hashed prefixed email salted and hashed again
  * @see lithium\util\String::hash()
  */
 public static function generate($email, array $options = array())
 {
     $options += array('prefix' => null, 'salt' => LI3_UM_TokenSalt, 'type' => 'sha256');
     $prefix = $options['prefix'] ? $options['prefix'] : mt_rand(100000, 999999);
     unset($options['prefix']);
     return String::hash($prefix . md5($email), $options);
 }
All Usage Examples Of lithium\util\String::hash