I know that you got it to work, but I wanted to add that I looked at the source code of PHP7, and there is simply no reason why it should not work every time.
In ./ext/hash/hash.cwe define our table of available hashes:
PHP_MINIT_FUNCTION(hash)
{
zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
php_hash_register_algo("sha224", &php_hash_sha224_ops);
php_hash_register_algo("sha256", &php_hash_sha256_ops);
php_hash_register_algo("sha384", &php_hash_sha384_ops);
php_hash_register_algo("sha512", &php_hash_sha512_ops);
}
php_hash_register_algo() also very simple:
PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops)
{
size_t algo_len = strlen(algo);
char *lower = zend_str_tolower_dup(algo, algo_len);
zend_hash_str_add_ptr(&php_hash_hashtable, lower, algo_len, (void *) ops);
efree(lower);
}
So what of php_hash_sha256_ops? This is defined in ./ext/hash/hash_sha.c:
const php_hash_ops php_hash_sha256_ops = {
(php_hash_init_func_t) PHP_SHA256Init,
(php_hash_update_func_t) PHP_SHA256Update,
(php_hash_final_func_t) PHP_SHA256Final,
(php_hash_copy_func_t) php_hash_copy,
32,
64,
sizeof(PHP_SHA256_CTX)
};
, , PHP_SHA256Init(), PHP_SHA256Update() PHP_SHA256Final() . , sha256 .