ellite/Wallos · error · InvalidArgumentException
Invalid "algorithm" parameter.
Error message
Invalid "algorithm" parameter.
What it means
OTP classes store the HMAC hash algorithm as a URI parameter. getDigest() reads the 'algorithm' parameter and throws this InvalidArgumentException when the stored value is missing, not a string, or an empty string, because a digest cannot be computed otherwise.
Solutions
- Call setParameter('algorithm', 'sha1') (or sha256/sha512) on the OTP object before using it
- If parsing a provisioning URI, ensure the otpauth:// URI contains a non-empty algorithm query parameter
- Check the object's getParameters() output to confirm what 'algorithm' currently holds
Example fix
// before
$totp->setParameter('algorithm', null);
echo $totp->getDigest();
// after
$totp->setParameter('algorithm', 'sha256');
echo $totp->getDigest(); Defensive patterns
Strategy: validation
Validate before calling
$params = $otp->getParameters();
if (!isset($params['algorithm']) || !is_string($params['algorithm']) || $params['algorithm'] === '') {
$otp->setParameter('algorithm', 'sha1');
} Type guard
function hasAlgorithm($otp): bool {
$v = $otp->getParameters()['algorithm'] ?? null;
return is_string($v) && $v !== '';
} Try / catch
try {
$digest = $otp->getDigest();
} catch (\InvalidArgumentException $e) {
$otp->setParameter('algorithm', 'sha1');
$digest = $otp->getDigest();
} Prevention
- Always construct OTPs via factory methods (createFromSecret) that set defaults
- Never assign algorithm via raw array writes; use setParameter
- Check hash_algos() support for non-standard digests
When it happens
Trigger: Calling getDigest() (directly or indirectly via at()/verify()) on an OTP object whose 'algorithm' parameter was never set, was set to null, or was set to a non-string/empty value bypassing the parameter map validation.
Common situations: Constructing a TOTP/HOTP programmatically with setParameter('algorithm', null), loading a provisioning URI whose query string has an empty algorithm= value, or upgrading library versions where the algorithm was previously defaulted silently.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Counter must be at least 0.
- Unsupported " " OTP type
- Invalid "counter" parameter.
- The counter must be at least 0.
- Invalid data.
AI-assisted analysis of ellite/Wallos@52820e87ca (2026-09-13).
Data as JSON: /api/errors/107f249700b3ba15.
Report an issue: GitHub.
Appendix: source
Thrown at libs/OTPHP/ParameterTrait.php:96
}
public function setIssuerIncludedAsParameter(bool $issuer_included_as_parameter): void
{
$this->issuer_included_as_parameter = $issuer_included_as_parameter;
}
public function getDigits(): int
{
$value = $this->getParameter('digits');
(is_int($value) && $value > 0) || throw new InvalidArgumentException('Invalid "digits" parameter.');
return $value;
}
public function getDigest(): string
{
$value = $this->getParameter('algorithm');
(is_string($value) && $value !== '') || throw new InvalidArgumentException('Invalid "algorithm" parameter.');
return $value;
}
public function hasParameter(string $parameter): bool
{
return array_key_exists($parameter, $this->parameters);
}
public function getParameter(string $parameter): mixed
{
if ($this->hasParameter($parameter)) {
return $this->getParameters()[$parameter];
}
throw new InvalidArgumentException(sprintf('Parameter "%s" does not exist', $parameter));
}
View on GitHub (pinned to 52820e87ca)