|
135 | 135 | //! |
136 | 136 | //! [RFC3066]: https://datatracker.ietf.org/doc/html/rfc3066 |
137 | 137 | //! [RFC4251 § 5]: https://datatracker.ietf.org/doc/html/rfc4251#section-5 |
| 138 | +//! |
| 139 | +//! ## Deriving [`Decode`] and [`Encode`] |
| 140 | +//! |
| 141 | +//! The traits [`Decode`] and [`Encode`] can be derived for any struct or enum where all its fields |
| 142 | +//! implement [`Decode`] and [`Encode`] respectively. |
| 143 | +//! |
| 144 | +//! To use this functionality, enable the `derive` crate feature for `ssh-encoding`. |
| 145 | +//! |
| 146 | +//! ### Example |
| 147 | +//! |
| 148 | +//! Here is an example of how you could define a handful of the SSH message types: |
| 149 | +//! |
| 150 | +#![cfg_attr(all(feature = "alloc", feature = "derive"), doc = "```")] |
| 151 | +#![cfg_attr(not(all(feature = "alloc", feature = "derive")), doc = "```ignore")] |
| 152 | +//! use ssh_encoding::{Decode, Encode}; |
| 153 | +//! |
| 154 | +//! #[derive(Debug, PartialEq, Encode, Decode)] |
| 155 | +//! #[repr(u8)] |
| 156 | +//! enum Message { |
| 157 | +//! Disconnect { |
| 158 | +//! reason_code: u32, |
| 159 | +//! description: String, |
| 160 | +//! language_tag: String, |
| 161 | +//! } = 1, |
| 162 | +//! EcdhInit { |
| 163 | +//! client_public_key: Vec<u8>, |
| 164 | +//! } = 30, |
| 165 | +//! EcdhReply { |
| 166 | +//! host_key: HostKey, |
| 167 | +//! server_public_key: Vec<u8>, |
| 168 | +//! #[ssh(length_prefixed)] |
| 169 | +//! host_signature: HostSignature, |
| 170 | +//! } = 31, |
| 171 | +//! } |
| 172 | +//! |
| 173 | +//! #[derive(Debug, PartialEq, Encode, Decode)] |
| 174 | +//! #[ssh(length_prefixed)] |
| 175 | +//! struct HostKey { |
| 176 | +//! key_type: String, |
| 177 | +//! ecdsa_curve_identifier: String, |
| 178 | +//! ecdsa_public_key: Vec<u8>, |
| 179 | +//! } |
| 180 | +//! |
| 181 | +//! #[derive(Debug, PartialEq, Encode, Decode)] |
| 182 | +//! struct HostSignature { |
| 183 | +//! signature_type: String, |
| 184 | +//! signature: Vec<u8>, |
| 185 | +//! } |
| 186 | +//! |
| 187 | +//! let message = Message::EcdhReply { |
| 188 | +//! host_key: HostKey { |
| 189 | +//! key_type: "ecdsa-sha2-nistp256".into(), |
| 190 | +//! ecdsa_curve_identifier: "nistp256".into(), |
| 191 | +//! ecdsa_public_key: vec![0x01, 0x02, 0x03], |
| 192 | +//! }, |
| 193 | +//! server_public_key: vec![0x04, 0x05, 0x06], |
| 194 | +//! host_signature: HostSignature { |
| 195 | +//! signature_type: "ecdsa-sha2-nistp256".into(), |
| 196 | +//! signature: vec![0x07, 0x08, 0x09], |
| 197 | +//! }, |
| 198 | +//! }; |
| 199 | +//! |
| 200 | +//! let encoded = message.encode_vec().unwrap(); |
| 201 | +//! assert_eq!(&encoded[..13], &[31, 0, 0, 0, 42, 0, 0, 0, 19, 101, 99, 100, 115]); |
| 202 | +//! let decoded = Message::decode(&mut &encoded[..]).unwrap(); |
| 203 | +//! assert_eq!(message, decoded); |
| 204 | +//! ``` |
138 | 205 |
|
139 | 206 | #[cfg(feature = "alloc")] |
140 | 207 | #[macro_use] |
@@ -180,5 +247,3 @@ pub use crate::pem::{DecodePem, EncodePem}; |
180 | 247 |
|
181 | 248 | #[cfg(feature = "derive")] |
182 | 249 | pub use ssh_derive::{Decode, Encode}; |
183 | | -#[cfg(feature = "derive")] |
184 | | -pub mod derive; |
|
0 commit comments