Search code examples
phpphp-extensionlibsodium

Including Paragonie Halite in project doesn't find variables and functions


I've installed libsodium on Windows for PHP 7 and I'm developing my project with PHPStorm. I've also installed Halite from Paragonie which couldn't even be installable if the libsodium extension were not installed correctly. Also the IDE finds the used functions and when clicking on the variables and so on it opens up the fitting files of libsodium.

But unfortunately in my setup I get the following error:

Uncaught Error: Undefined constant 'Sodium\CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE' in C:\Server\nginx-1.11.1\html\mywebsite\vendor\paragonie\halite\src\KeyFactory.php:344

My index.php file looks like the following:

    <?php
    require_once 'vendor/autoload.php';

    spl_autoload_register(function ($class) {
        require_once $class . '.php';
    });

    $router = new AltoRouter();

    $router->setBasePath('/' . basename(__DIR__));

    require_once 'config/routes.php';

    $match = $router->match();

    $GLOBALS['db'] = new \config\database(true, null);

    try
    {
        if ($match) {
            $routeController = new Modules\Core\RouteController($match);
            echo $routeController->bootstrap();
        }
    }
    catch (Exception $ex)
    {
        //@todo log every Exception
    }

I'm also submitting my form with Jquery which leads successfully to the execution of the following function (which doesn't find the libsodium functions/variables):

public function hash($password)
{
    $this->setEncryptionKey();
    return Password::hash(
        $password, // string
        $this->encryption_key      // \ParagonIE\Halite\Symmetric\EncryptionKey
    );
}

public function check($stored_hash, $password)
{
    try {
        if (Password::verify(
            $password, // string
            $stored_hash,
            $this->encryption_key
        )) {
            return true;
        }
    } catch (\ParagonIE\Halite\Alerts\InvalidMessage $ex) {
        // Handle an invalid message here. This usually means tampered ciphertext.
        throw new \Exception($ex);
    }
}

I've installed the php_libsodium.dll in the extension directory and put the libsodium.dll in in the directory of the php server. Just for trying out i put it later also in system32 directory and in Syswow64 directory - both didn't change anything.

I just installed the halite library from paragonie with composer but using it is unfortunately harder than I thought. I also tried to use the libsodium extension without Halite but that lead to similar errors that the needed classes, constants and functions and so on were not found.

I've also tried to change my autoloader, but the strange thing anyway is that the IDE finds everything without problems but executing the script in the browser just don't.


Solution

  • In order to use Halite 2.x, you need to have:

    • Libsodium 1.0.9+ (preferably 1.0.10 if you have trouble compiling .9)
    • PECL Libsodium 1.0.3+ (preferably 1.0.6) compiled against libsodium 1.0.9+.

    The easy way to verify that this is set up is:

    <?php
    use \ParagonIE\Halite\Halite;
    
    var_dump(Halite::isLibsodiumSetupCorrectly());
    

    If you want a little more specific information on what's failing, just run this instead:

    <?php
    use \ParagonIE\Halite\Halite;
    
    // TRUE enables verbose output:
    Halite::isLibsodiumSetupCorrectly(true);
    

    Most likely, you'll need to uninstall/reinstall the PHP extension compiled against a newer version of the shared library.

    There's an RFC under discussion for PHP 7.1 right now that will make this painless in the future, if accepted. The RFC passed, years later; libsodium will be in PHP 7.2.