class Hash (View source)

Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.

Constants

MODE_INTERNAL

Toggles the internal implementation

MODE_MHASH

Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+.

MODE_HASH

Toggles the hash() implementation, which works on PHP 5.1.2+.

Properties

int $hashParam Hash Parameter
int $b Byte-length of compression blocks / key (Internal HMAC)
int $l Byte-length of hash output (Internal HMAC)
string $hash Hash Algorithm
string $key Key
string $computedKey Computed Key
string $opad Outer XOR (Internal HMAC)
string $ipad Inner XOR (Internal HMAC)
string $engine Engine

Methods

__construct(string $hash = 'sha1')

Default Constructor.

setKey(string $key = false)

Sets the key for HMACs

_computeKey()

Pre-compute the key used by the HMAC

string
getHash()

Gets the hash function.

setHash(string $hash)

Sets the hash function.

string
hash(string $text)

Compute the HMAC.

int
getLength()

Returns the hash length (in bytes)

_md5(string $m)

Wrapper for MD5

_sha1(string $m)

Wrapper for SHA1

_md2(string $m)

Pure-PHP implementation of MD2

_sha256(string $m)

Pure-PHP implementation of SHA256

_sha512(string $m)

Pure-PHP implementation of SHA384 and SHA512

int
_rightRotate(int $int, int $amt)

Right Rotate

int
_rightShift(int $int, int $amt)

Right Shift

int
_not(int $int)

Not

int
_add()

Add

string
_string_shift(string $string, int $index = 1)

String Shift

Details

Hash __construct(string $hash = 'sha1')

Default Constructor.

Parameters

string $hash

Return Value

Hash

setKey(string $key = false)

Sets the key for HMACs

Keys can be of any length.

Parameters

string $key

_computeKey()

Pre-compute the key used by the HMAC

Quoting http://tools.ietf.org/html/rfc2104#section-2, "Applications that use keys longer than B bytes will first hash the key using H and then use the resultant L byte string as the actual key to HMAC."

As documented in https://www.reddit.com/r/PHP/comments/9nct2l/symfonypolyfill_hash_pbkdf2_correct_fix_for/ when doing an HMAC multiple times it's faster to compute the hash once instead of computing it during every call

string getHash()

Gets the hash function.

As set by the constructor or by the setHash() method.

Return Value

string

setHash(string $hash)

Sets the hash function.

Parameters

string $hash

string hash(string $text)

Compute the HMAC.

Parameters

string $text

Return Value

string

int getLength()

Returns the hash length (in bytes)

Return Value

int

_md5(string $m)

Wrapper for MD5

Parameters

string $m

_sha1(string $m)

Wrapper for SHA1

Parameters

string $m

_md2(string $m)

Pure-PHP implementation of MD2

See {@link http://tools.ietf.org/html/rfc1319 RFC1319}.

Parameters

string $m

_sha256(string $m)

Pure-PHP implementation of SHA256

See {@link http://en.wikipedia.org/wiki/SHA_hash_functions#SHA-256_.28a_SHA-2_variant.29_pseudocode SHA-256 (a SHA-2 variant) pseudocode - Wikipedia}.

Parameters

string $m

_sha512(string $m)

Pure-PHP implementation of SHA384 and SHA512

Parameters

string $m

int _rightRotate(int $int, int $amt)

Right Rotate

Parameters

int $int
int $amt

Return Value

int

See also

\self::_sha256()

int _rightShift(int $int, int $amt)

Right Shift

Parameters

int $int
int $amt

Return Value

int

See also

\self::_sha256()

int _not(int $int)

Not

Parameters

int $int

Return Value

int

See also

\self::_sha256()

int _add()

Add

_sha256() adds multiple unsigned 32-bit integers. Since PHP doesn't support unsigned integers and since the possibility of overflow exists, care has to be taken. BigInteger could be used but this should be faster.

Return Value

int

See also

\self::_sha256()

string _string_shift(string $string, int $index = 1)

String Shift

Inspired by array_shift

Parameters

string $string
int $index

Return Value

string