Enum NixHash

Source
pub enum NixHash {
    Md5([u8; 16]),
    Sha1([u8; 20]),
    Sha256([u8; 32]),
    Sha512(Box<[u8; 64]>),
}
Expand description

NixHash represents hashes known by Nix (md5/sha1/sha256/sha512).

Internally, these are represented as an enum of 4 kinds (the latter being boxed for size reasons, as we rarely use sha512, having a pointer there is fine).

There’s Self::algo and Self::digest_as_bytes accessors, as well as a Self::from_algo_and_digest constructor.

A few methods to parse (from_$format_$encoding) and emit (to_$format_$encoding) various formats and encodings Nix uses.

§Formats

The following formats exist:

§Nix Format

Lowercase algo, followed by a colon, then the digest.

§SRI Format

Uses the lowercase algo, followed by a -, then the digest (base64-encoded). This is also used in the Display implementation.

Contrary to the SRI spec, Nix doesn’t have an understanding of passing multiple hashes (with different algos) in SRI hashes. It instead simply cuts everything off after the expected length for the specified algo, and tries to parse the rest in permissive base64 (allowing missing padding).

§Digest only

It’s possible to not specify the algo at all. In that case, the expected NixHash algo MUST be provided externally.

§Encodings

For “Nix” and “Digest only” formats, the following encodings are supported:

  • lowerhex,
  • nixbase32,
  • base64 (StdEncoding)

Variants§

§

Md5([u8; 16])

§

Sha1([u8; 20])

§

Sha256([u8; 32])

§

Sha512(Box<[u8; 64]>)

Implementations§

Source§

impl NixHash

Source

pub fn algo(&self) -> HashAlgo

returns the algo as HashAlgo.

Source

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

returns the digest as variable-length byte slice.

Source

pub fn from_algo_and_digest( algo: HashAlgo, digest: &[u8], ) -> Result<NixHash, Error>

Constructs a new NixHash by specifying HashAlgo and digest. It can fail if the passed digest length doesn’t match what’s expected for the passed algo.

Source

pub fn from_nix_nixbase32(s: &str) -> Option<Self>

Constructs a new NixHash from the Nix default hash format, the inverse of Self::to_nix_nixbase32.

Source

pub fn to_nix_nixbase32(&self) -> String

Formats a NixHash in the Nix nixbase32 format.

Source

pub fn from_sri(s: &str) -> Result<NixHash, Error>

Parses a Nix SRI string to a NixHash. (See caveats in Self on the deviations from the SRI spec)

Source

pub fn write_sri_str(&self, w: &mut impl Write) -> Result<(), Error>

Writes a NixHash in SRI format to a std::fmt::Write.

Source

pub fn to_sri_string(&self) -> String

Formats a NixHash to an SRI string.

Source

pub fn to_nix_lowerhex_string(&self) -> String

Formats a NixHash in the Nix lowerhex format.

Source

pub fn from_str(s: &str, want_algo: Option<HashAlgo>) -> Result<NixHash, Error>

This parses all known output formats for NixHash. See NixHash for a list. An optional algo needs to be provided, which is mandatory to be specified if the “digest only” format is used. In other cases, consistency of an optionally externally configured algo with the one parsed is ensured.

Trait Implementations§

Source§

impl Clone for NixHash

Source§

fn clone(&self) -> NixHash

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 Debug for NixHash

Source§

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

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

impl<'de> Deserialize<'de> for NixHash

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for NixHash

Source§

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

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

impl Ord for NixHash

Same order as sorting the corresponding nixbase32 strings.

This order is used in the ATerm serialization of a derivation and thus affects the calculated output hash.

Source§

fn cmp(&self, other: &NixHash) -> 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 NixHash

Source§

fn eq(&self, other: &NixHash) -> 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 NixHash

Source§

fn partial_cmp(&self, other: &NixHash) -> 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 Serialize for NixHash

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for NixHash

Source§

impl StructuralPartialEq for NixHash

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

Source§

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

🔬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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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§

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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T