PHPのお勉強!

PHP TOP

sodium_crypto_sign_detached

(PHP 7 >= 7.2.0, PHP 8)

sodium_crypto_sign_detachedメッセージに署名する

説明

sodium_crypto_sign_detached(string $message, #[\SensitiveParameter] string $secret_key): string

秘密鍵を使って署名します。 この署名は、対応する公開鍵で検証できます。 この関数は、メッセージとは別の署名を返します。

パラメータ

message

署名するメッセージ

secret_key

秘密鍵。 sodium_crypto_sign_secretkey() を参照ください。

戻り値

署名を返します。

add a note

User Contributed Notes 2 notes

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

This is similar to sodium_crypto_sign(), but the returned string does not contain the original message, it is just a signature.

<?php

// $sign_seed = random_bytes(SODIUM_CRYPTO_SIGN_SEEDBYTES);
// $sign_pair = sodium_crypto_sign_seed_keypair($sign_seed);

$sign_pair = sodium_crypto_sign_keypair();
$sign_secret = sodium_crypto_sign_secretkey($sign_pair);
$sign_public = sodium_crypto_sign_publickey($sign_pair);

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

$message = 'Hello';

$signature = sodium_crypto_sign_detached($message, $sign_secret);

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

$message_valid = sodium_crypto_sign_verify_detached($signature, $message, $sign_public);

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

?>
up
0
cb at dialogs dot com
4 years ago
For those(like me) confused by sodium_crypto_sign() vs. sodium_crypto_sign_detached()

From the libsodium docs https://libsodium.gitbook.io/doc/public-key_cryptography/public-key_signatures

The crypto_sign() function prepends a signature to a message "Combined mode"

In "detached mode" the signature is stored without attaching a copy of the original message to it.