函式名稱:sodium_crypto_sign_verify_detached()
函式描述:該函式用於驗證簽名是否與訊息和公鑰匹配。
適用版本:PHP 7.2.0 及以上版本。
用法:
bool sodium_crypto_sign_verify_detached(
string $signature,
string $message,
string $publicKey
)
引數:
- $signature: 要驗證的簽名,必須為64位元組的二進位制字串。
- $message: 要驗證的訊息,可以是任意長度的字串。
- $publicKey: 用於驗證簽名的公鑰,必須為32位元組的二進位制字串。
返回值:
- 如果簽名與訊息和公鑰匹配,則返回true;否則返回false。
示例:
$message = "Hello, world!";
$signature = hex2bin("a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2");
$publicKey = hex2bin("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef");
if (sodium_crypto_sign_verify_detached($signature, $message, $publicKey)) {
echo "Signature is valid.";
} else {
echo "Signature is not valid.";
}
注意事項:
- 在使用此函式之前,需要確保已經安裝並啟用了 Sodium 擴充套件。
- 簽名和公鑰必須是二進位制字串,可以使用hex2bin()函式將十六進位制字串轉換為二進位制字串。
- 簽名的長度必須為64位元組,公鑰的長度必須為32位元組。
- 為了確保安全性,公鑰應該是由可靠的金鑰生成函式生成的,並且只有簽名者和驗證者知道該公鑰。
- 此函式用於驗證簽名,不會對訊息進行解密。
熱門工具排行榜