Core\Crypto\KeyPair::publicKey
$keyPair->publicKey(): Core\Crypto\PublicKeyGives you the half of this key pair that you send to other people.
A key pair has a private half and a public half. The private half stays inside your program. It signs messages with Core\Crypto::sign, and it agrees a shared secret with Core\Crypto::agree. The public half is the one you publish. Anybody who has it can check your signatures, or agree that same shared secret with you.
The public half is worked out from the private key each time you ask for it. It is not stored beside the key. A pair can therefore never give you a public key that belongs to a different pair.
Core\Crypto\PublicKey::write turns the result into bytes you can send, in the format you name. The other side reads those bytes back with Core\Crypto\PublicKey::read.
Return value
Returns Core\Crypto\PublicKey. The public key, of the same kind as the pair. It is the same value that Core\Crypto\PublicKey::read returns for the key that the other side receives.
Examples
The half you send
<?nvs// Shows how to get the public half of a pair, and the two ways to write it out.
Core\Crypto\KeyPair $pair = Core\Crypto\KeyPair::read( Core\Encoding::fromHex('302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf'), Core\Crypto\KeyKind::Ed25519,);Core\Crypto\PublicKey $half = $pair->publicKey();
// `Raw` is the key itself, and nothing else. It is the shortest form.echo Core\Encoding::toHex($half->write(Core\Crypto\PublicKey::Format::Raw)), "\n";
// `Spki` is the same key with a header saying which kind it is. Most other// programs expect this form.echo Core\Encoding::toHex($half->write(Core\Crypto\PublicKey::Format::Spki)), "\n";
// The public half is worked out from the private key, so one file always gives// you the same public key.Core\Crypto\KeyPair $again = Core\Crypto\KeyPair::read( Core\Encoding::fromHex('302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf'), Core\Crypto\KeyKind::Ed25519,);echo 'the same key every time: ', ($again->publicKey()->write(Core\Crypto\PublicKey::Format::Raw) == $half->write(Core\Crypto\PublicKey::Format::Raw) ? 'yes' : 'no'), "\n";fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac302a300506032b6570032100fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261acthe same key every time: yesSwap public halves and agree a secret
<?nvs// Shows two programs agreeing one shared secret. Each one sends its public half// and keeps its private half. Here both sides run in one file.
Core\Crypto\KeyPair $mine = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);Core\Crypto\KeyPair $theirs = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);
// This is what travels over the network. The private halves never leave.bytes $iSend = $mine->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);bytes $theySend = $theirs->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);
// Each side agrees with its own private half and the half it received.secret bytes $ours = Core\Crypto::agree( $mine, Core\Crypto\PublicKey::read($theySend, Core\Crypto\KeyKind::X25519, Core\Crypto\PublicKey::Format::Raw),);secret bytes $theirsAgreed = Core\Crypto::agree( $theirs, Core\Crypto\PublicKey::read($iSend, Core\Crypto\KeyKind::X25519, Core\Crypto\PublicKey::Format::Raw),);
// Both sides now hold the same secret, and nobody who watched the network has// it.echo 'both sides agreed the same secret: ', ($ours == $theirsAgreed ? 'yes' : 'no'), "\n";both sides agreed the same secret: yesPublish your key so a partner can check it
<?nvs// Shows the way a service publishes its key. The server signs every webhook it// sends, and the partner checks it with the public half from your documentation.
Core\Crypto\KeyPair $server = Core\Crypto\KeyPair::read( Core\Encoding::fromHex('302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf'), Core\Crypto\KeyKind::Ed25519,);
// What goes in your documentation: the public half as one line of base64.string $published = Core\Encoding::toBase64( $server->publicKey()->write(Core\Crypto\PublicKey::Format::Spki),);echo 'public key: ', $published, "\n";
// The server sends this body, and this signature in a header.bytes $body = '{"event":"order.shipped","id":1042}' as bytes;string $header = Core\Encoding::toBase64(Core\Crypto::sign($body, $server));
// The partner has only the published line. It reads the key from that line and// checks the signature. `verify` throws an error when the check does not hold.Core\Crypto\PublicKey $ours = Core\Crypto\PublicKey::read( Core\Encoding::fromBase64($published), Core\Crypto\KeyKind::Ed25519, Core\Crypto\PublicKey::Format::Spki,);Core\Crypto::verify($body, Core\Encoding::fromBase64($header), $ours);echo 'the partner accepted the webhook', "\n";public key: MCowBQYDK2VwAyEA/M6QCKM1ZSSFYSlafOHMywQLYSm7qP8kdElTu8ASYaw=the partner accepted the webhook