phpcryptographylibsodiumstring-hashing

PHP: Installing Libsodium to PHP v5.5


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.


Solution

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