phpseclib API Documentation
Class

phpseclib\Crypt\Hash

class Hash

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

Hash __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

at line 158
public Hash __construct(string $hash = 'sha1')

Default Constructor.

Parameters

string $hash

Return Value

Hash

at line 184
public setKey(string $key = false)

Sets the key for HMACs

Keys can be of any length.

Parameters

string $key

at line 202
public _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/symfonypolyfillhashpbkdf2correctfix_for/ when doing an HMAC multiple times it's faster to compute the hash once instead of computing it during every call

at line 234
public string getHash()

Gets the hash function.

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

Return Value

string

at line 245
public setHash(string $hash)

Sets the hash function.

Parameters

string $hash

at line 371
public string hash(string $text)

Compute the HMAC.

Parameters

string $text

Return Value

string

at line 412
public int getLength()

Returns the hash length (in bytes)

Return Value

int

at line 423
public _md5(string $m)

Wrapper for MD5

Parameters

string $m

at line 434
public _sha1(string $m)

Wrapper for SHA1

Parameters

string $m

at line 447
public _md2(string $m)

Pure-PHP implementation of MD2

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

Parameters

string $m

at line 523
public _sha256(string $m)

Pure-PHP implementation of SHA256

See {@link http://en.wikipedia.org/wiki/SHAhashfunctions#SHA-256.28aSHA-2variant.29pseudocode SHA-256 (a SHA-2 variant) pseudocode - Wikipedia}.

Parameters

string $m

at line 629
public _sha512(string $m)

Pure-PHP implementation of SHA384 and SHA512

Parameters

string $m

at line 811
public int _rightRotate(int $int, int $amt)

Right Rotate

Parameters

int $int
int $amt

Return Value

int

See also

self::_sha256()

at line 827
public int _rightShift(int $int, int $amt)

Right Shift

Parameters

int $int
int $amt

Return Value

int

See also

self::_sha256()

at line 841
public int _not(int $int)

Not

Parameters

int $int

Return Value

int

See also

self::_sha256()

at line 857
public 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()

at line 888
public string _string_shift(string $string, int $index = 1)

String Shift

Inspired by array_shift

Parameters

string $string
int $index

Return Value

string