Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
hash.hh File Reference
#include "types.hh"
#include "serialise.hh"
#include "file-system.hh"

Go to the source code of this file.

Classes

struct  nix::Hash
 
struct  nix::AbstractHashSink
 
class  nix::HashSink
 

Typedefs

typedef std::pair< Hash, uint64_t > nix::HashResult
 

Enumerations

enum struct  HashAlgorithm : char { MD5 = 42 , SHA1 , SHA256 , SHA512 }
 
enum struct  nix::HashFormat : int { Base64 , Nix32 , Base16 , SRI }
 Enumeration representing the hash formats. More...
 

Functions

 nix::MakeError (BadHash, Error)
 
Hash nix::newHashAllowEmpty (std::string_view hashStr, std::optional< HashAlgorithm > ha)
 
std::string nix::printHash16or32 (const Hash &hash)
 
Hash nix::hashString (HashAlgorithm ha, std::string_view s)
 
Hash nix::hashFile (HashAlgorithm ha, const Path &path)
 
Hash nix::compressHash (const Hash &hash, unsigned int newSize)
 
HashFormat nix::parseHashFormat (std::string_view hashFormatName)
 
std::optional< HashFormatnix::parseHashFormatOpt (std::string_view hashFormatName)
 
std::string_view nix::printHashFormat (HashFormat HashFormat)
 
HashAlgorithm nix::parseHashAlgo (std::string_view s)
 
std::optional< HashAlgorithm > nix::parseHashAlgoOpt (std::string_view s)
 
std::string_view nix::printHashAlgo (HashAlgorithm ha)
 

Variables

const int nix::md5HashSize = 16
 
const int nix::sha1HashSize = 20
 
const int nix::sha256HashSize = 32
 
const int nix::sha512HashSize = 64
 

Typedef Documentation

◆ HashResult

typedef std::pair<Hash, uint64_t> nix::HashResult

The final hash and the number of bytes digested.

Todo
Convert to proper struct

Enumeration Type Documentation

◆ HashFormat

enum struct nix::HashFormat : int
strong

Enumeration representing the hash formats.

Enumerator
Base64 

Base 64 encoding.

See also
IETF RFC 4648, section 4.
Nix32 

Nix-specific base-32 encoding.

See also
nix32Chars
Base16 

Lowercase hexadecimal encoding.

See also
base16Chars
SRI 

"<hash algo>:<Base 64 hash>", format of the SRI integrity attribute.

See also
W3C recommendation Subresource Intergrity.

Function Documentation

◆ compressHash()

Hash nix::compressHash ( const Hash & hash,
unsigned int newSize )

Compress a hash to the specified number of bytes by cyclically XORing bytes together.

◆ hashFile()

Hash nix::hashFile ( HashAlgorithm ha,
const Path & path )

Compute the hash of the given file, hashing its contents directly.

(Metadata, such as the executable permission bit, is ignored.)

◆ hashString()

Hash nix::hashString ( HashAlgorithm ha,
std::string_view s )

Compute the hash of the given string.

◆ newHashAllowEmpty()

Hash nix::newHashAllowEmpty ( std::string_view hashStr,
std::optional< HashAlgorithm > ha )

Helper that defaults empty hashes to the 0 hash.

◆ parseHashAlgo()

HashAlgorithm nix::parseHashAlgo ( std::string_view s)

Parse a string representing a hash algorithm.

◆ parseHashAlgoOpt()

std::optional< HashAlgorithm > nix::parseHashAlgoOpt ( std::string_view s)

Will return nothing on parse error

◆ parseHashFormat()

HashFormat nix::parseHashFormat ( std::string_view hashFormatName)

Parse a string representing a hash format.

◆ parseHashFormatOpt()

std::optional< HashFormat > nix::parseHashFormatOpt ( std::string_view hashFormatName)

std::optional version of parseHashFormat that doesn't throw error.

◆ printHash16or32()

std::string nix::printHash16or32 ( const Hash & hash)

Print a hash in base-16 if it's MD5, or base-32 otherwise.

◆ printHashAlgo()

std::string_view nix::printHashAlgo ( HashAlgorithm ha)

And the reverse.

◆ printHashFormat()

std::string_view nix::printHashFormat ( HashFormat hashFormat)

The reverse of parseHashFormat.