Hash
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
Default Constructor.
Sets the key for HMACs
Pre-compute the key used by the HMAC
Gets the hash function.
Sets the hash function.
Compute the HMAC.
Returns the hash length (in bytes)
Wrapper for MD5
Wrapper for SHA1
Pure-PHP implementation of MD2
Pure-PHP implementation of SHA256
Pure-PHP implementation of SHA384 and SHA512
Right Rotate
Right Shift
Not
Add
String Shift
Details
Hash
__construct(string $hash = 'sha1')
Default Constructor.
setKey(string $key = false)
Sets the key for HMACs
Keys can be of any length.
_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.
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
See {@link http://tools.ietf.org/html/rfc1319 RFC1319}.
_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}.
_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
_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.
string
_string_shift(string $string, int $index = 1)
String Shift
Inspired by array_shift