Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created November 18, 2018 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/cdaecb608f965b037cd8a522a9584123 to your computer and use it in GitHub Desktop.
Save rust-play/cdaecb608f965b037cd8a522a9584123 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
pub trait SigningKey {
fn create_digest(&self, data: Vec<u8>) -> Vec<u8>;
}
pub struct RsaKey {
modulus: Vec<u8>,
exponent: Vec<u8>,
}
impl SigningKey for RsaKey {
fn create_digest(&self, data: Vec<u8>) -> Vec<u8> {
// In the real world at this point we'd calculate a SHA1, stick the
// right DER goop in front of it and return that. Because this is
// only an example we're just going to return what we got handed.
Vec::from(data)
}
}
pub enum EcdsaCurve {
NistP256,
NistP384,
NistP521,
}
pub struct EcdsaKey {
pub curve: EcdsaCurve,
pub key_blob: Vec<u8>,
}
impl SigningKey for EcdsaKey {
fn create_digest(&self, data: Vec<u8>) -> Vec<u8> {
// See above for an explanation of our horrible lies.
Vec::from(data)
}
}
pub enum Key {
Ecdsa(EcdsaKey),
Rsa(RsaKey),
}
impl Key {
fn create_digest(&self, data: Vec<u8>) -> Vec<u8> {
// How can I call the create_digest method of the variant that I have
// here without copy/pasting the same match arm all the way down?
match self {
Key::Ecdsa(k) => k.create_digest(data),
Key::Rsa(k) => k.create_digest(data),
// Oh god I hope I never have to add any more...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment