Documentation

Hash
in package

Tags
author

Jim Wigginton terrafrost@php.net

author

Andreas Fischer bantu@phpbb.com

access

public

Table of Contents

Constants

PADDING_SHA3  = 2
Padding Types
PADDING_SHAKE  = 3
Padding Types

Properties

$algo  : string
Hash Algorithm
$c  : AES
umac cipher object
$computedKey  : string
Computed Key
$factory128  : mixed
$factory36  : mixed
$factory64  : mixed
$hashParam  : int
Hash Parameter
$ipad  : string
Inner XOR (Internal HMAC)
$key  : string
Key
$length  : int
Byte-length of hash output (Internal HMAC)
$marker128  : mixed
$marker64  : mixed
$maxwordrange128  : mixed
$maxwordrange64  : mixed
$nonce  : string
Nonce
$offset128  : mixed
$offset64  : mixed
$opad  : string
Outer XOR (Internal HMAC)
$pad  : string
umac pad
$paddingType  : int
Padding Type
$parameters  : array<string|int, mixed>
Hash Parameters
$recomputeAESKey  : bool
Recompute AES Key

Methods

__construct()  : mixed
Default Constructor.
__toString()  : mixed
__toString() magic method
getBlockLength()  : int
Returns the block length (in bits)
getBlockLengthInBytes()  : int
Returns the block length (in bytes)
getHash()  : string
Gets the hash function.
getLength()  : int
Returns the hash length (in bits)
getLengthInBytes()  : int
Returns the hash length (in bytes)
hash()  : string
Compute the Hash / HMAC / UMAC.
setHash()  : mixed
Sets the hash function.
setKey()  : mixed
Sets the key for HMACs
setNonce()  : mixed
Sets the nonce for UMACs
computeKey()  : mixed
Pre-compute the key used by the HMAC
kdf()  : string
KDF: Key-Derivation Function
L1Hash()  : string
L1-HASH Algorithm
L2Hash()  : string
L2-HASH: Second-Layer Hash
L3Hash()  : string
L3-HASH: Third-Layer Hash
nh()  : string
NH Algorithm
pdf()  : string
PDF Algorithm
poly()  : int
POLY Algorithm
processSHA3Block32()  : mixed
32-bit block processing method for SHA3
processSHA3Block64()  : mixed
64-bit block processing method for SHA3
rotateLeft32()  : mixed
Rotate 32-bit int
rotateLeft64()  : mixed
Rotate 64-bit int
sha3_32()  : mixed
Pure-PHP 32-bit implementation of SHA3
sha3_64()  : mixed
Pure-PHP 64-bit implementation of SHA3
sha3_pad()  : string
Pads SHA3 based on the mode
sha512()  : string
Pure-PHP implementation of SHA512
uhash()  : string
UHASH Algorithm

Constants

PADDING_SHA3

Padding Types

public mixed PADDING_SHA3 = 2
Tags
access

private

PADDING_SHAKE

Padding Types

public mixed PADDING_SHAKE = 3
Tags
access

private

Properties

$algo

Hash Algorithm

private string $algo
Tags
see
self::setHash()
access

private

$c

umac cipher object

private AES $c
Tags
see
self::hash()
access

private

$computedKey

Computed Key

private string $computedKey = false
Tags
see
self::_computeKey()
access

private

$factory128

private static mixed $factory128

$factory36

private static mixed $factory36

$factory64

private static mixed $factory64

$hashParam

Hash Parameter

private int $hashParam
Tags
see
self::setHash()
access

private

$ipad

Inner XOR (Internal HMAC)

private string $ipad

