3

I have installed libsodium and libsodium-php on ubuntu 16.04 but when I run:

`<?php
var_dump([
    \Sodium\library_version_major(),
    \Sodium\library_version_minor(),
    \Sodium\version_string()
]);`

I get an error saying:

PHP Fatal error:  Uncaught Error: Call to undefined function Sodium\library_version_major() 

According to phpinfo() Sodium is enabled and the compiled version is 2.0.1 and the library version is 1.0.13. What am I doing wrong?

T. Doe
  • 43
  • 1
  • 4

3 Answers3

5

The PHP API for libsodium has changed in version 2.0.0 of the extension.

Originally, all functions were in a \Sodium\ namespace.

However, following a vote by PHP developers regarding its inclusion in PHP 7.2, it was decided to move everything to the global namespace instead.

So, what used to be \Sodium\library_version_major() is now sodium_library_version_major().

Frank Denis
  • 1,475
  • 9
  • 12
  • 4
    Replacing `\Sodium\library_version_major()` with `sodium_library_version_major()` results in the same error. – T. Doe Jul 16 '17 at 07:39
  • 1
    Bad example since that one was replaced with the SODIUM_LIBRARY_MAJOR_VERSION constant. But actual functions now have a sodium_ prefix. – Frank Denis Jul 16 '17 at 07:44
  • I also got problems with it, thanks for the answer. Is there an official documentation for those new functions? Apparently they change more function names, since I cannot access ```sodium_crypto_pwhash_scryptsalsa208sha256_str()```. On the libsodium website I can see only outdated examples. – undefinedman Aug 26 '17 at 19:59
  • `crypto_pwhash_scryptsalsa208sha256_str()` doesn't exist any more. It doesn't exist any longer in minimal builds of libsodium either, since there has been a high-level `crypto_pwhash()` API for a long time now. So, use `sodium_crypto_pwhash()`, `sodium_crypto_pwhash_str()` and `sodium_crypto_pwhash_str_verify()`. – Frank Denis Aug 26 '17 at 20:11
  • 1
    I did the same thing, and it's enabled in phpinfo(). But `sodium_library_version_major()` still get the same error. – AFwcxx Sep 14 '17 at 08:08
  • Cool. any idea why it is working but in a bit peculiar way, here: https://stackoverflow.com/q/46215938/2037746 – AFwcxx Sep 14 '17 at 09:50
  • This answer could also help debug which specific syntax you should use, depending on the php/sodium version installed: https://stackoverflow.com/questions/53956155/php-random-bytes-with-sodium-integer-error – Mladen B. Feb 22 '21 at 09:06
3

For those who installed Pecl Version of Soidum and Enabled it in php.ini (extension=sodium.so) And Have same error like Call to Undefined ...

After Restarting Apache & nginx and lack of success finally Reboot server get sodium work exteremly.

PHP 7.3 & >7.3 = libsodium 2.1

Hope to be helpful.

Behnam Alavi
  • 127
  • 1
  • 4
3

For those who couldnt get the answer working.. thats because it should be:

<?php
var_dump([
    SODIUM_LIBRARY_MAJOR_VERSION,
    SODIUM_LIBRARY_MINOR_VERSION,
    SODIUM_LIBRARY_VERSION
]);
Fimlore
  • 147
  • 1
  • 9