2012-07-18 18:59:03 +00:00
|
|
|
#pragma once
|
2003-06-15 13:41:32 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
#include "types.hh"
|
2007-02-21 14:31:42 +00:00
|
|
|
#include "serialise.hh"
|
2003-06-15 13:41:32 +00:00
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
2003-06-15 13:41:32 +00:00
|
|
|
|
|
|
|
|
2016-04-20 12:12:38 +00:00
|
|
|
MakeError(BadHash, Error);
|
|
|
|
|
|
|
|
|
2020-06-25 13:50:30 +00:00
|
|
|
enum HashType : char { htMD5 = 42, htSHA1, htSHA256, htSHA512 };
|
2005-01-13 15:44:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
const int md5HashSize = 16;
|
|
|
|
const int sha1HashSize = 20;
|
2005-01-14 12:03:04 +00:00
|
|
|
const int sha256HashSize = 32;
|
2015-11-04 15:31:06 +00:00
|
|
|
const int sha512HashSize = 64;
|
2005-01-13 15:44:44 +00:00
|
|
|
|
2020-05-10 18:32:21 +00:00
|
|
|
extern std::set<std::string> hashTypes;
|
|
|
|
|
2005-11-16 08:27:06 +00:00
|
|
|
extern const string base32Chars;
|
|
|
|
|
2020-06-18 22:09:22 +00:00
|
|
|
enum Base : int { Base64, Base32, Base16, SRI };
|
2017-07-04 12:47:59 +00:00
|
|
|
|
2005-01-13 15:44:44 +00:00
|
|
|
|
2003-06-15 13:41:32 +00:00
|
|
|
struct Hash
|
|
|
|
{
|
2020-06-19 18:41:33 +00:00
|
|
|
constexpr static size_t maxHashSize = 64;
|
|
|
|
size_t hashSize = 0;
|
|
|
|
uint8_t hash[maxHashSize] = {};
|
2005-01-13 15:44:44 +00:00
|
|
|
|
2020-06-19 18:41:33 +00:00
|
|
|
HashType type;
|
2005-01-14 16:04:03 +00:00
|
|
|
|
|
|
|
/* Create a zero-filled hash object. */
|
2020-07-01 22:03:22 +00:00
|
|
|
Hash(HashType type);
|
2017-07-04 12:47:59 +00:00
|
|
|
|
2020-08-01 15:32:20 +00:00
|
|
|
/* Parse the hash from a string representation in the format
|
2018-12-13 13:30:52 +00:00
|
|
|
"[<type>:]<base16|base32|base64>" or "<type>-<base64>" (a
|
|
|
|
Subresource Integrity hash expression). If the 'type' argument
|
2020-06-02 15:52:13 +00:00
|
|
|
is not present, then the hash type must be specified in the
|
2017-07-04 12:47:59 +00:00
|
|
|
string. */
|
2020-07-01 22:34:18 +00:00
|
|
|
static Hash parseAny(std::string_view s, std::optional<HashType> type);
|
2020-08-01 15:32:20 +00:00
|
|
|
|
|
|
|
/* Parse a hash from a string representation like the above, except the
|
|
|
|
type prefix is mandatory is there is no separate arguement. */
|
2020-07-01 22:34:18 +00:00
|
|
|
static Hash parseAnyPrefixed(std::string_view s);
|
2020-08-01 15:32:20 +00:00
|
|
|
|
|
|
|
/* Parse a plain hash that musst not have any prefix indicating the type.
|
|
|
|
The type is passed in to disambiguate. */
|
2020-07-02 15:34:40 +00:00
|
|
|
static Hash parseNonSRIUnprefixed(std::string_view s, HashType type);
|
2017-07-04 12:47:59 +00:00
|
|
|
|
2020-07-02 15:11:18 +00:00
|
|
|
static Hash parseSRI(std::string_view original);
|
2020-07-01 21:32:06 +00:00
|
|
|
|
2020-06-30 18:10:30 +00:00
|
|
|
private:
|
2020-08-01 15:32:20 +00:00
|
|
|
/* The type must be provided, the string view must not include <type>
|
|
|
|
prefix. `isSRI` helps disambigate the various base-* encodings. */
|
2020-07-02 15:09:04 +00:00
|
|
|
Hash(std::string_view s, HashType type, bool isSRI);
|
2020-06-30 18:10:30 +00:00
|
|
|
|
|
|
|
public:
|
2016-04-20 12:12:38 +00:00
|
|
|
/* Check whether a hash is set. */
|
2020-06-02 15:52:13 +00:00
|
|
|
operator bool () const { return (bool) type; }
|
2016-04-20 12:12:38 +00:00
|
|
|
|
2003-06-16 14:19:32 +00:00
|
|
|
/* Check whether two hash are equal. */
|
2003-07-15 22:28:27 +00:00
|
|
|
bool operator == (const Hash & h2) const;
|
2003-06-16 14:19:32 +00:00
|
|
|
|
|
|
|
/* Check whether two hash are not equal. */
|
2003-07-15 22:28:27 +00:00
|
|
|
bool operator != (const Hash & h2) const;
|
2003-06-16 14:19:32 +00:00
|
|
|
|
2003-07-15 21:24:05 +00:00
|
|
|
/* For sorting. */
|
|
|
|
bool operator < (const Hash & h) const;
|
2016-01-27 16:18:20 +00:00
|
|
|
|
|
|
|
/* Returns the length of a base-16 representation of this hash. */
|
|
|
|
size_t base16Len() const
|
|
|
|
{
|
|
|
|
return hashSize * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the length of a base-32 representation of this hash. */
|
|
|
|
size_t base32Len() const
|
|
|
|
{
|
|
|
|
return (hashSize * 8 - 1) / 5 + 1;
|
|
|
|
}
|
2016-04-20 12:12:38 +00:00
|
|
|
|
2017-07-04 12:47:59 +00:00
|
|
|
/* Returns the length of a base-64 representation of this hash. */
|
|
|
|
size_t base64Len() const
|
|
|
|
{
|
|
|
|
return ((4 * hashSize / 3) + 3) & ~3;
|
|
|
|
}
|
2016-04-20 12:12:38 +00:00
|
|
|
|
2017-07-04 12:47:59 +00:00
|
|
|
/* Return a string representation of the hash, in base-16, base-32
|
|
|
|
or base-64. By default, this is prefixed by the hash type
|
|
|
|
(e.g. "sha256:"). */
|
2020-06-03 10:38:23 +00:00
|
|
|
std::string to_string(Base base, bool includeType) const;
|
2020-03-24 13:26:13 +00:00
|
|
|
|
|
|
|
std::string gitRev() const
|
|
|
|
{
|
2020-06-18 22:09:22 +00:00
|
|
|
assert(type == htSHA1);
|
|
|
|
return to_string(Base16, false);
|
2020-03-24 13:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string gitShortRev() const
|
|
|
|
{
|
2020-06-18 22:09:22 +00:00
|
|
|
assert(type == htSHA1);
|
|
|
|
return std::string(to_string(Base16, false), 0, 7);
|
2020-03-24 13:26:13 +00:00
|
|
|
}
|
2017-07-04 12:47:59 +00:00
|
|
|
};
|
2005-01-14 16:04:03 +00:00
|
|
|
|
2020-06-12 15:09:42 +00:00
|
|
|
/* Helper that defaults empty hashes to the 0 hash. */
|
2020-06-18 21:58:27 +00:00
|
|
|
Hash newHashAllowEmpty(std::string hashStr, std::optional<HashType> ht);
|
2005-01-14 16:04:03 +00:00
|
|
|
|
2012-10-23 16:05:50 +00:00
|
|
|
/* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
|
|
|
|
string printHash16or32(const Hash & hash);
|
|
|
|
|
2003-06-16 14:19:32 +00:00
|
|
|
/* Compute the hash of the given string. */
|
2020-06-19 18:41:33 +00:00
|
|
|
Hash hashString(HashType ht, std::string_view s);
|
2003-06-16 14:19:32 +00:00
|
|
|
|
|
|
|
/* Compute the hash of the given file. */
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
Hash hashFile(HashType ht, const Path & path);
|
2003-06-15 13:41:32 +00:00
|
|
|
|
2003-06-16 14:19:32 +00:00
|
|
|
/* Compute the hash of the given path. The hash is defined as
|
2006-02-01 16:48:49 +00:00
|
|
|
(essentially) hashString(ht, dumpPath(path)). */
|
2020-07-30 11:10:49 +00:00
|
|
|
typedef std::pair<Hash, uint64_t> HashResult;
|
2010-11-16 17:11:46 +00:00
|
|
|
HashResult hashPath(HashType ht, const Path & path,
|
2006-12-12 23:05:01 +00:00
|
|
|
PathFilter & filter = defaultPathFilter);
|
2003-06-16 15:59:23 +00:00
|
|
|
|
2005-01-14 16:04:03 +00:00
|
|
|
/* Compress a hash to the specified number of bytes by cyclically
|
|
|
|
XORing bytes together. */
|
|
|
|
Hash compressHash(const Hash & hash, unsigned int newSize);
|
|
|
|
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
/* Parse a string representing a hash type. */
|
2020-06-19 18:41:33 +00:00
|
|
|
HashType parseHashType(std::string_view s);
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
|
2020-06-02 15:52:13 +00:00
|
|
|
/* Will return nothing on parse error */
|
2020-06-19 18:41:33 +00:00
|
|
|
std::optional<HashType> parseHashTypeOpt(std::string_view s);
|
* Removed the `id' attribute hack.
* Formalise the notion of fixed-output derivations, i.e., derivations
for which a cryptographic hash of the output is known in advance.
Changes to such derivations should not propagate upwards through the
dependency graph. Previously this was done by specifying the hash
component of the output path through the `id' attribute, but this is
insecure since you can lie about it (i.e., you can specify any hash
and then produce a completely different output). Now the
responsibility for checking the output is moved from the builder to
Nix itself.
A fixed-output derivation can be created by specifying the
`outputHash' and `outputHashAlgo' attributes, the latter taking
values `md5', `sha1', and `sha256', and the former specifying the
actual hash in hexadecimal or in base-32 (auto-detected by looking
at the length of the attribute value). MD5 is included for
compatibility but should be considered deprecated.
* Removed the `drvPath' pseudo-attribute in derivation results. It's
no longer necessary.
* Cleaned up the support for multiple output paths in derivation store
expressions. Each output now has a unique identifier (e.g., `out',
`devel', `docs'). Previously there was no way to tell output paths
apart at the store expression level.
* `nix-hash' now has a flag `--base32' to specify that the hash should
be printed in base-32 notation.
* `fetchurl' accepts parameters `sha256' and `sha1' in addition to
`md5'.
* `nix-prefetch-url' now prints out a SHA-1 hash in base-32. (TODO: a
flag to specify the hash.)
2005-01-17 16:55:19 +00:00
|
|
|
|
2008-12-03 16:10:17 +00:00
|
|
|
/* And the reverse. */
|
|
|
|
string printHashType(HashType ht);
|
|
|
|
|
2007-02-21 14:31:42 +00:00
|
|
|
|
2008-05-21 11:17:31 +00:00
|
|
|
union Ctx;
|
2007-02-21 14:31:42 +00:00
|
|
|
|
2018-03-29 22:56:13 +00:00
|
|
|
struct AbstractHashSink : virtual Sink
|
|
|
|
{
|
|
|
|
virtual HashResult finish() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HashSink : public BufferedSink, public AbstractHashSink
|
2007-02-21 14:31:42 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
HashType ht;
|
|
|
|
Ctx * ctx;
|
2020-07-30 11:10:49 +00:00
|
|
|
uint64_t bytes;
|
2007-02-21 14:31:42 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
HashSink(HashType ht);
|
2010-03-09 14:32:03 +00:00
|
|
|
HashSink(const HashSink & h);
|
2007-02-21 14:31:42 +00:00
|
|
|
~HashSink();
|
2018-03-29 22:56:13 +00:00
|
|
|
void write(const unsigned char * data, size_t len) override;
|
|
|
|
HashResult finish() override;
|
2011-12-15 16:19:53 +00:00
|
|
|
HashResult currentHash();
|
2007-02-21 14:31:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|