略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: sodium_crypto_auth

2024-12-21

sodium_crypto_auth

(PHP 7 >= 7.2.0, PHP 8)

sodium_crypto_authCompute a tag for the message

说明

sodium_crypto_auth(string $message, string $key): string

Symmetric message authentication via sodium_crypto_auth() provides integrity, but not confidentiality.

Unlike with digital signatures (e.g. sodium_crypto_sign_detached()), any party capable of verifying a message is also capable of authenticating their own messages. (Hence, symmetric authentication.)

参数

message

The message you intend to authenticate

key

Authentication key

返回值

Authentication tag

add a noteadd a note

User Contributed Notes 1 note

up
1
craig at craigfrancis dot co dot uk
3 years ago
Here's a quick example on how to use sodium_crypto_auth(); where you have a message that you want to sign, so anyone who can access the *shared* key can confirm that the message hasn't been tampered with.

This is similar to sodium_crypto_sign_detached(), but both signer and verifier have access to the same key.

<?php

$key
= sodium_crypto_auth_keygen();

//--------------------------------------------------
// Person 1, signing

$message = 'Hello';

$signature = sodium_crypto_auth($message, $key);

//--------------------------------------------------
// Person 2, verifying

$message_valid = sodium_crypto_auth_verify($signature, $message, $key);

if (!
$message_valid) {
    exit(
'Message has been changed.');
}

?>

官方地址:https://www.php.net/manual/en/function.sodium-crypto-auth.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3