diff --git a/ssh-key/src/public/sk.rs b/ssh-key/src/public/sk.rs index 53dab24..dedc191 100644 --- a/ssh-key/src/public/sk.rs +++ b/ssh-key/src/public/sk.rs @@ -132,6 +132,15 @@ pub struct SkEd25519 { } impl SkEd25519 { + /// Construct new instance of SkEd25519. + #[cfg(feature = "alloc")] + pub fn new(public_key: Ed25519PublicKey, application: impl Into) -> Self { + SkEd25519 { + public_key: public_key, + application: application, + } + } + /// Get the Ed25519 private key for this security key. pub fn public_key(&self) -> &Ed25519PublicKey { &self.public_key diff --git a/ssh-key/tests/public_key.rs b/ssh-key/tests/public_key.rs index 0c54d62..4c02047 100644 --- a/ssh-key/tests/public_key.rs +++ b/ssh-key/tests/public_key.rs @@ -3,7 +3,7 @@ use hex_literal::hex; use sec1::consts::U32; use ssh_key::{Algorithm, PublicKey}; -use ssh_key::public::SkEcdsaSha2NistP256; +use ssh_key::public::{Ed25519PublicKey, SkEcdsaSha2NistP256, SkEd25519}; use std::collections::HashSet; #[cfg(feature = "ecdsa")] @@ -346,6 +346,25 @@ fn decode_sk_ed25519_openssh() { ); } +#[test] +fn new_sk_ed25519_openssh() { + const EXAMPLE_PUBKEY: Ed25519PublicKey = Ed25519PublicKey {0: [ + 0x21, 0x68, 0xfe, 0x4e, 0x4b, 0x53, 0xcf, 0x3a, + 0xde, 0xee, 0xba, 0x60, 0x2f, 0x5e, 0x50, 0xed, + 0xb5, 0xef, 0x44, 0x1d, 0xba, 0x88, 0x4f, 0x51, + 0x19, 0x10, 0x9d, 0xb2, 0xda, 0xfd, 0xd7, 0x33, + ]}; + + let sk_key = SkEd25519::new(EXAMPLE_PUBKEY, "ssh:".to_string()); + let key = PublicKey::from_openssh(OPENSSH_SK_ED25519_EXAMPLE).unwrap(); + + let ed25519_key = key.key_data().sk_ed25519().unwrap(); + assert_eq!( + &sk_key, + ed25519_key + ); +} + #[cfg(all(feature = "alloc"))] #[test] fn decode_custom_algorithm_openssh() {