xp-forge / hashing
Hashing library
Installs: 8 145
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=7.0.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0 | ^9.0 | ^8.0 | ^7.0
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
Fluent interface to hashing functions provided by PHP, extended with Murmur3.
Examples
Calculate hash for a string, output using base32:
use text\hash\Hashing; $hash= Hashing::murmur3_32()->new($seed= 0x2a); $base32= $hash->digest('The quick brown fox jumps over the lazy dog.')->base32();
Incrementally updating hash, output hex (much like the builtin md5()
function does):
use text\hash\Hashing; $hash= Hashing::md5()->new(); while ($stream->available()) { $hash->update($stream->read()); } $hex= $hash->digest()->hex();
Comparing hashes using constant time comparison:
use text\hash\{Hashing, HashCode}; $computed= Hashing::sha256()->digest($req->param('password')); // From request $stored= HashCode::fromHex($record['password_hash']); // From database if ($computed->equals($stored)) { // Not susceptible to timing attacks }
Algorithms
The following algorithms exist as shortcuts inside the entry point class:
Hashing::md5()
Hashing::sha1()
Hashing::sha256()
Hashing::sha512()
Hashing::murmur3_32()
Hashing::murmur3_128()
Other algorithms can be instantiated via Hashing::algorithm(string $name, var... $args)
, which may raise an IllegalArgumentException if the given algorithm is not available.