1use std::fmt;
2
3use base64;
4use byteorder::{BigEndian, WriteBytesExt};
5
6use {PublicKey, PrivateKey};
7
8
9impl Clone for PublicKey {
11 fn clone(&self) -> PublicKey {
12 use PublicKey::*;
13 match *self {
14 Rsa { ref exponent, ref modulus } => {
15 Rsa { exponent: exponent.clone(), modulus: modulus.clone() }
16 }
17 Ed25519(data) => Ed25519(data),
18 }
19 }
20}
21
22impl PartialEq for PublicKey {
24 fn eq(&self, other: &PublicKey) -> bool {
25 use PublicKey::*;
26 match (self, other) {
27 (&Rsa { exponent: ref e1, modulus: ref n1 },
28 &Rsa { exponent: ref e2, modulus: ref n2 })
29 => e1 == e2 && n1 == n2,
30 (&Rsa {..}, _) => false,
31 (&Ed25519(ref d1), &Ed25519(ref d2)) => d1 == d2,
32 (&Ed25519(..), _) => false,
33 }
34
35 }
36}
37
38impl Eq for PublicKey { }
39
40impl fmt::Display for PublicKey {
41 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42 use PublicKey::*;
43 match *self {
44 Rsa { ref exponent, ref modulus } => {
45 let mut buf = Vec::with_capacity(512);
46 buf.write_u32::<BigEndian>("ssh-rsa".len() as u32).unwrap();
47 buf.extend(b"ssh-rsa");
48 buf.write_u32::<BigEndian>(exponent.len() as u32).unwrap();
49 buf.extend(exponent);
50 buf.write_u32::<BigEndian>(modulus.len() as u32).unwrap();
51 buf.extend(modulus);
52 write!(f, "ssh-rsa {}",
53 base64::display::Base64Display::standard(&buf))
54 }
55 Ed25519(data) => {
56 let mut buf = Vec::with_capacity(512);
57 buf.write_u32::<BigEndian>("ssh-ed25519".len() as u32).unwrap();
58 buf.extend(b"ssh-ed25519");
59 buf.write_u32::<BigEndian>(data.len() as u32).unwrap();
60 buf.extend(&data);
61 write!(f, "ssh-ed25519 {}",
62 base64::display::Base64Display::standard(&buf))
63 }
64 }
65 }
66}
67
68impl Clone for PrivateKey {
70 fn clone(&self) -> PrivateKey {
71 use PrivateKey::*;
72 match *self {
73 Ed25519(data) => Ed25519(data),
74 Rsa { ref n, ref e, ref d, ref iqmp, ref p, ref q }
75 => Rsa { n: n.clone(), e: e.clone(), d: d.clone(),
76 iqmp: iqmp.clone(), p: p.clone(), q: q.clone() },
77 }
78 }
79}
80
81impl PartialEq for PrivateKey {
83 fn eq(&self, other: &PrivateKey) -> bool {
84 use PrivateKey::*;
85 match (self, other) {
86 (&Rsa { n: ref n1, e: ref e1, d: ref d1, iqmp: ref iqmp1,
87 p: ref p1, q: ref q1 },
88 &Rsa { n: ref n2, e: ref e2, d: ref d2, iqmp: ref iqmp2,
89 p: ref p2, q: ref q2 }
90 ) => {
91 n1 == n2 && e1 == e2 && d1 == d2 && iqmp1 == iqmp2 &&
92 p1 == p2 && q1 == q2
93 }
94 (&Rsa {..}, _) => false,
95 (&Ed25519(ref d1), &Ed25519(ref d2)) => &d1[..] == &d2[..],
96 (&Ed25519(..), _) => false,
97 }
98
99 }
100}
101
102impl Eq for PrivateKey { }