lithium\util\String::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.

Parameters

  • string $string

    The string to hash.

  • array $options

    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.

Returns

string

Returns a hashed string.

Source

	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']);
	}