aboutsummaryrefslogtreecommitdiff
#pragma once

#include <gcrypt.h>

#include "types.hh"
#include "serialise.hh"


namespace nix {


extern const string base32Chars;

typedef enum {
    htUnknown = 0,
    htMD5 = GCRY_MD_MD5,
    htSHA1 = GCRY_MD_SHA1,
    htSHA256 = GCRY_MD_SHA256,
    htSHA512 = GCRY_MD_SHA512,
    htSHA3_256 = GCRY_MD_SHA3_256,
    htSHA3_512 = GCRY_MD_SHA3_512,
    htBLAKE2s_256 = GCRY_MD_BLAKE2S_256
} HashType;

struct Hash
{
    static const unsigned int maxHashSize = 64;
    unsigned int hashSize;
    unsigned char hash[maxHashSize];

    HashType type;

    /* Create an unusable hash object. */
    Hash();

    /* Create a zero-filled hash object. */
    Hash(HashType type);

    /* Check whether two hash are equal. */
    bool operator == (const Hash & h2) const;

    /* Check whether two hash are not equal. */
    bool operator != (const Hash & h2) const;

    /* For sorting. */
    bool operator < (const Hash & h) const;
};


/* Convert a hash to a hexadecimal representation. */
string printHash(const Hash & hash);

/* Parse a hexadecimal representation of a hash code. */
Hash parseHash(HashType ht, const string & s);

/* Returns the length of a base-32 hash representation. */
unsigned int hashLength32(const Hash & hash);

/* Convert a hash to a base-32 representation. */
string printHash32(const Hash & hash);

/* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
string printHash16or32(const Hash & hash);

/* Parse a base-32 representation of a hash code. */
Hash parseHash32(HashType ht, const string & s);

/* Parse a base-16 or base-32 representation of a hash code. */
Hash parseHash16or32(HashType ht, const string & s);

/* Verify that the given string is a valid hash code. */
bool isHash(const string & s);

/* Compute the hash of the given string. */
Hash hashString(HashType ht, const string & s);

/* Compute the hash of the given file. */
Hash hashFile(HashType ht, const Path & path);

/* Compute the hash of the given path.  The hash is defined as
   (essentially) hashString(ht, dumpPath(path)). */
struct PathFilter;
extern PathFilter defaultPathFilter;
typedef std::pair<Hash, unsigned long long> HashResult;
HashResult hashPath(HashType ht, const Path & path,
    PathFilter & filter = defaultPathFilter);

/* Compress a hash to the specified number of bytes by cyclically
   XORing bytes together. */
Hash compressHash(const Hash & hash, unsigned int newSize);

/* Parse a string representing a hash type. */
HashType parseHashType(const string & s);

/* And the reverse. */
string printHashType(HashType ht);


struct Ctx;

class HashSink : public BufferedSink
{
private:
    HashType ht;
    Ctx * ctx;
    unsigned long long bytes;

public:
    HashSink(HashType ht);
    HashSink(const HashSink & h);
    ~HashSink();
    void write(const unsigned char * data, size_t len);
    HashResult finish();
    HashResult currentHash();
};


}
) and use it for pretty colored output....Ludovic Courtès 2018-06-01Add (gnu store database)....Caleb Ristvedt 2018-06-01gexp: Add 'with-extensions'....Ludovic Courtès 2018-01-08services: guix: Add 'log-compression' option....Ludovic Courtès 2017-12-01weather: Use (guix progress) for progress report....Ludovic Courtès 2017-05-18union: Gracefully handle dangling symlinks in the input....Ludovic Courtès 2017-05-04dir-locals.el: Add 'modify-phases' keywords....Alex Kost 2017-04-18Add (guix workers)....Ludovic Courtès 2017-01-28Add (guix memoization)....Ludovic Courtès kke 2020-03-26gnu: potrace: Update to 1.16....Marius Bakke 2020-03-26gnu: fontconfig: Build fix for the Hurd....Jan Nieuwenhuizen 2020-03-18gnu: fontforge: Update to 20200314....Marius Bakke 2020-03-14Merge branch 'master' into core-updatesMarius Bakke 2020-03-14gnu: libuninameslist: Update to 20200313....Marius Bakke 2020-03-11gnu: t1lib: Use HTTPS home page....Tobias Geerinckx-Rice 2020-03-10Merge branch 'master' into core-updatesMarius Bakke 2020-03-07gnu: woff2: Update to 1.0.2...Lars-Dominik Braun 2020-03-05gnu: libraqm: Parametrize version....Nicolas Goaziou 2020-03-05gnu: Add libraqm....Nicolas Goaziou 2020-03-04Merge branch 'master' into core-updatesMarius Bakke 2020-03-04gnu: Update pypi.python.org home pages & use HTTPS....Tobias Geerinckx-Rice 2020-03-04gnu: Add fntsample....Roel Janssen 2020-02-14Merge branch 'master' into core-updatesMarius Bakke 2020-02-12gnu: ttfautohint: Use HTTPS home page....Tobias Geerinckx-Rice 2020-01-24gnu: fontforge: Fix build with Python 3.8....Marius Bakke 2020-01-22gnu: Replace uses of 'libjpeg' with 'libjpeg-turbo'....Marius Bakke