Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sodium_crypto_pwhash_str() Throws ValueError For Out-Of-Range Limits

The password-hashing functions sodium_crypto_pwhash(), sodium_crypto_pwhash_str(), sodium_crypto_pwhash_scryptsalsa208sha256() and sodium_crypto_pwhash_scryptsalsa208sha256_str() validate their $opslimit and $memlimit arguments against libsodium’s documented minimums. Until PHP 8.6, an out-of-range value threw a SodiumException. In PHP 8.6, it throws a ValueError instead, which better reflects that the problem is an invalid argument rather than an internal libsodium failure. SodiumException is still thrown for genuine libsodium failures.

PHP code

<?php

try {
    $hash = sodium_crypto_pwhash_str('password', 1, 1);
    var_dump($hash);
} catch (\ValueError $e) {
    echo "ValueError: ".$e->getMessage()."\n";
} catch (\SodiumException $e) {
    echo "SodiumException: ".$e->getMessage()."\n";
}

?>

Before

SodiumException: sodium_crypto_pwhash_str(): Argument #3 ($memlimit) must be greater than or equal to 8192

After

ValueError: sodium_crypto_pwhash_str(): Argument #3 ($memlimit) must be greater than or equal to 8192

PHP version change

This behavior changed in 8.6.

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat