internet_checksum

Struct Checksum

Source
pub struct Checksum { /* private fields */ }
Expand description

RFC 1071 “internet checksum” computation.

Checksum implements the “internet checksum” defined in RFC 1071 and updated in RFC 1141 and RFC 1624, which is used by many different protocols’ packet formats. The checksum operates by computing the 1s complement of the 1s complement sum of successive 16-bit words of the input.

Implementations§

Source§

impl Checksum

Source

pub const fn new() -> Self

Initialize a new checksum.

Source

pub fn add_bytes(&mut self, bytes: &[u8])

Add bytes to the checksum.

If bytes does not contain an even number of bytes, a single zero byte will be added to the end before updating the checksum.

Note that add_bytes has some fixed overhead regardless of the size of bytes. Where performance is a concern, prefer fewer calls to add_bytes with larger input over more calls with smaller input.

Source

pub fn checksum(&self) -> [u8; 2]

Computes the checksum, and returns the array representation.

checksum returns the checksum of all data added using add_bytes so far. Calling checksum does not reset the checksum. More bytes may be added after calling checksum, and they will be added to the checksum as expected.

If an odd number of bytes have been added so far, the checksum will be computed as though a single 0 byte had been added at the end in order to even out the length of the input.

Trait Implementations§

Source§

impl Default for Checksum

Source§

fn default() -> Checksum

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.