thrownewDomainException('libsodium is not available');
}
if(!\is_string($key)){
thrownewInvalidArgumentException('key must be a string when using EdDSA');
}
try{
// The last non-empty line is used as the key.
$lines=array_filter(explode("\n",$key));
$key=base64_decode((string)end($lines));
if(\strlen($key)===0){
thrownewDomainException('Key cannot be empty string');
}
returnsodium_crypto_sign_detached($msg,$key);
}catch(Exception$e){
thrownewDomainException($e->getMessage(),0,$e);
}
}
thrownewDomainException('Algorithm not supported');
}
/**
* Verify a signature with the message, key and method. Not all methods
* are symmetric, so we must have a separate verify and sign method.
*
* @param string $msg The original message (header and body)
* @param string $signature The original signature
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate $keyMaterial For HS*, a string key works. for RS*, must be an instance of OpenSSLAsymmetricKey
* @param string $alg The algorithm
*
* @return bool
*
* @throws DomainException Invalid Algorithm, bad key, or OpenSSL failure
*/
privatestaticfunctionverify(
string$msg,
string$signature,
$keyMaterial,
string$alg
):bool{
if(empty(static::$supported_algs[$alg])){
thrownewDomainException('Algorithm not supported');