20

I am searching for any crypto library that provides SHA-512 hash. Googling around I found some but SHA-512 is absent.

Please suggest.

Charles
  • 50,943
  • 13
  • 104
  • 142
RKh
  • 13,818
  • 46
  • 152
  • 265

2 Answers2

37

If you are using PHP >= 5.3, the function openssl_digest should do the trick :

echo openssl_digest('glop', 'sha512');

gives me this output (splitted in two lines to get better readibility) :

416b1861951170e1f6eb6543b0dd3d4f1994ce8da7cd82061513d3ddd1dd81111
f4ada5caf6421f1d17425c6f29bdb4a95cf84df9eda4164f5a762acbb490a68

(And you can use openssl_get_md_methods to get the list of available digest methods)


And with PHP 5.1 or 5.2, you have the hash function :

echo hash('sha512', 'glop');

gives me the same output (splitted, too) :

416b1861951170e1f6eb6543b0dd3d4f1994ce8da7cd82061513d3ddd1dd81111
f4ada5caf6421f1d17425c6f29bdb4a95cf84df9eda4164f5a762acbb490a68

And, here, to know the list of available digest methods, you can use hash_algos

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 5
    what is the difference between `openssl_digest` and `hash` in php >= 5.3? – Markus Feb 07 '11 at 20:24
  • 2
    -1 for not explaining why `openssl_digest` should be preferred over `hash`. – o0'. Oct 25 '11 at 10:18
  • 2
    well can anyone explain if and why to use openssl_digest instead of hash? – Rob Jun 16 '12 at 14:59
  • They're the same. Also, check out this question for a good read: http://stackoverflow.com/questions/8952807/openssl-digest-vs-hash-vs-hash-hmac-difference-between-salt-hmac – Joel Mellon Oct 31 '12 at 15:38
16

In PHP 5 >= 5.1.2, PECL hash >= 1.1:

hash('sha512', someStr);

See hash() for more information.
To see all hash algorithms available to you, try:

print_r(hash_algos());
Nick Presta
  • 28,134
  • 6
  • 57
  • 76