0

How to correctly install Libsodium with PHP verson 5.5. I'm trying to follow the instruction on https://paragonie.com/book/pecl-libsodium/read/00-intro.md#installing-libsodium

Here are the steps I did:

  1. Go to http://windows.php.net/downloads/pecl/releases/libsodium/1.0.6/

  2. Download "php_libsodium-1.0.6-5.5-nts-vc11-x64.zip" and extract files.

  3. Copy "libsodium.dll" in my directory "C:\Program Files (x86)\PHP\v5.5" where is "php.exe"

  4. Copy "php_libsodium.dll" in my directory "C:\Program Files (x86)\PHP\v5.5\ext"

  5. Enable "extension=php_libsodium.dll" in php.ini file

  6. Restart the server

But when I tested it by writing a simple PHP test file:

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

// hash the password and return an ASCII string suitable for storage
$hash_str = sodium_crypto_pwhash_str(
        "mypassword",
        SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
        SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
        );

echo "hash: " . $hash_str;
?>

The result page shows the error:

Fatal error: Call to undefined function sodium_crypto_pwhash_str() in C:\PHP\testLibsodium.php on line 7

The library Libsodium seems not installed because it doesn't know the function. What things I need to do to install PHP Libsodium in PHP version 5.5? Thank you very much.

Update: I've installed the X86 version as advised by @iann and run this code:

$storeInDatabase = \Sodium\crypto_pwhash_str(
        "safasfdwr32sfdfas234",
        SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
        SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
        );

Now seems the function is being read but I'm getting an error:

Notice: Use of undefined constant SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE - assumed 'SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE'

Notice: Use of undefined constant SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE - assumed 'SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE'

Warning: Sodium\crypto_pwhash_str() expects parameter 2 to be long

Catchable fatal error: crypto_pwhash_str(): invalid parameters

Does this mean that my libsodium is installed correctly, but why I'm getting an error? Thank you again.

Jemru
  • 2,091
  • 16
  • 39
  • 52

1 Answers1

2

The sodium_* functions weren't in the global namespace until the library was moved into native PHP, in version 7.2. From the manual:

In PHP before 7.2 with libsodium from PECL, the functions below were defined in the Sodium name space. In PHP 7.2, the namespaces were dropped in favor of a sodium_ prefix (to conform to the PHP internal development standards).

So if you're installing from PECL, you need to use the previous function names. In your case:

$hash_str = \Sodium\crypto_pwhash_str(
...
iainn
  • 16,826
  • 9
  • 33
  • 40
  • Hi @iainn. Thank you it is helpful however I tried it but the same error: $hash_str = \Sodium\crypto_pwhash_str( "mypassword", SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE ); – Jemru Feb 13 '18 at 10:58
  • do I still need to install libsodium aside from PECL? Is it still not working. – Jemru Feb 14 '18 at 06:33
  • @Jemru If the function still isn't registered under the other name, it sounds like the extension isn't loaded at all. Bit of a guess, but you mention that PHP is installed in **Program Files (x86)**, but that you've downloaded an extension labelled **x64**. You might be mixing 32 bit PHP with a 64 bit extension. Try [this one](http://windows.php.net/downloads/pecl/releases/libsodium/1.0.6/php_libsodium-1.0.6-5.5-nts-vc11-x86.zip) – iainn Feb 14 '18 at 09:34
  • Hi @iainn thank you i think the libsodium is installed. However I'm not sure why I'm getting an error on the function when I run an example above? Do I still need to install anything aside from "php_libsodium-1.0.6-5.5-nts-vc11-x86.zip"? – Jemru Feb 15 '18 at 06:18
  • 1
    Easy mode: Add https://github.com/paragonie/sodium_compat via Composer and then the undefined constant issues will disappear. – Scott Arciszewski Oct 28 '18 at 21:18