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

- Thread: https://externals.io/message/20166
- Original archive: https://news-web.php.net/php.internals/20166
- Started by Sara Golemon on 2005-11-18T16:43:28+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #20166 — Sara Golemon — 2005-11-18T16:43:28+00:00

URL: https://externals.io/message/20166#20166

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

---

## #20170 — Sara Golemon — 2005-11-18T19:52:48+00:00

URL: https://externals.io/message/20166#20170 · In reply to: #20166

> 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

---

## #20171 — Roman Ivanov — 2005-11-18T20:27:42+00:00

URL: https://externals.io/message/20166#20171 · In reply to: #20166

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]])?

---

## #20173 — Wez Furlong — 2005-11-18T20:55:51+00:00

URL: https://externals.io/message/20166#20173 · In reply to: #20171

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.


> Sara Golemon wrote:
> > […]
>
> Hm... hash(string $value[, $algo='sha256' [,$raw=false]])?

---

## #20179 — Markus Fischer — 2005-11-19T15:13:32+00:00

URL: https://externals.io/message/20166#20179 · In reply to: #20166

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

---

## #20183 — Sara Golemon — 2005-11-19T20:35:34+00:00

URL: https://externals.io/message/20166#20183 · In reply to: #20179

> 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
