Hash

author

Jim Wigginton terrafrost@php.net

author

Andreas Fischer bantu@phpbb.com

package

Default

Methods

Default Constructor.

__construct(string $hash = 'sha256')

Arguments

$hash

string

__toString() magic method

__toString()

Pre-compute the key used by the HMAC

computeKey()

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

Returns the block length (in bits)

getBlockLength(): integer

Response

integer

Returns the block length (in bytes)

getBlockLengthInBytes(): integer

Response

integer

Gets the hash function.

getHash(): string

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

Response

string

Returns the hash length (in bits)

getLength(): integer

Response

integer

Returns the hash length (in bytes)

getLengthInBytes(): integer

Response

integer

Compute the Hash / HMAC / UMAC.

hash(string $text): string

Arguments

$text

string

Response

string

KDF: Key-Derivation Function

kdf(integer $index,integer $numbytes): string

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

Arguments

$index

integer

a non-negative integer less than 2^64

$numbytes

integer

a non-negative integer less than 2^64

Response

string

string of length numbytes bytes

L1-HASH Algorithm

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

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.

Arguments

$k

string

string of length 1024 bytes.

$m

string

string of length less than 2^67 bits.

Response

string

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

L2-HASH: Second-Layer Hash

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

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).

Arguments

$k

string

string of length 24 bytes.

$m

string

string of length less than 2^64 bytes.

Response

string

string of length 16 bytes.

L3-HASH: Third-Layer Hash

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

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.

Arguments

$k1

string

string of length 64 bytes.

$k2

string

string of length 4 bytes.

$m

string

string of length 16 bytes.

Response

string

string of length 4 bytes.

NH Algorithm

nh(string $k,string $m, $length): string
static

Arguments

$k

string

string of length 1024 bytes.

$m

string

string with length divisible by 32 bytes.

$length

Response

string

string of length 8 bytes.

PDF Algorithm

pdf(): string

Response

string

string of length taglen bytes.

POLY Algorithm

poly(integer $wordbits,\phpseclib3\Math\BigInteger $maxwordrange,\phpseclib3\Math\BigInteger $k,string $m): integer
static

Arguments

$wordbits

integer

the integer 64 or 128.

$maxwordrange

\phpseclib3\Math\BigInteger

positive integer less than 2^wordbits.

$k

\phpseclib3\Math\BigInteger

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

$m

string

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

Response

integer

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

32-bit block processing method for SHA3

processSHA3Block32(array &$s)
static

Arguments

$s

array

64-bit block processing method for SHA3

processSHA3Block64(array &$s)
static

Arguments

$s

array

Rotate 32-bit int

rotateLeft32(array $x,integer $shift)
static

Arguments

$x

array

$shift

integer

Rotate 64-bit int

rotateLeft64(integer $x,integer $shift)
static

Arguments

$x

integer

$shift

integer

Sets the hash function.

setHash(string $hash)

Arguments

$hash

string

Sets the key for HMACs

setKey(string $key = false)

Keys can be of any length.

Arguments

$key

string

Sets the nonce for UMACs

setNonce(string $nonce = false)

Keys can be of any length.

Arguments

$nonce

string

Pure-PHP 32-bit implementation of SHA3

sha3_32(string $p,integer $c,integer $r,integer $d,integer $padType)
static

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

Arguments

$p

string

$c

integer

$r

integer

$d

integer

$padType

integer

Pure-PHP 64-bit implementation of SHA3

sha3_64(string $p,integer $c,integer $r,integer $d,integer $padType)
static

Arguments

$p

string

$c

integer

$r

integer

$d

integer

$padType

integer

Pads SHA3 based on the mode

sha3_pad(integer $padLength,integer $padType): string
static

Arguments

$padLength

integer

$padType

integer

Response

string

Pure-PHP implementation of SHA512

sha512(string $m,array $hash): string
static

Arguments

$m

string

$hash

array

Response

string

UHASH Algorithm

uhash(string $m,integer $taglen): string

Arguments

$m

string

string of length less than 2^67 bits.

$taglen

integer

the integer 4, 8, 12 or 16.

Response

string

string of length taglen bytes.

Constants

Padding Types

PADDING_KECCAK

Padding Types

PADDING_SHA3

Padding Types

PADDING_SHAKE

Properties

Padding Type

paddingType :integer

Only used by SHA3

var

Type(s)

integer

Hash Parameter

hashParam :integer
see
var

Type(s)

integer

Byte-length of hash output (Internal HMAC)

length :integer
see
var

Type(s)

integer

Hash Algorithm

algo :string
see
var

Type(s)

string

Key

key :string
see
var

Type(s)

string

Nonce

nonce :string
see
var

Type(s)

string

Hash Parameters

parameters :array
var

Type(s)

array

Computed Key

computedKey :string
see
var

Type(s)

string

Outer XOR (Internal HMAC)

opad :string

Used only for sha512/*

see
var

Type(s)

string

Inner XOR (Internal HMAC)

ipad :string

Used only for sha512/*

see
var

Type(s)

string

Recompute AES Key

recomputeAESKey :boolean

Used only for umac

see
var

Type(s)

boolean

umac cipher object

c :\phpseclib3\Crypt\AES
see
var

Type(s)

\phpseclib3\Crypt\AES

umac pad

pad :string
see
var

Type(s)

string

Block Size

blockSize :integer
var

Type(s)

integer

factory36

factory36 :
static

Type(s)

factory64

factory64 :
static

Type(s)

factory128

factory128 :
static

Type(s)

offset64

offset64 :
static

Type(s)

offset128

offset128 :
static

Type(s)

marker64

marker64 :
static

Type(s)

marker128

marker128 :
static

Type(s)

maxwordrange64

maxwordrange64 :
static

Type(s)

maxwordrange128

maxwordrange128 :
static

Type(s)