pub struct Writer { /* private fields */ }Expand description
A Writer is used for encoding a key in OpenSSH compatible format.
Implementations§
Source§impl Writer
impl Writer
Sourcepub fn write_bytes(&mut self, val: &[u8])
pub fn write_bytes(&mut self, val: &[u8])
Writes a byte sequence to the underlying vector. The value is represented as a the byte sequence length, followed by the actual byte sequence.
§Example
let mut writer = Writer::new();
writer.write_bytes(&[0, 0, 0, 42]);
let bytes = writer.into_bytes();
assert_eq!(bytes, vec![0, 0, 0, 4, 0, 0, 0, 42]);Sourcepub fn write_string(&mut self, val: &str)
pub fn write_string(&mut self, val: &str)
Writes a string value to the underlying byte sequence.
§Example
let mut writer = Writer::new();
writer.write_string("a test string");
let bytes = writer.into_bytes();
assert_eq!(bytes, [0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]);Sourcepub fn write_u64(&mut self, val: u64)
pub fn write_u64(&mut self, val: u64)
Writes a u64 value to the underlying byte sequence.
§Example
let mut writer = Writer::new();
writer.write_u64(0xFFFFFFFFFFFFFFFF);
let bytes = writer.into_bytes();
assert_eq!(bytes, [255, 255, 255, 255, 255, 255, 255, 255]);Sourcepub fn write_u32(&mut self, val: u32)
pub fn write_u32(&mut self, val: u32)
Writes a u32 value to the underlying byte sequence.
§Example
let mut writer = Writer::new();
writer.write_u32(0xFFFFFFFF);
let bytes = writer.into_bytes();
assert_eq!(bytes, [255, 255, 255, 255]);Sourcepub fn write_mpint(&mut self, val: &[u8])
pub fn write_mpint(&mut self, val: &[u8])
Writes an mpint value to the underlying byte sequence.
If the MSB bit of the first byte is set then the number is
negative, otherwise it is positive.
Positive numbers must be preceeded by a leading zero byte according to RFC 4251, section 5.
§Example
let mut writer = Writer::new();
writer.write_mpint(&[1, 0, 1]);
let bytes = writer.into_bytes();
assert_eq!(bytes, [0, 0, 0, 3, 1, 0, 1]);Sourcepub fn write_string_vec(&mut self, vec: &[String])
pub fn write_string_vec(&mut self, vec: &[String])
Writes a Vec<String> to the underlying byte sequence.
§Example
let mut writer = Writer::new();
writer.write_string_vec(&vec![String::from("Test"), String::from("Test")]);
let bytes = writer.into_bytes();
assert_eq!(bytes, [0, 0, 0, 16, 0, 0, 0, 4, 84, 101, 115, 116, 0, 0, 0, 4, 84, 101, 115, 116]);Sourcepub fn write_string_map(&mut self, map: &HashMap<String, String>)
pub fn write_string_map(&mut self, map: &HashMap<String, String>)
Writes a HashMap<String, String> to the underlying byte sequence.
§Example
let mut writer = Writer::new();
let mut example_map = HashMap::new();
example_map.insert(String::from("Test"), String::from(""));
writer.write_string_map(&example_map);
let bytes = writer.into_bytes();
assert_eq!(bytes, [0, 0, 0, 12, 0, 0, 0, 4, 84, 101, 115, 116, 0, 0, 0, 0]);Sourcepub fn write_pub_key(&mut self, key: &PublicKey)
pub fn write_pub_key(&mut self, key: &PublicKey)
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Converts the Writer into a byte sequence.
This consumes the underlying byte sequence used by the Writer.
§Example
let mut writer = Writer::new();
writer.write_string("some data");
let bytes = writer.into_bytes();
assert_eq!(bytes, [0, 0, 0, 9, 115, 111, 109, 101, 32, 100, 97, 116, 97]);Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Converts the Writer into a byte sequence.
This consumes the underlying byte sequence used by the Writer.
§Example
let mut writer = Writer::new();
writer.write_string("some data");
let bytes = writer.into_bytes();
assert_eq!(bytes, [0, 0, 0, 9, 115, 111, 109, 101, 32, 100, 97, 116, 97]);