Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP7: Unknown hashing algorithm: sha256

Tags:

php-7

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

  • the extension "php_openssl.dll" is enabled.
  • hash("sha512", "password"); // WORKS !
  • print_r( hash_algos() );

    [0] => md2 [1] => md4 [2] => md5 [3] => sha1 [4] => sha224 [5] => sha256 [6] => sha384 [7] => sha512 ...

like image 960
c-toesca Avatar asked Nov 29 '15 01:11

c-toesca


1 Answers

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.

like image 68
Will Avatar answered Oct 29 '22 14:10

Will