In PHP7, when I hash a string like this:
$hash = hash("sha256", "password");
I have this warning:
Unknown hashing algorithm: sha256
In PHP 5.2.9, it was working. Is the sha256 deprecated in php7? Other idea?
Notes
print_r( hash_algos() );
[0] => md2 [1] => md4 [2] => md5 [3] => sha1 [4] => sha224 [5] => sha256
[6] => sha384 [7] => sha512 ...
I know that you got it to work, but I wanted to add that I've looked through the PHP7 source code and there's simply no reason why it shouldn't work every time.
In ./ext/hash/hash.c
, we 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()
is 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
? That's 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)
};
By looking at the code in this file, you can also see that there are no preventative conditions in PHP_SHA256Init()
, PHP_SHA256Update()
, or PHP_SHA256Final()
. I can't find a single possible way that sha256
could be disabled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With