Struct internet_checksum::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>,

§

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.