'PHP Sodium not installed?
I'm trying to use Libsodium for the first time to for encryption, but I've encountered an error. I'm running everything on XAMPP; sodium is in the /ext/ folder and I've added extension=sodium to the php.ini file.
This is my code.
// This refers to the previous code block.
require "safeCrypto.php";
// Do this once then store it somehow:
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
$message = 'We are all living in a yellow submarine';
$ciphertext = safeEncrypt($message, $key);
$plaintext = safeDecrypt($ciphertext, $key);
echo $ciphertext;
echo $plaintext;
echo phpversion();
It throws this error.
Warning: Use of undefined constant SODIUM_CRYPTO_SECRETBOX_KEYBYTES - assumed 'SODIUM_CRYPTO_SECRETBOX_KEYBYTES' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\php\crypttest.php on line 6
Fatal error: Uncaught TypeError: random_bytes() expects parameter 1 to be integer, string given in D:\xampp\htdocs\php\crypttest.php:6
get_loaded_extensions() shows that sodium is NOT installed, and I can't figure out why. The php_sodium.dll file exists in the ext folder and I've added extension=sodium in the php.ini file.
I'm using PHP 7.2.10.
Solution 1:[1]
I had the same problem last weekend. I have a fresh installation of Windows 10 to use as my new development machine I also installed JDK with Apache Netbeans 11.1 And a new installation of XAMMP (xampp-windows-x64-7.3.11-0-VC15-installer) After installation I modified the php.ini to uncomment the "extension=sodium" and started to check if sodium was active in php with the command "php -m" in the xampp shell. There I saw that the module of sodium was loaded.
In my test.php I put in the line:
<?php
echo random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
?>
To check if it worked. Sadly I got an error:
Warning: Use of undefined constant SODIUM_CRYPTO_SECRETBOX_KEYBYTES - assumed 'SODIUM_CRYPTO_SECRETBOX_KEYBYTES' (this will throw an Error in a future version of PHP)
I did extended research on this and other forums to find an aswer but did not find one until I tried the method of Jim (https://stackoverflow.com/a/49142981/10509146) He did go back to an earlier version of php, but the answer is in his step 4. Copy libsodium.dll to the "apache/bin" folder. And libsodium worked! I deinstalled Xampp completly and tried it again. With the same result. All you need to do is to copy the libsodium.dll from the "xampp/php" folder to the "xampp/apache/bin" folder and ofcourse uncomment the extension in php.ini and you are good to go.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Johan de Klijn |
