Skip to content

Core\Crypto::sign

Signs a message with the private half of a key pair, so that anybody holding the public half can check who wrote it and that it arrived unchanged.

There is no algorithm to choose. The scheme is the kind of the key pair: Ed25519 signs as itself, P256 signs as ECDSA with SHA-256, and the two RSA kinds sign over SHA-256. A call cannot name a scheme the key is not for.

A signature is bytes that travel next to the message, often as text in a header or a field. The other side calls Core\Crypto::verify with the message, the signature and your public key.

Two signatures over one message are not always equal. Ed25519 and RsaPkcs1 give the same signature every time. P256 and RsaPss use fresh random data in every call, so two signatures differ and both are valid.

Good to know: an X25519 pair signs nothing, and this member throws an error for one. That kind is for Core\Crypto::agree.

Parameters

  • $messagebytesThe whole bytes to sign. You cannot pass a hash of the message in its place. A signature over a hash means something different.
  • $keyCore\Crypto\KeyPairThe key pair to sign with. It can be of any kind except X25519, which can only agree.

Return value

Returns bytes. The signature. For P256, it is 64 bytes: the r ‖ s pair that JWS and WebCrypto use, never DER. For Ed25519, it is 64 bytes. For either RSA kind, it has as many bytes as the modulus. RsaPkcs1 and Ed25519 give the same signature for a message every time. P256 and RsaPss use randomness, so two signatures of one message differ, and both verify.

Errors

  • LogicError$key is an X25519 key pair, which cannot sign. The kind is set when the pair is generated or read, so this is a bug in your program.

Examples

Sign a message and check it

<?nvs
// Shows how to sign a message, and how the other side checks it. The signature
// travels next to the message.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
bytes $message = 'ship 2 boxes to Ada' as bytes;
bytes $signature = Core\Crypto::sign($message, $pair);
// An Ed25519 signature is 64 bytes. This prints `the signature is 64 bytes`.
echo 'the signature is ', Core\Bytes::length($signature), ' bytes', "\n";
// The other side needs the message, the signature and your public key.
// `Core\Crypto::verify` returns nothing, and throws an error when the check fails.
Core\Crypto::verify($message, $signature, $pair->publicKey());
echo 'the signature belongs to this key', "\n";
// One changed word in the message makes the check fail.
try {
Core\Crypto::verify('ship 3 boxes to Ada' as bytes, $signature, $pair->publicKey());
echo 'the changed message passed', "\n";
} catch (Throwable $e) {
echo 'the changed message was refused', "\n";
}
Output
the signature is 64 bytes
the signature belongs to this key
the changed message was refused

The kind decides the signature

<?nvs
// Shows what the kind of the key pair decides. You never name an algorithm, so
// the pair you sign with is the whole choice.
bytes $message = 'invoice 4711' as bytes;
// `Ed25519` signs one message the same way every time.
Core\Crypto\KeyPair $ed25519 = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
bytes $first = Core\Crypto::sign($message, $ed25519);
bytes $second = Core\Crypto::sign($message, $ed25519);
echo 'two Ed25519 signatures are equal: ', ($first == $second ? 'yes' : 'no'), "\n";
// `P256` uses fresh random data in every call, so two signatures over one
// message differ. Both of them are valid.
Core\Crypto\KeyPair $p256 = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
bytes $one = Core\Crypto::sign($message, $p256);
bytes $two = Core\Crypto::sign($message, $p256);
echo 'two P256 signatures are equal: ', ($one == $two ? 'yes' : 'no'), "\n";
Core\Crypto::verify($message, $one, $p256->publicKey());
Core\Crypto::verify($message, $two, $p256->publicKey());
echo 'both P256 signatures are valid', "\n";
// Both kinds sign into 64 bytes. This prints 64 and 64.
echo Core\Bytes::length($first), "\n";
echo Core\Bytes::length($one), "\n";
Output
two Ed25519 signatures are equal: yes
two P256 signatures are equal: no
both P256 signatures are valid
64
64

Sign a webhook the receiver can check

<?nvs
// Shows how a program signs what it sends, so the receiver knows the message
// came from this program and arrived unchanged. This is what a webhook sender
// does.
Core\Crypto\KeyPair $sender = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
// The receiver is given this one public key beforehand. A public key is safe to
// publish.
bytes $published = $sender->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);
// What the sender sends: the body, and the signature as text in a header.
string $body = '{"order":1042,"state":"shipped"}';
string $header = Core\Encoding::toHex(Core\Crypto::sign($body as bytes, $sender));
// This is the receiving side. It reads the key it was given...
Core\Crypto\PublicKey $key = Core\Crypto\PublicKey::read(
$published,
Core\Crypto\KeyKind::Ed25519,
Core\Crypto\PublicKey::Format::Spki,
);
// ...and checks the body it just received against the header.
try {
Core\Crypto::verify($body as bytes, Core\Encoding::fromHex($header), $key);
echo 'accepted: ', $body, "\n";
} catch (Throwable $e) {
echo 'refused', "\n";
}
Output
accepted: {"order":1042,"state":"shipped"}