sodium_crypto_sign_open()
验证由 sodium_crypto_sign_detached() 函数生成的数字签名,并返回原始消息
函数:sodium_crypto_sign_open()
适用版本:PHP 7.2.0 及以上
用法:sodium_crypto_sign_open() 函数用于验证由 sodium_crypto_sign_detached() 函数生成的数字签名,并返回原始消息。
语法:sodium_crypto_sign_open(string $signature, string $public_key) : string|bool
参数:
$signature: 必需,一个已签名的消息,作为二进制字符串。
$public_key: 必需,验证签名所使用的公钥,作为二进制字符串。
返回值:
如果验证成功,则返回原始消息(作为二进制字符串)。
如果验证失败,则返回 false。
示例:
// 公钥和签名的二进制字符串
$publicKey = hex2bin('4a9d9b9d9e8e54d1e2a3f4a9d9b9d9e8e54d1e2a3f4a9d9b9d9e8e54d1e2a3f');
$signature = hex2bin('4a9d9b9d9e8e54d1e2a3f4a9d9b9d9e8e54d1e2a3f4a9d9b9d9e8e54d1e2a3f');
// 需要验证的消息
$message = "Hello, world!";
// 验证签名并返回原始消息
$originalMessage = sodium_crypto_sign_open($signature, $publicKey);
if ($originalMessage !== false) {
echo "消息验证成功:". $originalMessage;
} else {
echo "消息验证失败!";
}
注意事项:
在使用 sodium_crypto_sign_open() 函数之前,必须先加载 sodium 扩展。
$public_key 必须是由 sodium_crypto_sign_publickey_from_secretkey() 函数生成的公钥。
$signature 必须是由 sodium_crypto_sign_detached() 函数生成的签名。