From 91027cd14e4cfd040c5d3b24453fc20a1a5dff11 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Mon, 3 Jun 2024 17:13:27 +0200 Subject: [PATCH] Add `CoreEdDsaPrivateSigningKey::generate` --- src/core/jwk/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/core/jwk/mod.rs b/src/core/jwk/mod.rs index ba10d03..8148d34 100644 --- a/src/core/jwk/mod.rs +++ b/src/core/jwk/mod.rs @@ -7,7 +7,8 @@ use crate::{ }; use ed25519_dalek::pkcs8::DecodePrivateKey; -use ed25519_dalek::Signer; +use ed25519_dalek::{Signer, SigningKey}; +use rand::RngCore; use rsa::pkcs1::DecodeRsaPrivateKey; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256, Sha384, Sha512}; @@ -493,6 +494,19 @@ pub struct CoreEdDsaPrivateSigningKey { key_pair: ed25519_dalek::SigningKey, } impl CoreEdDsaPrivateSigningKey { + /// Generate a new `CoreEdDsaPrivateSigningKey` + pub fn generate(kid: Option) -> Self { + let mut rng = rand::rngs::OsRng; + + let mut secret = ed25519_dalek::SecretKey::default(); + rng.fill_bytes(&mut secret); + + Self { + kid, + key_pair: SigningKey::from_bytes(&secret), + } + } + /// Converts an EdDSA private key (in PEM format) to a JWK representing its public key. pub fn from_ed25519_pem(pem: &str, kid: Option) -> Result { Ok(Self {