Core\Crypto::generateKeyPair
Core\Crypto::generateKeyPair(
Core\Crypto\KeyKind $kind,
): Core\Crypto\KeyPairDraws a new private key of the kind you name, and gives you the pair it belongs to.
A key pair is two halves that belong together. The private half stays inside your program. It signs messages with Core\Crypto::sign, and it agrees a shared secret with another program through Core\Crypto::agree. The public half is the one you publish or send to somebody else, so they can check your signatures or agree that same secret. Core\Crypto\KeyPair::publicKey gives you that half.
There is no key size to choose, because each kind has one size. P256 agrees and signs, X25519 only agrees, and Ed25519 only signs.
Every call draws a new pair. To keep one pair across restarts, store the bytes that Core\Crypto\KeyPair::write gives you and read them back with Core\Crypto\KeyPair::read.
Good to know: the two RSA kinds are not drawn here. An RSA key is issued to you by somebody else, so you read that file with Core\Crypto\KeyPair::read.
Parameters
$kindCore\Crypto\KeyKindThe kind of key to create.P256agrees and signs,X25519only agrees, andEd25519only signs. The two RSA kinds are only read, never created.
Return value
Returns Core\Crypto\KeyPair. The key pair, ready to sign or to agree. Its write method gives PKCS#8, which Core\Crypto\KeyPair::read reads back.
Errors
LogicError$kindisRsaPkcs1orRsaPss. This method never creates an RSA key. You get an RSA key from whoever issued it, andCore\Crypto\KeyPair::readreads it.
Examples
A new pair and the half you send
<?nvs// Shows how to draw a key pair and how to get the half you send. The private// half stays in your program. The public half is safe to publish.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
// `publicKey` gives you the half you send. `write` turns it into bytes, in one// of the three formats other languages read.bytes $raw = $pair->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);bytes $spki = $pair->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);
// This prints `raw: 32 bytes` and `spki: 44 bytes`.echo 'raw: ', Core\Bytes::length($raw), ' bytes', "\n";echo 'spki: ', Core\Bytes::length($spki), ' bytes', "\n";
// Every call draws a new pair, so a second pair has a different public key.Core\Crypto\KeyPair $other = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);bytes $otherRaw = $other->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);echo 'the second pair is a different one: ', ($raw != $otherRaw ? 'yes' : 'no'), "\n";raw: 32 bytesspki: 44 bytesthe second pair is a different one: yesOne kind for each job
<?nvs// Shows the three kinds of key pair you can draw. The kind is the only thing// you choose, and it decides what the pair can do.
// `P256` agrees a shared secret with another program, and signs.Core\Crypto\KeyPair $p256 = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);// `X25519` only agrees a shared secret.Core\Crypto\KeyPair $x25519 = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);// `Ed25519` only signs.Core\Crypto\KeyPair $ed25519 = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
// Each kind has one size, so the public key of a kind is always the same// length. This prints 91, 44 and 44.array<Core\Crypto\KeyPair> $pairs = [$p256, $x25519, $ed25519];foreach ($pairs as Core\Crypto\KeyPair $pair) { echo Core\Bytes::length($pair->publicKey()->write(Core\Crypto\PublicKey::Format::Spki)), "\n";}
// The two keys that agree must be of the same kind. Here both sides are// `X25519`, and they reach one secret.Core\Crypto\KeyPair $peer = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);secret bytes $mine = Core\Crypto::agree($x25519, $peer->publicKey());secret bytes $theirs = Core\Crypto::agree($peer, $x25519->publicKey());echo 'one secret from two X25519 pairs: ', ($mine == $theirs ? 'yes' : 'no'), "\n";914444one secret from two X25519 pairs: yesOne server key kept across restarts
<?nvs// Shows how a server keeps one key pair when it restarts. The pair is drawn// once, saved to a file, and read back the next time the program starts.
Core\Crypto\KeyPair $pair = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);
// `write` gives the private key as bytes. `Core\Secret::revealBytes` takes a// secret and a reason, and gives you plain bytes your program can store.bytes $stored = Core\Secret::revealBytes( $pair->write(), 'the server key is written to disk and read back at the next start',);string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'server-key.bin');Core\IO::write($path, $stored);
// This is what the next start does. `Core\IO::readBytes` reads the saved bytes.// You name the kind, because the stored bytes do not say which kind you expect.Core\Crypto\KeyPair $again = Core\Crypto\KeyPair::read( Core\IO::readBytes($path), Core\Crypto\KeyKind::P256,);
// It is the same pair, so it has the same public key. Clients that already know// this server can still check its signatures.bytes $before = $pair->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);bytes $after = $again->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);echo 'the same key came back: ', ($before == $after ? 'yes' : 'no'), "\n";the same key came back: yes