| 1 | use byteorder::{BigEndian, ByteOrder}; |
| 2 | |
| 3 | /// A `Writer` is used for encoding a key in OpenSSH compatible format. |
| 4 | #[derive(Debug)] |
| 5 | pub struct Writer { |
| 6 | inner: Vec<u8>, |
| 7 | } |
| 8 | |
| 9 | impl Writer { |
| 10 | /// Creates a new `Writer` instance. |
| 11 | /// |
| 12 | /// # Example |
| 13 | /// ```rust |
| 14 | /// let writer = sshkeys::Writer::new(); |
| 15 | /// ``` |
| 16 | pub fn new() -> Writer { |
| 17 | Writer { inner: Vec::new() } |
| 18 | } |
| 19 | |
| 20 | /// Writes a byte sequence to the underlying vector. |
| 21 | /// The value is represented as a the byte sequence length, |
| 22 | /// followed by the actual byte sequence. |
| 23 | /// |
| 24 | /// # Example |
| 25 | /// ```rust |
| 26 | /// # use sshkeys; |
| 27 | /// let mut writer = sshkeys::Writer::new(); |
| 28 | /// writer.write_bytes(&[0, 0, 0, 42]); |
| 29 | /// let bytes = writer.into_bytes(); |
| 30 | /// assert_eq!(bytes, vec![0, 0, 0, 4, 0, 0, 0, 42]); |
| 31 | /// ``` |
| 32 | pub fn write_bytes(&mut self, val: &[u8]) { |
| 33 | let size = val.len() as u32; |
| 34 | let mut buf = vec![0; 4]; |
| 35 | BigEndian::write_u32(&mut buf, size); |
| 36 | self.inner.append(&mut buf); |
| 37 | self.inner.extend_from_slice(&val); |
| 38 | } |
| 39 | |
| 40 | /// Writes a `string` value to the underlying byte sequence. |
| 41 | /// |
| 42 | /// # Example |
| 43 | /// ```rust |
| 44 | /// # use sshkeys; |
| 45 | /// let mut writer = sshkeys::Writer::new(); |
| 46 | /// writer.write_string("a test string"); |
| 47 | /// let bytes = writer.into_bytes(); |
| 48 | /// assert_eq!(bytes, [0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]); |
| 49 | /// ``` |
| 50 | pub fn write_string(&mut self, val: &str) { |
| 51 | self.write_bytes(val.as_bytes()); |
| 52 | } |
| 53 | |
| 54 | /// Writes an `mpint` value to the underlying byte sequence. |
| 55 | /// If the MSB bit of the first byte is set then the number is |
| 56 | /// negative, otherwise it is positive. |
| 57 | /// Positive numbers must be preceeded by a leading zero byte according to RFC 4251, section 5. |
| 58 | /// |
| 59 | /// # Example |
| 60 | /// ```rust |
| 61 | /// # use sshkeys; |
| 62 | /// let mut writer = sshkeys::Writer::new(); |
| 63 | /// writer.write_mpint(&[1, 0, 1]); |
| 64 | /// let bytes = writer.into_bytes(); |
| 65 | /// assert_eq!(bytes, [0, 0, 0, 3, 1, 0, 1]); |
| 66 | /// ``` |
| 67 | pub fn write_mpint(&mut self, val: &[u8]) { |
| 68 | let mut bytes = val.to_vec(); |
| 69 | |
| 70 | // If most significant bit is set then prepend a zero byte to |
| 71 | // avoid interpretation as a negative number. |
| 72 | if val.get(0).unwrap_or(&0) & 0x80 != 0 { |
| 73 | bytes.insert(0, 0); |
| 74 | } |
| 75 | |
| 76 | self.write_bytes(&bytes); |
| 77 | } |
| 78 | |
| 79 | /// Converts the `Writer` into a byte sequence. |
| 80 | /// This consumes the underlying byte sequence used by the `Writer`. |
| 81 | /// |
| 82 | /// # Example |
| 83 | /// ```rust |
| 84 | /// let mut writer = sshkeys::Writer::new(); |
| 85 | /// writer.write_string("some data"); |
| 86 | /// let bytes = writer.into_bytes(); |
| 87 | /// assert_eq!(bytes, [0, 0, 0, 9, 115, 111, 109, 101, 32, 100, 97, 116, 97]); |
| 88 | /// ``` |
| 89 | pub fn into_bytes(self) -> Vec<u8> { |
| 90 | self.inner |
| 91 | } |
| 92 | } |