Use the Ed25519Key or Secp256K1Key classes to work with public/private key pairs.
$newKeyPair = new Casper\Util\Crypto\Ed25519Key();
$anotherKeyPair = new Casper\Util\Crypto\Secp256K1Key();
To read a key from a PEM file use the method createFromPrivateKeyFile()
$keyPair = \Casper\Util\Crypto\Ed25519Key::createFromPrivateKeyFile('/tmp/my_ed25519_pk.pem');
$anotherKeyPair = \Casper\Util\Crypto\Secp256K1Key::createFromPrivateKeyFile('/tmp/my_secp256k1_pk.pem');
When you create a new key you may want to save the private part in a file for later usage.
$privateKeyPemString = $keyPair->exportPrivateKeyInPem();
file_put_contents('/tmp/private_key.pem', $privateKeyPemString);
You can also export the public key part
$publicKeyPemString = $keyPair->exportPublicKeyInPem();
file_put_contents('/tmp/public_key.pem', $publicKeyPemString);
You can sign any message by sign()
method and verify signature by verify()
method
$message = 'Hello';
$signature = $keyPair->sign($message);
$isVerified = $keyPair->verify($signature, $message);