sha256(), sha256_file(), and other hashing algos

php.internals

Sara Golemon

20 years ago
Following an offline discussion with some internals folks, I've put together a general hashing extensions which could potentially find a place in core as an enabled by default extension: ext/hash: string hash(string $algo, string $value[,$raw=false]) string hash_file(string $algo, string $filename[,$raw=false]) Where $algo is (currently) any of 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160' each of which are implemented internally using raw math and no library dependencies. I've also got an upgrade path laid out for extracting md5() and sha1() from core in future versions (6.0?) without compromising backward compatability. The extension also supports incremental hashing: resource hash_init($algo); bool hash_update($context, $data); string hash_final($context[,$raw=false]) External extensions can already "register" additional hashing algos using the module dependency hooks now available. For the future I've got plans for allowing registration of userspace hashing functions, HMAC support, and a streams filter implementation as well as additional engines: crc, haval, md4, etc... Examples: $digest = hash('sha256', 'abc'); $ctx = hash_init('sha256'); hash_update($ctx, 'a'); hash_update($ctx, 'bc'); $digest = hash_final($ctx); I'm just putting on some finishing touches and will post a tarball later today. -Sara

Sara Golemon

20 years ago
> I'm just putting on some finishing touches and will post a tarball later > today. >
Meh... It'll be easier to discuss within CVS... It's in pecl/hash as of now. -Sara

Roman Ivanov

20 years ago
Sara Golemon wrote:
> string hash(string $algo, string $value[,$raw=false]) > string hash_file(string $algo, string $filename[,$raw=false])
Hm... hash(string $value[, $algo='sha256' [,$raw=false]])?

Wez Furlong

20 years ago
Assuming that sha256 is always going to be the best default is pretty short-sighted. It's only a matter of time before that one is partially broken in some way too; we won't be able to change the default value of the function by then, because everyone using the default will have code that depends on sha256, and voila, we've gotten locked into something that might not be so desirable. Default values should be avoided unless they're *always* going to make sense. --Wez. On 11/18/05, Roman Ivanov <gamblergluck@yahoo.com> wrote:

Markus Fischer

20 years ago
Hi, I'm missing the functionality to get a list of registered hash functions so one can probe whether a certain hash function is available or not. - Markus

Sara Golemon

20 years ago
> I'm missing the functionality to get a list of registered hash functions > so one can probe whether a certain hash function is available or not. >
There is now :) array hash_algos(void) Returns numerically indexed array of hashing engine names. And just incase anyone missed it. The proto for hash_init() has been extended: resource hash_init(string algo[, int options, string key]) Options currently has one possible value: HASH_HMAC When specified, key must be specified as well (to be used for the HMAC key). HMAC can be used with any of the hashing algos included in ext/hash. Examples of using HMAC with official test vectors can be found in tests/hmac-md5.phpt I'd rather not complicate the proto for the core hash() function with HMAC and other options, but maybe a separate hash_hmac() and/or hash_hmac_file() might be in order? -Sara