Struct sshkeys::Reader

source ·
pub struct Reader<'a> { /* private fields */ }
Expand description

A Reader is used for reading from a byte sequence representing an encoded OpenSSH public key or certificate.

Implementations§

source§

impl<'a> Reader<'a>

source

pub fn new<T: ?Sized + AsRef<[u8]>>(inner: &T) -> Reader<'_>

Creates a new Reader instance from the given byte sequence.

§Example
let data = vec![0, 0, 0, 42];
let mut reader = sshkeys::Reader::new(&data);
let num = reader.read_u32().unwrap();
assert_eq!(num, 42);
source

pub fn set_offset(&mut self, offset: usize) -> Result<()>

Sets the Reader current offset to a given position.

§Example
let data = vec![0, 0, 0, 42];
let mut reader = sshkeys::Reader::new(&data);
let num = reader.read_u32().unwrap();
assert_eq!(num, 42);
reader.set_offset(0);
let num_42_again = reader.read_u32().unwrap();
assert_eq!(num_42_again, 42);
source

pub fn read_bytes(&mut self) -> Result<Vec<u8>>

Reads a byte buffer from the wrapped byte sequence and returns it as a Vec<u8>. The buffer is represented by it’s length as u32 value followed by the actual bytes to read.

§Example
let data = vec![0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103];
let mut reader = sshkeys::Reader::new(&data);
let bytes = reader.read_bytes().unwrap();
assert_eq!(bytes, [97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103]);
source

pub fn read_mpint(&mut self) -> Result<Vec<u8>>

Reads an mpint value from the wrapped byte sequence.

Drops the leading byte if it’s value is zero according to the RFC 4251, section 5.

§Example
let data = vec![0, 0, 0, 3, 1, 0, 1];
let mut reader = sshkeys::Reader::new(&data);
let mpint = reader.read_mpint().unwrap();
assert_eq!(mpint, [1, 0, 1]);
source

pub fn read_string(&mut self) -> Result<String>

Reads a string value from the wrapped byte sequence and returns it as a String. The value that we read should be a valid UTF-8.

§Example
let data = vec![0, 0, 0, 13, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103];
let mut reader = sshkeys::Reader::new(&data);
let result = reader.read_string().unwrap();
assert_eq!(result, "a test string");
source

pub fn read_u32(&mut self) -> Result<u32>

Reads an u32 value from the wrapped byte sequence and returns it.

§Example
let data = vec![0, 0, 0, 42];
let mut reader = sshkeys::Reader::new(&data);
let num = reader.read_u32().unwrap();
assert_eq!(num, 42);
source

pub fn read_u64(&mut self) -> Result<u64>

Reads an u64 value from the wrapped byte sequence and returns it.

§Example
let data = vec![0, 0, 0, 0, 0, 0, 0, 42];
let mut reader = sshkeys::Reader::new(&data);
let num = reader.read_u64().unwrap();
assert_eq!(num, 42);

Trait Implementations§

source§

impl<'a> Debug for Reader<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.