Used only for sha512/*

Tags
see
self::hash()
access

private

$key

Key

private string $key = false
Tags
see
self::setKey()
access

private

$length

Byte-length of hash output (Internal HMAC)

private int $length
Tags
see
self::setHash()
access

private

$marker128

private static mixed $marker128

$marker64

private static mixed $marker64

$maxwordrange128

private static mixed $maxwordrange128

$maxwordrange64

private static mixed $maxwordrange64

$nonce

Nonce

private string $nonce = false
Tags
see
self::setNonce()
access

private

$offset128

private static mixed $offset128

$offset64

private static mixed $offset64

$opad

Outer XOR (Internal HMAC)

private string $opad

Used only for sha512/*

Tags
see
self::hash()
access

private

$pad

umac pad

private string $pad
Tags
see
self::hash()
access

private

$paddingType

Padding Type

private int $paddingType = 0

Only used by SHA3

Tags
access

private

$parameters

Hash Parameters

private array<string|int, mixed> $parameters = []
Tags
access

private

$recomputeAESKey

Recompute AES Key

private bool $recomputeAESKey

Used only for umac

Tags
see
self::hash()
access

private

Methods

__construct()

Default Constructor.

public __construct([string $hash = 'sha256' ]) : mixed
Parameters
$hash : string = 'sha256'
Tags
access

public

__toString()

__toString() magic method

public __toString() : mixed

getBlockLength()

Returns the block length (in bits)

public getBlockLength() : int
Tags
access

public

Return values
int

getBlockLengthInBytes()

Returns the block length (in bytes)

public getBlockLengthInBytes() : int
Tags
access

public

Return values
int

getHash()

Gets the hash function.

public getHash() : string

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

Tags
access

public

Return values
string

getLength()

Returns the hash length (in bits)

public getLength() : int
Tags
access

public

Return values
int

getLengthInBytes()

Returns the hash length (in bytes)

public getLengthInBytes() : int
Tags
access

public

Return values
int

hash()

Compute the Hash / HMAC / UMAC.

public hash(string $text) : string
Parameters
$text : string
Tags
access

public

Return values
string

setHash()

Sets the hash function.

public setHash(string $hash) : mixed
Parameters
$hash : string
Tags
access

public

setKey()

Sets the key for HMACs

public setKey([string $key = false ]) : mixed

Keys can be of any length.

Parameters
$key : string = false
Tags
access

public

setNonce()

Sets the nonce for UMACs

public setNonce([string $nonce = false ]) : mixed

Keys can be of any length.

Parameters
$nonce : string = false
Tags
access

public

computeKey()

Pre-compute the key used by the HMAC

private computeKey() : mixed

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

Tags
access

private

kdf()

KDF: Key-Derivation Function

private kdf(int $index, int $numbytes) : string

The key-derivation function generates pseudorandom bits used to key the hash functions.

Parameters
$index : int

a non-negative integer less than 2^64

$numbytes : int

a non-negative integer less than 2^64

Return values
string

string of length numbytes bytes

L1Hash()

L1-HASH Algorithm

private static L1Hash(string $k, string $m) : string

The first-layer hash breaks the message into 1024-byte chunks and hashes each with a function called NH. Concatenating the results forms a string, which is up to 128 times shorter than the original.

Parameters
$k : string

string of length 1024 bytes.

$m : string

string of length less than 2^67 bits.

Return values
string

string of length (8 * ceil(bitlength(M)/8192)) bytes.

L2Hash()

L2-HASH: Second-Layer Hash

private static L2Hash(string $k, string $m) : string

The second-layer rehashes the L1-HASH output using a polynomial hash called POLY. If the L1-HASH output is long, then POLY is called once on a prefix of the L1-HASH output and called using different settings on the remainder. (This two-step hashing of the L1-HASH output is needed only if the message length is greater than 16 megabytes.) Careful implementation of POLY is necessary to avoid a possible timing attack (see Section 6.6 for more information).

Parameters
$k : string

string of length 24 bytes.

$m : string

string of length less than 2^64 bytes.

Return values
string

string of length 16 bytes.

L3Hash()

L3-HASH: Third-Layer Hash

private static L3Hash(string $k1, string $k2, string $m) : string

The output from L2-HASH is 16 bytes long. This final hash function hashes the 16-byte string to a fixed length of 4 bytes.

Parameters
$k1 : string

string of length 64 bytes.

$k2 : string

string of length 4 bytes.

$m : string

string of length 16 bytes.

Return values
string

string of length 4 bytes.

nh()

NH Algorithm

private static nh(string $k, string $m, mixed $length) : string
Parameters
$k : string

string of length 1024 bytes.

$m : string

string with length divisible by 32 bytes.

$length : mixed
Return values
string

string of length 8 bytes.

pdf()

PDF Algorithm

private pdf() : string
Return values
string

string of length taglen bytes.

poly()

POLY Algorithm

private static poly(int $wordbits, BigInteger $maxwordrange, BigInteger $k, string $m) : int
Parameters
$wordbits : int

the integer 64 or 128.

$maxwordrange : BigInteger

positive integer less than 2^wordbits.

$k : BigInteger

integer in the range 0 ... prime(wordbits) - 1.

$m : string

string with length divisible by (wordbits / 8) bytes.

Return values
int

in the range 0 ... prime(wordbits) - 1.

processSHA3Block32()

32-bit block processing method for SHA3

private static processSHA3Block32(array<string|int, mixed> &$s) : mixed
Parameters
$s : array<string|int, mixed>
Tags
access

private

processSHA3Block64()

64-bit block processing method for SHA3

private static processSHA3Block64(array<string|int, mixed> &$s) : mixed
Parameters
$s : array<string|int, mixed>
Tags
access

private

rotateLeft32()

Rotate 32-bit int

private static rotateLeft32(array<string|int, mixed> $x, int $shift) : mixed
Parameters
$x : array<string|int, mixed>
$shift : int
Tags
access

private

rotateLeft64()

Rotate 64-bit int

private static rotateLeft64(int $x, int $shift) : mixed
Parameters
$x : int
$shift : int
Tags
access

private

sha3_32()

Pure-PHP 32-bit implementation of SHA3

private static sha3_32(string $p, int $c, int $r, int $d, int $padType) : mixed

Whereas BigInteger.php's 32-bit engine works on PHP 64-bit this 32-bit implementation of SHA3 will not work on PHP 64-bit. This is because this implementation employees bitwise NOTs and bitwise left shifts. And the round constants only work on 32-bit PHP. eg. dechex(-2147483648) returns 80000000 on 32-bit PHP and FFFFFFFF80000000 on 64-bit PHP. Sure, we could do bitwise ANDs but that would slow things down.

SHA512 requires BigInteger to simulate 64-bit unsigned integers because SHA2 employees addition whereas SHA3 just employees bitwise operators. PHP64 only supports signed 64-bit integers, which complicates addition, whereas that limitation isn't an issue for SHA3.

In https://ws680.nist.gov/publication/get_pdf.cfm?pub_id=919061#page=16 KECCAK[C] is defined as "the KECCAK instance with KECCAK-f[1600] as the underlying permutation and capacity c". This is relevant because, altho the KECCAK standard defines a mode (KECCAK-f[800]) designed for 32-bit machines that mode is incompatible with SHA3

Parameters
$p : string
$c : int
$r : int
$d : int
$padType : int
Tags
access

private

sha3_64()

Pure-PHP 64-bit implementation of SHA3

private static sha3_64(string $p, int $c, int $r, int $d, int $padType) : mixed
Parameters
$p : string
$c : int
$r : int
$d : int
$padType : int
Tags
access

private

sha3_pad()

Pads SHA3 based on the mode

private static sha3_pad(int $padLength, int $padType) : string
Parameters
$padLength : int
$padType : int
Tags
access

private

Return values
string

sha512()

Pure-PHP implementation of SHA512

private static sha512(string $m, array<string|int, mixed> $hash) : string
Parameters
$m : string
$hash : array<string|int, mixed>
Tags
access

private

Return values
string

uhash()

UHASH Algorithm

private uhash(string $m, int $taglen) : string
Parameters
$m : string

string of length less than 2^67 bits.

$taglen : int

the integer 4, 8, 12 or 16.

Return values
string

string of length taglen bytes.


        
On this page

Search results