use p256::{ ecdsa::{ signature::{Signer, Verifier}, SigningKey, VerifyingKey, }, pkcs8::EncodePrivateKey, PublicKey, SecretKey, }; use rand_core::OsRng; use std::fs::OpenOptions; use std::io::Write; fn main(){ let secret_key = SigningKey::random(&mut OsRng); let secret_key_serialized = secret_key .to_pkcs8_pem(Default::default()) .unwrap() .to_string(); println!("Secret Key: \n{}", secret_key_serialized); let secret_key = secret_key_serialized.parse::<SecretKey>().unwrap(); let public_key = secret_key.public_key(); let public_key_serialized = public_key.to_string(); println!("Public Key: \n{}", public_key_serialized); let public_key = public_key_serialized.parse::<PublicKey>().unwrap(); let signing_key: SigningKey = secret_key.into(); let message = b"ECDSA proves knowledge of a secret number in the context of a single message"; let signature = signing_key.sign(message); let verifying_key: VerifyingKey = public_key.into(); assert!(verifying_key.verify(message, &signature).is_ok()); }
Finished `dev` profile [unoptimized + debuginfo] target(s) in 5.89s
Running `target/debug/sample`
Secret Key:
—–BEGIN PRIVATE KEY—–
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgqmVTGsuHMDgaN3TG
Jvsem2P1dA3l/wnRsxPfN8PTnUqhRANCAASv58iralPg2mOjuf28sSC8UuxqR4kD
u9tATiYXDvaU6BfmuI0tl0JsrZ2Brf1BkXtzbwBbiM2+h6+I6J55TU2p
—–END PRIVATE KEY—–
Public Key:
—–BEGIN PUBLIC KEY—–
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEr+fIq2pT4Npjo7n9vLEgvFLsakeJ
A7vbQE4mFw72lOgX5riNLZdCbK2dga39QZF7c28AW4jNvoeviOieeU1NqQ==
—–END PUBLIC KEY—–
p256とk256は微妙に違うのね。。これをpemファイルとして保存したい。