Skip to content

Commit

Permalink
ssh-key: introduce SkEd25519::new() constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jviki committed Mar 6, 2024
1 parent 11a037c commit 627a87a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ssh-key/src/public/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> 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
Expand Down
21 changes: 20 additions & 1 deletion ssh-key/tests/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 627a87a

Please sign in to comment.