Debian Codegraph @

src/pubkey.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#};
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#;
5
6 use superrust-analyzer cargo sshkeys 0.3.4 crate/::errorrust-analyzer cargo sshkeys 0.3.4 error/::{Errorrust-analyzer cargo sshkeys 0.3.4 error/Error#, ErrorKindrust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#, Resultrust-analyzer cargo sshkeys 0.3.4 error/Result#};
7 use superrust-analyzer cargo sshkeys 0.3.4 crate/::keytyperust-analyzer cargo sshkeys 0.3.4 keytype/::{KeyTyperust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#, KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#};
8 use superrust-analyzer cargo sshkeys 0.3.4 crate/::readerrust-analyzer cargo sshkeys 0.3.4 reader/::Readerrust-analyzer cargo sshkeys 0.3.4 reader/Reader#;
9 use superrust-analyzer cargo sshkeys 0.3.4 crate/::writerrust-analyzer cargo sshkeys 0.3.4 writer/::Writerrust-analyzer cargo sshkeys 0.3.4 writer/Writer#;
10
11 use base64rust-analyzer cargo base64 0.22.1 crate/;
12 use base64rust-analyzer cargo base64 0.22.1 crate/::Enginerust-analyzer cargo base64 0.22.1 engine/Engine#;
13
14 use sha2rust-analyzer cargo sha2 0.10.9 crate/::{Digestrust-analyzer cargo digest 0.10.7 digest/Digest#, Sha256rust-analyzer cargo sha2 0.10.9 Sha256#, Sha384rust-analyzer cargo sha2 0.10.9 Sha384#, Sha512rust-analyzer cargo sha2 0.10.9 Sha512#};
15
16 /// A type which represents the different kinds a public key can be.
17 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
18 pub enum PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind# {
19 /// Represents an RSA public key.
20 Rsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Rsa#(RsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey#),
21
22 /// Represents a DSA public key.
23 Dsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Dsa#(DsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#),
24
25 /// Represents an ECDSA public key.
26 Ecdsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Ecdsa#(EcdsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#),
27
28 /// Represents an ED25519 public key.
29 Ed25519rust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Ed25519#(Ed25519PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey#),
30 }
31
32 /// RSA public key.
33 /// The format of RSA public keys is described in RFC 4253, section 6.6
34 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
35 pub struct RsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey# {
36 /// Exponent of key.
37 pub erust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey#e.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
38
39 /// Modulus of key.
40 pub nrust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey#n.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
41 }
42
43 /// DSA public key.
44 /// The format of DSA public keys is described in RFC 4253, section 6.6
45 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
46 pub struct DsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey# {
47 /// Parameter `p`.
48 pub prust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#p.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
49
50 /// Parameter `q`.
51 pub qrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#q.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
52
53 /// Parameter `g`.
54 pub grust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#g.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
55
56 /// Parameter `y`.
57 pub yrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#y.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
58 }
59
60 /// Represents the different kinds of supported curves.
61 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
62 pub enum CurveKindrust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind# {
63 /// Represents a NIST P-256 curve.
64 Nistp256rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp256#,
65
66 /// Represents a NIST P-384 curve.
67 Nistp384rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp384#,
68
69 /// Represents a NIST P-521 curve.
70 Nistp521rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp521#,
71 }
72
73 /// A type which represents a cryptographic curve.
74 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
75 pub struct Curverust-analyzer cargo sshkeys 0.3.4 pubkey/Curve# {
76 /// The curve kind.
77 pub kindrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#kind.: CurveKindrust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#,
78
79 /// Curve identifier.
80 pub identifierrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#identifier.: &'static str,
81 }
82
83 impl Curverust-analyzer cargo sshkeys 0.3.4 pubkey/Curve# {
84 /// Creates a new `Curve` from the given identifier.
85 ///
86 /// # Example
87 /// ```rust
88 /// # use sshkeys;
89 /// let curve = sshkeys::Curve::from_identifier("nistp256").unwrap();
90 /// assert_eq!(curve.kind, sshkeys::CurveKind::Nistp256);
91 /// ```
92 pub fn from_identifierrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[Curve]from_identifier().(id: &str) -> Resultrust-analyzer cargo sshkeys 0.3.4 error/Result#<Curverust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#> {
93 let curve = match id {
94 "nistp256" => Curverust-analyzer cargo sshkeys 0.3.4 pubkey/Curve# {
95 kindrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#kind.: CurveKindrust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#::Nistp256rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp256#,
96 identifierrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#identifier.: "nistp256",
97 },
98 "nistp384" => Curverust-analyzer cargo sshkeys 0.3.4 pubkey/Curve# {
99 kindrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#kind.: CurveKindrust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#::Nistp384rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp384#,
100 identifierrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#identifier.: "nistp384",
101 },
102 "nistp521" => Curverust-analyzer cargo sshkeys 0.3.4 pubkey/Curve# {
103 kindrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#kind.: CurveKindrust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#::Nistp521rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp521#,
104 identifierrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#identifier.: "nistp521",
105 },
106 _ => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sshkeys 0.3.4 error/Error#::with_kindrust-analyzer cargo sshkeys 0.3.4 error/impl#[Error]with_kind().(ErrorKindrust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#::UnknownCurverust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#UnknownCurve#(id.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))),
107 };
108
109 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(curve)
110 }
111 }
112
113 /// ECDSA public key.
114 /// The format of ECDSA public keys is described in RFC 5656, section 3.1.
115 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
116 pub struct EcdsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey# {
117 /// The curve being used.
118 pub curverust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#curve.: Curverust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#,
119
120 /// The public key.
121 pub keyrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#key.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
122
123 /// Associated security key application, if any.
124 pub sk_applicationrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#sk_application.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
125 }
126
127 /// ED25519 public key.
128 /// The format of ED25519 public keys is described in https://tools.ietf.org/html/draft-bjh21-ssh-ed25519-02
129 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
130 pub struct Ed25519PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey# {
131 /// The public key.
132 pub keyrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey#key.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>,
133
134 /// Associated security key application, if any.
135 pub sk_applicationrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey#sk_application.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
136 }
137
138 /// A type which represents an OpenSSH public key.
139 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
140 pub struct PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey# {
141 /// Key type.
142 pub key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type.: KeyTyperust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#,
143
144 /// The kind of public key.
145 pub kindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#kind.: PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#,
146
147 /// Associated comment, if any.
148 pub commentrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#comment.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
149 }
150
151 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey# {
152 fn fmtrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
153 let comment = match self.commentrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#comment. {
154 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref c) => c,
155 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => "",
156 };
157
158 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
159 f,
160 "{} {} {}",
161 self.key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type.,
162 base64rust-analyzer cargo base64 0.22.1 crate/::enginerust-analyzer cargo base64 0.22.1 engine/::general_purposerust-analyzer cargo base64 0.22.1 engine/general_purpose/::STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD..encoderust-analyzer cargo base64 0.22.1 engine/Engine#encode().(&self.encoderust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]encode().()),
163 comment
164 )
165 }
166 }
167
168 /// The `FingerprintKind` enum represents the different fingerprint representation.
169 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
170 pub enum FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind# {
171 /// A kind used to represent the fingerprint using SHA256.
172 Sha256rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha256#,
173
174 /// A kind used to represent the fingerprint using SHA384.
175 Sha384rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha384#,
176
177 /// A kind used to represent the fingerprint using SHA512.
178 Sha512rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha512#,
179 }
180
181 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind# {
182 fn fmtrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[FingerprintKind][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
183 let kind = match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
184 FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#::Sha256rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha256# => "SHA256",
185 FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#::Sha384rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha384# => "SHA384",
186 FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#::Sha512rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha512# => "SHA512",
187 };
188
189 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}", kind)
190 }
191 }
192
193 /// A type that represents an OpenSSH public key fingerprint.
194 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
195 pub struct Fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint# {
196 /// The kind used to represent the fingerprint.
197 pub kindrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint#kind.: FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#,
198
199 /// The computed fingerprint.
200 pub hashrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint#hash.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
201 }
202
203 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint# {
204 fn fmtrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[Fingerprint][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
205 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}:{}", self.kindrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint#kind., self.hashrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint#hash.)
206 }
207 }
208
209 impl Fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint# {
210 /// Computes the fingerprint of a byte sequence using a given fingerprint representation.
211 ///
212 /// This method computes a fingerprint the way OpenSSH does it and is generally being
213 /// used to compute the fingerprint of an already encoded OpenSSH public key.
214 ///
215 /// # Example
216 /// ```rust
217 /// # use sshkeys;
218 /// let fp = sshkeys::Fingerprint::compute(sshkeys::FingerprintKind::Sha256, "some data".as_bytes());
219 /// assert_eq!(fp.kind, sshkeys::FingerprintKind::Sha256);
220 /// assert_eq!(fp.hash, "EweZDmulyhRes16ZGCqb7EZTG8VN32VqYCx4D6AkDe4");
221 /// ```
222 pub fn computerust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[Fingerprint]compute().<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(kind: FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#, data: &T) -> Fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint# {
223 let digest = match kind {
224 FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#::Sha256rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha256# => Sha256rust-analyzer cargo sha2 0.10.9 Sha256#::digestrust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]digest().(&data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()).to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
225 FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#::Sha384rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha384# => Sha384rust-analyzer cargo sha2 0.10.9 Sha384#::digestrust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]digest().(&data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()).to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
226 FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#::Sha512rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha512# => Sha512rust-analyzer cargo sha2 0.10.9 Sha512#::digestrust-analyzer cargo digest 0.10.7 digest/impl#[D][Digest]digest().(&data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()).to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),
227 };
228
229 let mut encoded = base64rust-analyzer cargo base64 0.22.1 crate/::enginerust-analyzer cargo base64 0.22.1 engine/::general_purposerust-analyzer cargo base64 0.22.1 engine/general_purpose/::STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD..encoderust-analyzer cargo base64 0.22.1 engine/Engine#encode().(&digest);
230
231 // Trim padding characters from end
232 let hash = match encoded.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().('=') {
233 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(offset) => encoded.drainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]drain().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#offset).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(),
234 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => encoded,
235 };
236
237 let fp = Fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint# {
238 kindrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint#kind.: kind,
239 hashrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint#hash.: hash,
240 };
241
242 fp
243 }
244 }
245
246 impl PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey# {
247 /// Reads an OpenSSH public key from a given path.
248 ///
249 /// # Examples
250 ///
251 /// ```rust
252 /// # fn example() -> sshkeys::Result<()> {
253 /// let key = sshkeys::PublicKey::from_path("/path/to/id_ed25519.pub")?;
254 /// # Ok(())
255 /// # }
256 /// ```
257 pub fn from_pathrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]from_path().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(path: P) -> Resultrust-analyzer cargo sshkeys 0.3.4 error/Result#<PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#> {
258 let mut contents = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
259 Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_string().(&mut contents)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
260
261 PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#::from_stringrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]from_string().(&contents)
262 }
263
264 /// Reads an OpenSSH public key from a given string.
265 ///
266 /// # Examples
267 ///
268 /// ```rust
269 /// # use sshkeys;
270 /// let key = sshkeys::PublicKey::from_string("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHkbe7gwx7s0dlApEEzpUyOAPrzPLy4czEZw/sh8m8rd me@home").unwrap();
271 /// let fp = key.fingerprint();
272 /// assert_eq!(fp.hash, "ciQkdxjFUhk2E2vRkWJD9kB8pi+EneOkaCJJHNWzPC4");
273 /// ```
274 pub fn from_stringrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]from_string().(contents: &str) -> Resultrust-analyzer cargo sshkeys 0.3.4 error/Result#<PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#> {
275 // Ssh public keys are on a single line, so we need to remove any new lines now.
276 let contents = contents
277 .linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().()
278 .nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Lines<'a>`][Iterator]next().()
279 .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo sshkeys 0.3.4 error/Error#::with_kindrust-analyzer cargo sshkeys 0.3.4 error/impl#[Error]with_kind().(ErrorKindrust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#::InvalidFormatrust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#InvalidFormat#))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
280
281 // Per man 8 sshd:
282 // """
283 // The optional comment field continues to the end of the line
284 // """
285 //
286 // This means we can't simply use str::split_whitespace since that can eat
287 // the comment field if the comment has spaces (which are valid).
288 // Additionally the function we *need* is currently nightly only.
289 //
290 // https://doc.rust-lang.org/std/str/struct.SplitWhitespace.html#method.remainder
291 //
292 // So this means we have to do this the hard way - we need to walk the string
293 // finding and splitting as we go. That's what the helper "split_whitespace" does
294 // for us.
295 let (kt_name, remainder) =
296 split_whitespacerust-analyzer cargo sshkeys 0.3.4 pubkey/split_whitespace().(contents).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo sshkeys 0.3.4 error/Error#::with_kindrust-analyzer cargo sshkeys 0.3.4 error/impl#[Error]with_kind().(ErrorKindrust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#::InvalidFormatrust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#InvalidFormat#))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
297
298 // If we have the ability to split, this means we have a comment.
299 // If the split fails, this means we only have data.
300 let (data, comment) = split_whitespacerust-analyzer cargo sshkeys 0.3.4 pubkey/split_whitespace().(remainder)
301 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|(data, comment)| {
302 let comment = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().(comment);
303
304 (data, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(comment))
305 })
306 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(
307 // We need to trim the end here as the key may have trailing whitespace
308 // but no comment.
309 (remainder.trim_endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end().(), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
310 );
311
312 let kt = KeyTyperust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#::from_namerust-analyzer cargo sshkeys 0.3.4 keytype/impl#[KeyType]from_name().(&kt_name)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
313
314 let decoded = base64rust-analyzer cargo base64 0.22.1 crate/::enginerust-analyzer cargo base64 0.22.1 engine/::general_purposerust-analyzer cargo base64 0.22.1 engine/general_purpose/::STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD..decoderust-analyzer cargo base64 0.22.1 engine/Engine#decode().(&data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
315 let mut reader = Readerrust-analyzer cargo sshkeys 0.3.4 reader/Reader#::newrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]new().(&decoded);
316
317 // Validate key type before reading rest of the data
318 let kt_from_reader = reader.read_stringrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_string().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
319 if kt_name rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]ne().!=rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]ne(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]ne().kt_from_reader {
320 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sshkeys 0.3.4 error/Error#::with_kindrust-analyzer cargo sshkeys 0.3.4 error/impl#[Error]with_kind().(ErrorKindrust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#::KeyTypeMismatchrust-analyzer cargo sshkeys 0.3.4 error/ErrorKind#KeyTypeMismatch#));
321 }
322
323 // Construct a new `PublicKey` value and preserve the `comment` value.
324 let k = PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#::from_readerrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]from_reader().(&kt_name, &mut reader)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
325 let key = PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey# {
326 key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type.: kt,
327 kindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#kind.: k.kindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#kind.,
328 commentrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#comment.: comment,
329 };
330
331 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(key)
332 }
333
334 /// Reads a public key from a given byte sequence.
335 ///
336 /// The byte sequence is expected to be the base64 decoded body of the public key.
337 ///
338 /// # Example
339 ///
340 /// ```rust
341 /// # use sshkeys;
342 /// let data = vec![0, 0, 0, 11, 115, 115, 104, 45,
343 /// 101, 100, 50, 53, 53, 49, 57,
344 /// 0, 0, 0, 32, 121, 27, 123, 184,
345 /// 48, 199, 187, 52, 118, 80, 41, 16,
346 /// 76, 233, 83, 35, 128, 62, 188,
347 /// 207, 47, 46, 28, 204, 70, 112,
348 /// 254, 200, 124, 155, 202, 221];
349 ///
350 /// let key = sshkeys::PublicKey::from_bytes(&data).unwrap();
351 /// let fp = key.fingerprint();
352 /// assert_eq!(fp.hash, "ciQkdxjFUhk2E2vRkWJD9kB8pi+EneOkaCJJHNWzPC4");
353 /// ```
354 pub fn from_bytesrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]from_bytes().<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(data: &T) -> Resultrust-analyzer cargo sshkeys 0.3.4 error/Result#<PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#> {
355 let mut reader = Readerrust-analyzer cargo sshkeys 0.3.4 reader/Reader#::newrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]new().(&data);
356 let kt_name = reader.read_stringrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_string().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
357
358 PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#::from_readerrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]from_reader().(&kt_name, &mut reader)
359 }
360
361 // This function is used for extracting a public key from an existing reader, e.g.
362 // we already have a reader for reading an OpenSSH certificate key and
363 // we want to extract the public key information from it.
364 pub(craterust-analyzer cargo sshkeys 0.3.4 crate/) fn from_readerrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]from_reader().(kt_name: &str, reader: &mut Readerrust-analyzer cargo sshkeys 0.3.4 reader/Reader#) -> Resultrust-analyzer cargo sshkeys 0.3.4 error/Result#<PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#> {
365 let kt = KeyTyperust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#::from_namerust-analyzer cargo sshkeys 0.3.4 keytype/impl#[KeyType]from_name().(&kt_name)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
366
367 let kind = match kt.kindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#kind. {
368 KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::Rsarust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#Rsa# | KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::RsaCertrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#RsaCert# => {
369 let k = RsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey# {
370 erust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey#e.: reader.read_mpintrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_mpint().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
371 nrust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey#n.: reader.read_mpintrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_mpint().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
372 };
373
374 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Rsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Rsa#(k)
375 }
376 KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::Dsarust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#Dsa# | KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::DsaCertrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#DsaCert# => {
377 let k = DsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey# {
378 prust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#p.: reader.read_mpintrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_mpint().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
379 qrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#q.: reader.read_mpintrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_mpint().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
380 grust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#g.: reader.read_mpintrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_mpint().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
381 yrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#y.: reader.read_mpintrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_mpint().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
382 };
383
384 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Dsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Dsa#(k)
385 }
386 KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::Ecdsarust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#Ecdsa# | KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::EcdsaCertrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#EcdsaCert# | KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::EcdsaSkrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#EcdsaSk# => {
387 let identifier = reader.read_stringrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_string().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
388 let curve = Curverust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#::from_identifierrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[Curve]from_identifier().(&identifier)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
389 let key = reader.read_bytesrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_bytes().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
390 let sk_application = match kt.kindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#kind. {
391 KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::EcdsaSkrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#EcdsaSk# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(reader.read_stringrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_string().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().),
392 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
393 };
394 let k = EcdsaPublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey# {
395 curverust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#curve.: curve,
396 keyrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#key.: key,
397 sk_applicationrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#sk_application.: sk_application,
398 };
399
400 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Ecdsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Ecdsa#(k)
401 }
402 KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::Ed25519rust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#Ed25519# | KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::Ed25519Certrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#Ed25519Cert# | KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::Ed25519Skrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#Ed25519Sk# => {
403 let key = reader.read_bytesrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_bytes().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
404 let sk_application = match kt.kindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#kind. {
405 KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::Ed25519Skrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#Ed25519Sk# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(reader.read_stringrust-analyzer cargo sshkeys 0.3.4 reader/impl#[`Reader<'a>`]read_string().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().),
406 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
407 };
408 let k = Ed25519PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey# {
409 keyrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey#key.: key,
410 sk_applicationrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey#sk_application.: sk_application,
411 };
412
413 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Ed25519rust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Ed25519#(k)
414 }
415 };
416
417 let key = PublicKeyrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey# {
418 key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type.: kt,
419 kindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#kind.: kind,
420 commentrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#comment.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
421 };
422
423 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(key)
424 }
425
426 /// Returns the number of bits of the public key.
427 ///
428 /// # Example
429 ///
430 /// ```rust
431 /// # use sshkeys;
432 /// let key = sshkeys::PublicKey::from_string("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHkbe7gwx7s0dlApEEzpUyOAPrzPLy4czEZw/sh8m8rd me@home").unwrap();
433 /// assert_eq!(key.bits(), 256);
434 /// ```
435 pub fn bitsrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]bits().(&self) -> usize {
436 match self.kindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#kind. {
437 // For RSA public key the size of the key is the number of bits of the modulus
438 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Rsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Rsa#(ref k) => k.nrust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey#n..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().8,
439 // For DSA public keys the size of the key is the number of bits of the `p` parameter
440 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Dsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Dsa#(ref k) => k.prust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#p..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().8,
441 // ECDSA key size depends on the curve
442 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Ecdsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Ecdsa#(ref k) => match k.curverust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#curve..kindrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#kind. {
443 CurveKindrust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#::Nistp256rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp256# => 256,
444 CurveKindrust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#::Nistp384rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp384# => 384,
445 CurveKindrust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#::Nistp521rust-analyzer cargo sshkeys 0.3.4 pubkey/CurveKind#Nistp521# => 521,
446 },
447 // ED25519 key size is 256 bits
448 // https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03#section-5.5
449 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Ed25519rust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Ed25519#(_) => 256,
450 }
451 }
452
453 /// Encodes the public key in an OpenSSH compatible format.
454 ///
455 /// # Example
456 /// ```rust
457 /// # use sshkeys;
458 /// let key = sshkeys::PublicKey::from_string("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHkbe7gwx7s0dlApEEzpUyOAPrzPLy4czEZw/sh8m8rd me@home").unwrap();
459 /// assert_eq!(key.encode(), vec![0, 0, 0, 11, 115, 115, 104, 45, 101, 100, 50, 53, 53, 49, 57, 0, 0, 0, 32, 121, 27, 123, 184, 48, 199, 187, 52, 118, 80, 41, 16, 76, 233, 83, 35, 128, 62, 188, 207, 47, 46, 28, 204, 70, 112, 254, 200, 124, 155, 202, 221]);
460 /// ```
461 pub fn encoderust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]encode().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> {
462 let mut w = Writerrust-analyzer cargo sshkeys 0.3.4 writer/Writer#::newrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]new().();
463
464 w.write_stringrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_string().(self.key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type..plainrust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#plain.);
465 match self.kindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#kind. {
466 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Rsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Rsa#(ref k) => {
467 w.write_mpintrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_mpint().(&k.erust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey#e.);
468 w.write_mpintrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_mpint().(&k.nrust-analyzer cargo sshkeys 0.3.4 pubkey/RsaPublicKey#n.);
469 }
470 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Dsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Dsa#(ref k) => {
471 w.write_mpintrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_mpint().(&k.prust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#p.);
472 w.write_mpintrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_mpint().(&k.qrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#q.);
473 w.write_mpintrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_mpint().(&k.grust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#g.);
474 w.write_mpintrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_mpint().(&k.yrust-analyzer cargo sshkeys 0.3.4 pubkey/DsaPublicKey#y.);
475 }
476 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Ecdsarust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Ecdsa#(ref k) => {
477 w.write_stringrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_string().(&k.curverust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#curve..identifierrust-analyzer cargo sshkeys 0.3.4 pubkey/Curve#identifier.);
478 w.write_bytesrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_bytes().(&k.keyrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#key.);
479 if self.key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type..kindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#kind. rust-analyzer cargo sshkeys 0.3.4 keytype/impl#[KeyTypeKind][`PartialEq<Self>`]eq().==rust-analyzer cargo sshkeys 0.3.4 keytype/impl#[KeyTypeKind][`PartialEq<Self>`]eq(). rust-analyzer cargo sshkeys 0.3.4 keytype/impl#[KeyTypeKind][`PartialEq<Self>`]eq().KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::EcdsaSkrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#EcdsaSk# {
480 w.write_stringrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_string().(&k.sk_applicationrust-analyzer cargo sshkeys 0.3.4 pubkey/EcdsaPublicKey#sk_application..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().());
481 }
482 }
483 PublicKeyKindrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#::Ed25519rust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKeyKind#Ed25519#(ref k) => {
484 w.write_bytesrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_bytes().(&k.keyrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey#key.);
485 if self.key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type..kindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#kind. rust-analyzer cargo sshkeys 0.3.4 keytype/impl#[KeyTypeKind][`PartialEq<Self>`]eq().==rust-analyzer cargo sshkeys 0.3.4 keytype/impl#[KeyTypeKind][`PartialEq<Self>`]eq(). rust-analyzer cargo sshkeys 0.3.4 keytype/impl#[KeyTypeKind][`PartialEq<Self>`]eq().KeyTypeKindrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#::Ed25519Skrust-analyzer cargo sshkeys 0.3.4 keytype/KeyTypeKind#Ed25519Sk# {
486 w.write_stringrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]write_string().(&k.sk_applicationrust-analyzer cargo sshkeys 0.3.4 pubkey/Ed25519PublicKey#sk_application..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().());
487 }
488 }
489 }
490
491 w.into_bytesrust-analyzer cargo sshkeys 0.3.4 writer/impl#[Writer]into_bytes().()
492 }
493
494 /// Computes the fingerprint of the public key using the
495 /// default OpenSSH fingerprint representation with SHA256.
496 ///
497 /// # Example
498 ///
499 /// ```rust
500 /// # use sshkeys;
501 /// # fn example() -> sshkeys::Result<()> {
502 /// let key = sshkeys::PublicKey::from_path("/path/to/id_ed25519.pub")?;
503 /// let fp = key.fingerprint();
504 /// println!("{}", fp.hash);
505 /// # Ok(())
506 /// # }
507 /// ```
508 pub fn fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]fingerprint().(&self) -> Fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint# {
509 self.fingerprint_withrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]fingerprint_with().(FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#::Sha256rust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#Sha256#)
510 }
511
512 /// Computes the fingerprint of the public key using a given
513 /// fingerprint representation.
514 ///
515 /// # Example
516 ///
517 /// ```rust
518 /// # use sshkeys;
519 /// # fn example() -> sshkeys::Result<()> {
520 /// let key = sshkeys::PublicKey::from_path("/path/to/id_ed25519.pub").unwrap();
521 /// let sha512fp = key.fingerprint_with(sshkeys::FingerprintKind::Sha512);
522 /// println!("{}", sha512fp.hash);
523 /// # Ok(())
524 /// # }
525 /// ```
526 pub fn fingerprint_withrust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]fingerprint_with().(&self, kind: FingerprintKindrust-analyzer cargo sshkeys 0.3.4 pubkey/FingerprintKind#) -> Fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint# {
527 Fingerprintrust-analyzer cargo sshkeys 0.3.4 pubkey/Fingerprint#::computerust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[Fingerprint]compute().(kind, &self.encoderust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]encode().())
528 }
529
530 /// Writes the public key to a given writer.
531 ///
532 /// # Example
533 /// ```rust
534 /// # use sshkeys;
535 /// use std::fs::File;
536 /// # fn example() -> sshkeys::Result<()> {
537 /// let key = sshkeys::PublicKey::from_string("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...")?;
538 /// let mut file = File::create("/path/to/id_ed25519.pub")?;
539 /// key.write(&mut file).unwrap();
540 /// # Ok(())
541 /// # }
542 /// ```
543 pub fn writerust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]write().<W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(&self, w: &mut W) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
544 let encoded = self.encoderust-analyzer cargo sshkeys 0.3.4 pubkey/impl#[PublicKey]encode().();
545 let data = base64rust-analyzer cargo base64 0.22.1 crate/::enginerust-analyzer cargo base64 0.22.1 engine/::general_purposerust-analyzer cargo base64 0.22.1 engine/general_purpose/::STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD..encoderust-analyzer cargo base64 0.22.1 engine/Engine#encode().(&encoded);
546 match self.commentrust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#comment. {
547 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref c) => w.write_fmtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_fmt().(format_argsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/format_args!!("{} {} {}\n", self.key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type..namerust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#name., data, c)),
548 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => w.write_fmtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_fmt().(format_argsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/format_args!!("{} {}\n", self.key_typerust-analyzer cargo sshkeys 0.3.4 pubkey/PublicKey#key_type..namerust-analyzer cargo sshkeys 0.3.4 keytype/KeyType#name., data)),
549 }
550 }
551 }
552
553 // "We have SplitWhitespace at home".
554 // https://doc.rust-lang.org/std/str/struct.SplitWhitespace.html#method.remainder
555 fn split_whitespacerust-analyzer cargo sshkeys 0.3.4 pubkey/split_whitespace().(s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(&str, &str)> {
556 // Find the first whitespace index.
557 let whitespace_start = s.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().(char::is_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_whitespace().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
558 let (left, right) = s.split_at_checkedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_at_checked().(whitespace_start)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
559
560 // Now find the start of right, skipping over a range of whitespace.
561 let non_whitespace_start = right.findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]find().(|c: char| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_whitespace().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
562 let (_, right) = right.split_at_checkedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_at_checked().(non_whitespace_start)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
563
564 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((left, right))
565 }