ssh_key

Struct Mpint

source
pub struct Mpint { /* private fields */ }
Available on crate feature alloc only.
Expand description

Multiple precision integer, a.k.a. “mpint”.

This type is used for representing the big integer components of DSA and RSA keys.

Described in RFC4251 § 5:

Represents multiple precision integers in two’s complement format, stored as a string, 8 bits per byte, MSB first. Negative numbers have the value 1 as the most significant bit of the first byte of the data partition. If the most significant bit would be set for a positive number, the number MUST be preceded by a zero byte. Unnecessary leading bytes with the value 0 or 255 MUST NOT be included. The value zero MUST be stored as a string with zero bytes of data.

By convention, a number that is used in modular computations in Z_n SHOULD be represented in the range 0 <= x < n.

§Examples

value (hex)representation (hex)
000 00 00 00
9a378f9b2e332a700 00 00 08 09 a3 78 f9 b2 e3 32 a7
8000 00 00 02 00 80
-123400 00 00 02 ed cc
-deadbeef00 00 00 05 ff 21 52 41 11

Implementations§

source§

impl Mpint

source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Create a new multiple precision integer from the given big endian-encoded byte slice.

Note that this method expects a leading zero on positive integers whose MSB is set, but does NOT expect a 4-byte length prefix.

source

pub fn from_positive_bytes(bytes: &[u8]) -> Result<Self>

Create a new multiple precision integer from the given big endian encoded byte slice representing a positive integer.

The input may begin with leading zeros, which will be stripped when converted to Mpint encoding.

source

pub fn as_bytes(&self) -> &[u8]

Get the big integer data encoded as big endian bytes.

This slice will contain a leading zero if the value is positive but the MSB is also set. Use Mpint::as_positive_bytes to ensure the number is positive and strip the leading zero byte if it exists.

source

pub fn as_positive_bytes(&self) -> Option<&[u8]>

Get the bytes of a positive integer.

§Returns
  • Some(bytes) if the number is positive. The leading zero byte will be stripped.
  • None if the value is negative

Trait Implementations§

source§

impl AsRef<[u8]> for Mpint

source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for Mpint

source§

fn clone(&self) -> Mpint

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl ConstantTimeEq for Mpint

source§

fn ct_eq(&self, other: &Self) -> Choice

Determine if two items are equal. Read more
source§

fn ct_ne(&self, other: &Self) -> Choice

Determine if two items are NOT equal. Read more
source§

impl Debug for Mpint

source§

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

Formats the value using the given formatter. Read more
source§

impl Decode for Mpint

source§

type Error = Error

Type returned in the event of a decoding error.
source§

fn decode(reader: &mut impl Reader) -> Result<Self>

Attempt to decode a value of this type using the provided Reader.
source§

impl Display for Mpint

source§

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

Formats the value using the given formatter. Read more
source§

impl Encode for Mpint

source§

fn encoded_len(&self) -> Result<usize>

Get the length of this type encoded in bytes, prior to Base64 encoding.
source§

fn encode(&self, writer: &mut impl Writer) -> Result<()>

Encode this value using the provided Writer.
source§

fn encoded_len_prefixed(&self) -> Result<usize, Error>

Return the length of this type after encoding when prepended with a uint32 length prefix.
source§

fn encode_prefixed(&self, writer: &mut impl Writer) -> Result<(), Error>

Encode this value, first prepending a uint32 length prefix set to Encode::encoded_len.
source§

impl LowerHex for Mpint

source§

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

Formats the value using the given formatter. Read more
source§

impl Ord for Mpint

source§

fn cmp(&self, other: &Mpint) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Mpint

source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Mpint

source§

fn partial_cmp(&self, other: &Mpint) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl TryFrom<&[u8]> for Mpint

source§

type Error = Error

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

fn try_from(bytes: &[u8]) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<&BigUint> for Mpint

Available on crate features dsa or rsa only.
source§

type Error = Error

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

fn try_from(uint: &BigUint) -> Result<Mpint>

Performs the conversion.
source§

impl TryFrom<&Mpint> for BigUint

Available on crate features dsa or rsa only.
source§

type Error = Error

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

fn try_from(mpint: &Mpint) -> Result<BigUint>

Performs the conversion.
source§

impl TryFrom<BigUint> for Mpint

Available on crate features dsa or rsa only.
source§

type Error = Error

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

fn try_from(uint: BigUint) -> Result<Mpint>

Performs the conversion.
source§

impl TryFrom<Box<[u8]>> for Mpint

source§

type Error = Error

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

fn try_from(bytes: Box<[u8]>) -> Result<Self>

Performs the conversion.
source§

impl TryFrom<Mpint> for BigUint

Available on crate features dsa or rsa only.
source§

type Error = Error

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

fn try_from(mpint: Mpint) -> Result<BigUint>

Performs the conversion.
source§

impl UpperHex for Mpint

source§

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

Formats the value using the given formatter. Read more
source§

impl Zeroize for Mpint

source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
source§

impl Eq for Mpint

Auto Trait Implementations§

§

impl Freeze for Mpint

§

impl RefUnwindSafe for Mpint

§

impl Send for Mpint

§

impl Sync for Mpint

§

impl Unpin for Mpint

§

impl UnwindSafe for Mpint

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V