Skip to content

Core\Crypto\KeyPair::read

Reads a private key your program already has, and gives you the key pair it belongs to.

The key must be PKCS#8, so a key from another program and a key from Core\Crypto\KeyPair::write are read the same way. It may be binary DER, or one PEM block that starts with -----BEGIN PRIVATE KEY-----.

You always name the kind of key. The file does not say whether an RSA key is for RsaPkcs1 or for RsaPss. If somebody swapped the file for a key of another kind, read throws an error.

The key is parsed once, here. The pair can then sign with Core\Crypto::sign and agree a shared secret with Core\Crypto::agree. Core\Crypto\KeyPair::publicKey gives the half you send to other people.

Good to know: a key in another format throws a LogicError. Examples are a key with a password (ENCRYPTED PRIVATE KEY), an RSA key in PKCS#1 (RSA PRIVATE KEY) and an EC key in SEC1 (EC PRIVATE KEY). The message names the format and the command that converts the file to PKCS#8.

Parameters

  • $pkcs8secret bytesThe bytes of the key, as write returned them, or as the issuer of the key wrote them.
  • $kindCore\Crypto\KeyKindThe kind of the key. You must give it, for the same reason as in Core\Crypto\PublicKey::read. A PKCS#8 key says that it is RSA, but not whether it is for RS256 or PS256.

Return value

Returns Core\Crypto\KeyPair. The key pair, ready to sign or to agree. Its publicKey() method returns the public key, which you can send to others.

Errors

  • LogicError$pkcs8 is not a PKCS#8 private key of that kind. For example, it is a key of another kind, or a SEC1 or PKCS#1 key. It can be an encrypted PKCS#8, which needs a password, and this method has no password argument. It can be an RSA key outside 2048 to 4096 bits, or any malformed data. The message names the format that was found. Where a command converts that format to PKCS#8, the message gives the command. A private key belongs to your program, never to the other side. So this error always means that your program was given a wrong key.

Examples

Read a stored key and sign with it

<?nvs
// Shows how to read a private key your program stored, and how to sign a
// message with it. The key is an Ed25519 key, kept as hex text.
string $stored = '302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf';
// `read` takes the key's octets and the kind of key they hold. It gives you the
// pair that key belongs to.
Core\Crypto\KeyPair $pair = Core\Crypto\KeyPair::read(
Core\Encoding::fromHex($stored),
Core\Crypto\KeyKind::Ed25519,
);
bytes $message = 'order 1042 shipped' as bytes;
bytes $signature = Core\Crypto::sign($message, $pair);
// The public half of the same pair checks the signature. `verify` returns
// nothing, and throws an error when the check does not hold.
Core\Crypto::verify($message, $signature, $pair->publicKey());
echo 'the stored key signed the message', "\n";
// The file holds one key, so every run reads that key and writes this same
// signature.
echo Core\Encoding::toHex($signature), "\n";
Output
the stored key signed the message
4f616bc7d091dd65ef7dd0ca37d36665e6196302848f7f5e3e59c62ed068c5c687a46dbf89b4f82cca4781cb9053f0aa8e3bbfcfd6a807bf18eeda2bf9707504

The same key as a pem block

<?nvs
// Shows the two ways a key file is written. A PEM block is text, and `read`
// takes it as well as the binary form.
string $pem = "-----BEGIN PRIVATE KEY-----\n"
. "MC4CAQAwBQYDK2VwBCIEIDbnkTx1/12Juk9GUmMg9UfPdTaaVgoWj2q1uxePvqrf\n"
. "-----END PRIVATE KEY-----\n";
Core\Crypto\KeyPair $fromPem = Core\Crypto\KeyPair::read(
$pem as bytes,
Core\Crypto\KeyKind::Ed25519,
);
// The binary form of the same key. It is what the text above carries.
string $der = '302e020100300506032b65700422042036e7913c75ff5d89ba4f46526320f547cf75369a560a168f6ab5bb178fbeaadf';
Core\Crypto\KeyPair $fromDer = Core\Crypto\KeyPair::read(
Core\Encoding::fromHex($der),
Core\Crypto\KeyKind::Ed25519,
);
// One key read twice, so both pairs have the same public half.
bytes $fromText = $fromPem->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);
bytes $fromBinary = $fromDer->publicKey()->write(Core\Crypto\PublicKey::Format::Spki);
echo 'both files are one key: ', ($fromText == $fromBinary ? 'yes' : 'no'), "\n";
// `write` always gives the binary form, whichever form was read. Your program
// stores that and reads it back later.
secret bytes $again = $fromPem->write();
echo 'the pair writes the binary form back: ',
($again == Core\Encoding::fromHex($der) ? 'yes' : 'no'),
"\n";
Output
both files are one key: yes
the pair writes the binary form back: yes

One key read at the start signs every message

<?nvs
// Shows the way a server uses this member. The key comes from the place the
// server keeps its secrets. It is read once when the program starts, and every
// message is signed with the pair that was read.
class Outbox
{
// Reading the key for every message would parse the same file again and
// again, so the program reads it once and keeps the pair.
public static function open(string $secret): Core\Crypto\KeyPair
{
return Core\Crypto\KeyPair::read(
Core\Encoding::fromBase64($secret),
Core\Crypto\KeyKind::Ed25519,
);
}
public static function stamp(Core\Crypto\KeyPair $pair, string $event): string
{
return Core\Encoding::toBase64(Core\Crypto::sign($event as bytes, $pair));
}
}
// What a secret store gives you: the key as one line of base64.
string $fromTheStore = 'MC4CAQAwBQYDK2VwBCIEIDbnkTx1/12Juk9GUmMg9UfPdTaaVgoWj2q1uxePvqrf';
Core\Crypto\KeyPair $pair = Outbox::open($fromTheStore);
array<string> $events = [
'order.shipped 1042',
'order.shipped 1043',
'order.packed 1044',
];
foreach ($events as string $event) {
echo $event, ' ', Outbox::stamp($pair, $event), "\n";
}
Output
order.shipped 1042 CTI6sl7XBXBBVolnMuHo18ZpppaMsmjzEoMqFg9ZaMwFQ7cUTYz19TwelVvDww8dKKoqqcJdH8dGsy/NA10uCQ==
order.shipped 1043 SfVEYbVhE3Zh4wAIaNyXvIIHsCnJicm1KLLXD8lzT8DMcWtynWYZhiiK8I0JDI2Ah5WcCSHs6u83W4Ju8u5/Bg==
order.packed 1044 jakgRwLvBcqaLPmqjQYGHTRZUkFbdSbbBjtqjLxSTuewZgymPopQQIRJGICm8HhZ6akBsHAhZcq5WRFz14onBQ==