5

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 ...

6
  • Instead try "sha2".
    – U2EF1
    Nov 29, 2015 at 1:52
  • That's odd, it probably should work: 3v4l.org/ICYr5
    – mystery
    Nov 29, 2015 at 1:56
  • 5
    That said, you probably shouldn't hash passwords with SHA256. Use password_hash.
    – mystery
    Nov 29, 2015 at 1:57
  • 1
    Check the array you get back from hash_algos and see if it is there.
    – user1531971
    Nov 29, 2015 at 1:58
  • sha2 does'nt work. sha512 works. Andrea, thanks for the tip (password_hash), but I can't use it now.
    – c-toesca
    Nov 29, 2015 at 2:04

5 Answers 5

2

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.

2

What it boiled down to for me was that due to a forgotten comment in configs I was using mod_php with mpm_event apache. So non-thread-safe php7 in a threaded environment, hence shared memory corruption and a number of problems, one of them being sha1 was unavailable after some random uptime.

1

1)Open localhost/phpmyadmin or 127.0.0.1/phpmyadmin which certainly gives you an error.

2).To fix this, restart the server to start the service of PHPMYADMIN.

3).if you are on local server, open your xampp or wampp, stop all services then start it.

4).This will fix your all issues for sure

1
  • 1
    [reason for downvote] phpmyadmin has nothing to do with the question. It's about hashing, not database management.
    – Hans Vn
    Mar 13, 2021 at 15:23
0

I had the same issue today. It resolved itself after a reboot

0

just needed to Restart xampp or MAMP to fix the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.