4

I am attempting to encrypt a string using random bytes and sodium in PHP 7.2.7 for IIS.

$nonce = random_bytes('SODIUM_CRYPTO_SECRETBOX_NONCEBYTES');

and

$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);

both return this error:

Uncaught TypeError: random_bytes() expects parameter 1 to be integer

however this works fine

$key = random_bytes('SODIUM_CRYPTO_SECRETBOX_KEYBYTES');

Google seems to be failing me with this one, I have found no help when searching.

3 Answers 3

4

Updated

Depending on how you have your PHP configured. You may not be accessing the constant correctly.

Please run this script and see if it helps your problem. Let me know what the results are.

$extenstion = FALSE;
$constants = FALSE;

echo '<pre>';

if(in_array('libsodium', get_loaded_extensions())){

  $extenstion = TRUE;
  echo 'Libsodium IS installed.<br>';

}else{

  echo 'Libsodium IS NOT installed.<br>';

  }

$constantsArray = get_defined_constants(true);

if(isset($constantsArray['libsodium']) && $constantsArray['libsodium']){

  $constants = TRUE;

  echo 'You HAVE predefined Libsodium constants.<br>';

}else{

  echo 'You DO NOT have any predefined Libsodium constants.<br>';

}

if($extenstion === TRUE && $constants === TRUE){

  foreach($constantsArray['libsodium'] as $constant=>$value){

    if(preg_match('/CRYPTO_SECRETBOX_NONCEBYTES|CRYPTO_SECRETBOX_KEYBYTES/', $constant)){

      echo 'Your predefined constant\'s name is set as this: ' . $constant . ' To use in your functions: random_bytes(' . $constant . ');' .  '<br>';

    }

  }    

echo 'Here is a full list of your Libsodium constants:<br>';
print_r(get_defined_constants(true)['libsodium']);

}

//***Leave these commented out when you first run the test.***

//To see ALL of your constants uncomment this line:
//print_r(get_defined_constants(true));

//To see ALL of your extensions uncomment this line:
//print_r(get_loaded_extensions());

echo '</pre>';
4
  • This is the response ���c���������5����� E�~A ij�0�_������ Dec 28, 2018 at 18:29
  • @Andrew Lindemulder Please see updated answer. This will give us some insight into your current setup. Let me know.
    – Joseph_J
    Dec 29, 2018 at 6:51
  • @Andrew Lindemulder Any luck?
    – Joseph_J
    Dec 31, 2018 at 4:05
  • 2
    Thank you. get_loaded_extensions() helped me realize I didn't even have php-libsodium installed and get_defined_constants() with get_defined_functions() helped me learn the correct syntax.
    – Mladen B.
    Feb 22, 2021 at 9:03
0

Your second implementation

$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);

Should work if you have the extension installed and properly configured. I would look into that if I were you.

You can check your PHP's configuration using phpinfo() to check if the sodium is enabled.

And, If you haven't installed, install using:

sudo pecl install -f libsodium
1
-1

Excerpt this article:

Sodium (and NaCl) is an “opinionated” library: this means the algorithms used have been selected and cannot be changed. The library uses some of the most robust algorithms available, including Elliptic-curve cryptography (ECC).

And random_bytes() is not used to encrypt messages, this function only generates random bytecodes.

This sample code is to generate keys using Sodium:

$msg = 'This is a super secret message!';

// Generating an encryption key and a nonce
$key   = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES); // 256 bit
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); // 24 bytes

// Encrypt
$ciphertext = sodium_crypto_secretbox($msg, $nonce, $key);
// Decrypt
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);

echo $plaintext === $msg ? 'Success' : 'Error';

See? SODIUM_CRYPTO_SECRETBOX_NONCEBYTES is actually a predefined constant, which is the length of the key. Follow this document for other predefined constants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.