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§
Implementations§
Source§impl NixHash
impl NixHash
Sourcepub fn digest_as_bytes(&self) -> &[u8] ⓘ
pub fn digest_as_bytes(&self) -> &[u8] ⓘ
returns the digest as variable-length byte slice.
Sourcepub fn from_nix_nixbase32(s: &str) -> Option<Self>
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.
Sourcepub fn to_nix_nixbase32(&self) -> String
pub fn to_nix_nixbase32(&self) -> String
Formats a NixHash in the Nix nixbase32 format.
Sourcepub fn from_sri(s: &str) -> Result<NixHash, Error>
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)
Sourcepub fn write_sri_str(&self, w: &mut impl Write) -> Result<(), Error>
pub fn write_sri_str(&self, w: &mut impl Write) -> Result<(), Error>
Writes a NixHash in SRI format to a std::fmt::Write.
Sourcepub fn to_sri_string(&self) -> String
pub fn to_sri_string(&self) -> String
Formats a NixHash to an SRI string.
Sourcepub fn to_nix_lowerhex_string(&self) -> String
pub fn to_nix_lowerhex_string(&self) -> String
Formats a NixHash in the Nix lowerhex format.
Sourcepub fn from_str(s: &str, want_algo: Option<HashAlgo>) -> Result<NixHash, Error>
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<'de> Deserialize<'de> for NixHash
impl<'de> Deserialize<'de> for NixHash
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Ord for NixHash
Same order as sorting the corresponding nixbase32 strings.
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.