0

Greetings members of the programming technorati. I come seeking guidance on how to generate a sha3-512 hash by using the hash_algos method, since I noticed that it can generate hashes for many of the supported hash algorithms in php 7.xx. I wish to create a simple script using hash_algos to generate a hash with a salt. Here is the code:

  <?php 
  $data = 453570 ;

   $new_hash = hash_algos(sha-512);

  $sha = hash($new_hash, $data);

  echo $sha;



  //this will generate hashes and lengths of the hashes

    foreach (hash_algos() as $v) {
    $r = hash($v, $data, false);
    printf("%-12s %3d %s\n<br>", $v, strlen($r), $r);
    }


    ?>

Thanks in advance,

Batoe

  • 1
    `hash_algos()` doesn't take a parameter and just returns an array of supported hash types. Pick the one you want and use `hash()`. I don't really see what your question is here. –  Nov 16 '20 at 21:08
  • `SHA-512` is invalid, and `SHA512` is still SHA2. Use `SHA3-512`. – Sammitch Nov 16 '20 at 21:18
  • Ok, the problem I am encountering is that you can easily generate a hash with sha1, $access_hash = sha1($access.$salt); this is not the case with sha3. I guess since it is a new implementation, I can't just substitute the sha1 with sha3-512. I kept getting an fatal error "Fatal error: Uncaught Error: Call to undefined function sha3_256() ". Please show me the correct Syntax. thanks – Tony Starke Nov 16 '20 at 21:19
  • Did you look at the dup? – user3783243 Nov 16 '20 at 23:23

1 Answers1

0

PHP includes dedicated functions for a limited range of hash types, so

$h = sha1('secret');

will return the SHA1 hash. This is similar to

$h = hash('sha1', 'secret');

However, for many new hash functions, no explicit function has been included. Instead, all the available hash functions are incorporated into hash().

You can find out which hash types are supported with hash_algos(), which returns a array of available hash types. You want to generate an SHA3-512 hash, so check that list for the hash type you need. It's supported in PHP 7.1+.

Once you've verified that the hash type is supported, use hash() to generate the hash:

$h = hash('sha3-512', 'secret');