Oihana PHP Enums

HashAlgorithm uses ConstantsTrait

Enumeration of hash algorithm identifiers recognised by PHP's `hash()`, `hash_hmac()`, `hash_pbkdf2()` and `hash_hkdf()` families of functions.

Values correspond to the lowercase algorithm names returned by hash_algos() on a standard PHP 8.4+ build. Keeping them in a single enum avoids magic strings like 'sha256' sprinkled through callers (token hashing, file integrity checks, HMAC signing, etc.).

Usage:

$digest = hash( HashAlgorithm::SHA256 , $clearCode ) ;
$hmac   = hash_hmac( HashAlgorithm::SHA512 , $payload , $secret ) ;
HashAlgorithm::validate( 'sha256' ) ;

Notes:

  • Constant names follow the historic "family + bit length" convention (SHA256, SHA3_512). Dashes and slashes from the raw PHP names are normalised to underscores so the names stay valid PHP identifiers.
  • Deprecated algorithms (MD5, SHA1) are included for interop but should not be used for new security-sensitive code.
  • Non-cryptographic algorithms (CRC32, ADLER32, FNV) are grouped at the bottom and clearly labelled — useful for checksums, not secrets.
Tags
author

Marc Alcaraz

since
1.1.0

Table of Contents

Constants

ADLER32  : string = 'adler32'
CRC32  : string = 'crc32'
CRC32B  : string = 'crc32b'
CRC32C  : string = 'crc32c'
FNV132  : string = 'fnv132'
FNV164  : string = 'fnv164'
FNV1A32  : string = 'fnv1a32'
FNV1A64  : string = 'fnv1a64'
GOST  : string = 'gost'
GOST_CRYPTO  : string = 'gost-crypto'
HAVAL128_3  : string = 'haval128,3'
HAVAL128_4  : string = 'haval128,4'
HAVAL128_5  : string = 'haval128,5'
HAVAL160_3  : string = 'haval160,3'
HAVAL160_4  : string = 'haval160,4'
HAVAL160_5  : string = 'haval160,5'
HAVAL192_3  : string = 'haval192,3'
HAVAL192_4  : string = 'haval192,4'
HAVAL192_5  : string = 'haval192,5'
HAVAL224_3  : string = 'haval224,3'
HAVAL224_4  : string = 'haval224,4'
HAVAL224_5  : string = 'haval224,5'
HAVAL256_3  : string = 'haval256,3'
HAVAL256_4  : string = 'haval256,4'
HAVAL256_5  : string = 'haval256,5'
JOAAT  : string = 'joaat'
MD2  : string = 'md2'
MD4  : string = 'md4'
MD5  : string = 'md5'
MURMUR3A  : string = 'murmur3a'
MURMUR3C  : string = 'murmur3c'
MURMUR3F  : string = 'murmur3f'
RIPEMD128  : string = 'ripemd128'
RIPEMD160  : string = 'ripemd160'
RIPEMD256  : string = 'ripemd256'
RIPEMD320  : string = 'ripemd320'
SHA1  : string = 'sha1'
SHA224  : string = 'sha224'
SHA256  : string = 'sha256'
SHA384  : string = 'sha384'
SHA3_224  : string = 'sha3-224'
SHA3_256  : string = 'sha3-256'
SHA3_384  : string = 'sha3-384'
SHA3_512  : string = 'sha3-512'
SHA512  : string = 'sha512'
SHA512_224  : string = 'sha512/224'
SHA512_256  : string = 'sha512/256'
SNEFRU  : string = 'snefru'
SNEFRU256  : string = 'snefru256'
TIGER128_3  : string = 'tiger128,3'
TIGER128_4  : string = 'tiger128,4'
TIGER160_3  : string = 'tiger160,3'
TIGER160_4  : string = 'tiger160,4'
TIGER192_3  : string = 'tiger192,3'
TIGER192_4  : string = 'tiger192,4'
WHIRLPOOL  : string = 'whirlpool'
XXH128  : string = 'xxh128'
XXH3  : string = 'xxh3'
XXH32  : string = 'xxh32'
XXH64  : string = 'xxh64'

Methods

ensureAvailable()  : void
Ensures an algorithm is supported by the runtime, throwing otherwise.
isAvailable()  : bool
Returns true when the given algorithm identifier is both defined on this enum AND supported by the runtime.
supported()  : array<int, string>
Returns the list of algorithms actually supported by the current PHP runtime (intersection of {@see self::enums()} and {@see hash_algos()}).

Constants

Methods

ensureAvailable()

Ensures an algorithm is supported by the runtime, throwing otherwise.

public static ensureAvailable(string $algorithm[, array<int, string>|null $available = null ]) : void
Parameters
$algorithm : string

The algorithm identifier to validate.

$available : array<int, string>|null = null

The list of algorithms supported by the runtime. Defaults to hash_algos(); injectable to test the runtime guard.

Tags
throws
ConstantException

If the algorithm is unknown to this enum or disabled at runtime.

isAvailable()

Returns true when the given algorithm identifier is both defined on this enum AND supported by the runtime.

public static isAvailable(string $algorithm) : bool
Parameters
$algorithm : string

The algorithm identifier to check.

Return values
bool

supported()

Returns the list of algorithms actually supported by the current PHP runtime (intersection of {@see self::enums()} and {@see hash_algos()}).

public static supported() : array<int, string>
Return values
array<int, string>
On this page

Search results