2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2003-06-15 16:41:32 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "types.hh"
|
2007-02-21 16:31:42 +02:00
|
|
|
#include "serialise.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "file-system.hh"
|
2003-06-15 16:41:32 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
namespace nix {
|
2003-06-15 16:41:32 +03:00
|
|
|
|
|
|
|
|
2016-04-20 15:12:38 +03:00
|
|
|
MakeError(BadHash, Error);
|
|
|
|
|
|
|
|
|
2023-11-28 15:20:27 +02:00
|
|
|
enum struct HashAlgorithm : char { MD5 = 42, SHA1, SHA256, SHA512 };
|
2005-01-13 17:44:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
const int md5HashSize = 16;
|
|
|
|
const int sha1HashSize = 20;
|
2005-01-14 14:03:04 +02:00
|
|
|
const int sha256HashSize = 32;
|
2015-11-04 17:31:06 +02:00
|
|
|
const int sha512HashSize = 64;
|
2005-01-13 17:44:44 +02:00
|
|
|
|
2023-11-28 16:38:15 +02:00
|
|
|
extern const std::set<std::string> hashAlgorithms;
|
2020-05-10 21:32:21 +03:00
|
|
|
|
2023-11-28 16:38:15 +02:00
|
|
|
extern const std::string nix32Chars;
|
2005-11-16 10:27:06 +02:00
|
|
|
|
2023-10-18 12:08:06 +03:00
|
|
|
/**
|
|
|
|
* @brief Enumeration representing the hash formats.
|
|
|
|
*/
|
2023-10-13 13:48:36 +03:00
|
|
|
enum struct HashFormat : int {
|
2023-10-18 12:08:06 +03:00
|
|
|
/// @brief Base 64 encoding.
|
|
|
|
/// @see [IETF RFC 4648, section 4](https://datatracker.ietf.org/doc/html/rfc4648#section-4).
|
|
|
|
Base64,
|
2023-11-28 16:38:15 +02:00
|
|
|
/// @brief Nix-specific base-32 encoding. @see nix32Chars
|
|
|
|
Nix32,
|
2023-10-18 12:08:06 +03:00
|
|
|
/// @brief Lowercase hexadecimal encoding. @see base16Chars
|
|
|
|
Base16,
|
|
|
|
/// @brief "<hash algo>:<Base 64 hash>", format of the SRI integrity attribute.
|
|
|
|
/// @see W3C recommendation [Subresource Intergrity](https://www.w3.org/TR/SRI/).
|
|
|
|
SRI
|
|
|
|
};
|
2017-07-04 15:47:59 +03:00
|
|
|
|
2023-11-28 16:38:15 +02:00
|
|
|
extern const std::set<std::string> hashFormats;
|
|
|
|
|
2003-06-15 16:41:32 +03:00
|
|
|
struct Hash
|
|
|
|
{
|
2020-06-19 21:41:33 +03:00
|
|
|
constexpr static size_t maxHashSize = 64;
|
|
|
|
size_t hashSize = 0;
|
|
|
|
uint8_t hash[maxHashSize] = {};
|
2005-01-13 17:44:44 +02:00
|
|
|
|
2023-11-28 15:20:27 +02:00
|
|
|
HashAlgorithm algo;
|
2005-01-14 18:04:03 +02:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Create a zero-filled hash object.
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
explicit Hash(HashAlgorithm algo);
|
2017-07-04 15:47:59 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Parse the hash from a string representation in the format
|
|
|
|
* "[<type>:]<base16|base32|base64>" or "<type>-<base64>" (a
|
|
|
|
* Subresource Integrity hash expression). If the 'type' argument
|
|
|
|
* is not present, then the hash type must be specified in the
|
|
|
|
* string.
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
static Hash parseAny(std::string_view s, std::optional<HashAlgorithm> optAlgo);
|
2020-08-01 18:32:20 +03:00
|
|
|
|
2023-03-27 04:12:25 +03: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-02 01:34:18 +03:00
|
|
|
static Hash parseAnyPrefixed(std::string_view s);
|
2020-08-01 18:32:20 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Parse a plain hash that musst not have any prefix indicating the type.
|
|
|
|
* The type is passed in to disambiguate.
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
static Hash parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo);
|
2017-07-04 15:47:59 +03:00
|
|
|
|
2020-07-02 18:11:18 +03:00
|
|
|
static Hash parseSRI(std::string_view original);
|
2020-07-02 00:32:06 +03:00
|
|
|
|
2020-06-30 21:10:30 +03:00
|
|
|
private:
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* The type must be provided, the string view must not include <type>
|
|
|
|
* prefix. `isSRI` helps disambigate the various base-* encodings.
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
Hash(std::string_view s, HashAlgorithm algo, bool isSRI);
|
2020-06-30 21:10:30 +03:00
|
|
|
|
|
|
|
public:
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Check whether two hash are equal.
|
|
|
|
*/
|
2003-07-16 01:28:27 +03:00
|
|
|
bool operator == (const Hash & h2) const;
|
2003-06-16 17:19:32 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Check whether two hash are not equal.
|
|
|
|
*/
|
2003-07-16 01:28:27 +03:00
|
|
|
bool operator != (const Hash & h2) const;
|
2003-06-16 17:19:32 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* For sorting.
|
|
|
|
*/
|
2003-07-16 00:24:05 +03:00
|
|
|
bool operator < (const Hash & h) const;
|
2016-01-27 18:18:20 +02:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Returns the length of a base-16 representation of this hash.
|
|
|
|
*/
|
2023-11-25 18:33:44 +02:00
|
|
|
[[nodiscard]] size_t base16Len() const
|
2016-01-27 18:18:20 +02:00
|
|
|
{
|
|
|
|
return hashSize * 2;
|
|
|
|
}
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Returns the length of a base-32 representation of this hash.
|
|
|
|
*/
|
2023-11-25 18:33:44 +02:00
|
|
|
[[nodiscard]] size_t base32Len() const
|
2016-01-27 18:18:20 +02:00
|
|
|
{
|
|
|
|
return (hashSize * 8 - 1) / 5 + 1;
|
|
|
|
}
|
2016-04-20 15:12:38 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Returns the length of a base-64 representation of this hash.
|
|
|
|
*/
|
2023-11-25 18:33:44 +02:00
|
|
|
[[nodiscard]] size_t base64Len() const
|
2017-07-04 15:47:59 +03:00
|
|
|
{
|
|
|
|
return ((4 * hashSize / 3) + 3) & ~3;
|
|
|
|
}
|
2016-04-20 15:12:38 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Return a string representation of the hash, in base-16, base-32
|
2023-11-28 15:20:27 +02:00
|
|
|
* or base-64. By default, this is prefixed by the hash algo
|
2023-03-27 04:12:25 +03:00
|
|
|
* (e.g. "sha256:").
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
[[nodiscard]] std::string to_string(HashFormat hashFormat, bool includeAlgo) const;
|
2020-03-24 15:26:13 +02:00
|
|
|
|
2023-11-25 18:33:44 +02:00
|
|
|
[[nodiscard]] std::string gitRev() const
|
2020-03-24 15:26:13 +02:00
|
|
|
{
|
2023-10-13 04:48:15 +03:00
|
|
|
return to_string(HashFormat::Base16, false);
|
2020-03-24 15:26:13 +02:00
|
|
|
}
|
|
|
|
|
2023-11-25 18:33:44 +02:00
|
|
|
[[nodiscard]] std::string gitShortRev() const
|
2020-03-24 15:26:13 +02:00
|
|
|
{
|
2023-10-13 04:48:15 +03:00
|
|
|
return std::string(to_string(HashFormat::Base16, false), 0, 7);
|
2020-03-24 15:26:13 +02:00
|
|
|
}
|
2020-08-05 22:11:49 +03:00
|
|
|
|
|
|
|
static Hash dummy;
|
2024-01-03 22:02:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a random hash with hash algorithm `algo`
|
|
|
|
*/
|
|
|
|
static Hash random(HashAlgorithm algo);
|
2017-07-04 15:47:59 +03:00
|
|
|
};
|
2005-01-14 18:04:03 +02:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Helper that defaults empty hashes to the 0 hash.
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashAlgorithm> ha);
|
2005-01-14 18:04:03 +02:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Print a hash in base-16 if it's MD5, or base-32 otherwise.
|
|
|
|
*/
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string printHash16or32(const Hash & hash);
|
2012-10-23 19:05:50 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Compute the hash of the given string.
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
Hash hashString(HashAlgorithm ha, std::string_view s);
|
2003-06-16 17:19:32 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
2023-09-08 05:52:57 +03:00
|
|
|
* Compute the hash of the given file, hashing its contents directly.
|
|
|
|
*
|
|
|
|
* (Metadata, such as the executable permission bit, is ignored.)
|
2023-03-27 04:12:25 +03:00
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
Hash hashFile(HashAlgorithm ha, const Path & path);
|
2003-06-15 16:41:32 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
2023-11-04 22:25:41 +02:00
|
|
|
* The final hash and the number of bytes digested.
|
2023-09-08 05:52:57 +03:00
|
|
|
*
|
2023-11-04 22:25:41 +02:00
|
|
|
* @todo Convert to proper struct
|
2023-03-27 04:12:25 +03:00
|
|
|
*/
|
2020-07-30 14:10:49 +03:00
|
|
|
typedef std::pair<Hash, uint64_t> HashResult;
|
2003-06-16 18:59:23 +03:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Compress a hash to the specified number of bytes by cyclically
|
|
|
|
* XORing bytes together.
|
|
|
|
*/
|
2005-01-14 18:04:03 +02:00
|
|
|
Hash compressHash(const Hash & hash, unsigned int newSize);
|
|
|
|
|
2023-10-09 06:03:16 +03:00
|
|
|
/**
|
|
|
|
* Parse a string representing a hash format.
|
|
|
|
*/
|
|
|
|
HashFormat parseHashFormat(std::string_view hashFormatName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* std::optional version of parseHashFormat that doesn't throw error.
|
|
|
|
*/
|
|
|
|
std::optional<HashFormat> parseHashFormatOpt(std::string_view hashFormatName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The reverse of parseHashFormat.
|
|
|
|
*/
|
|
|
|
std::string_view printHashFormat(HashFormat hashFormat);
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Parse a string representing a hash type.
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
HashAlgorithm parseHashAlgo(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 18:55:19 +02:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* Will return nothing on parse error
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
std::optional<HashAlgorithm> parseHashAlgoOpt(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 18:55:19 +02:00
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
/**
|
|
|
|
* And the reverse.
|
|
|
|
*/
|
2023-11-28 15:20:27 +02:00
|
|
|
std::string_view printHashAlgo(HashAlgorithm ha);
|
2008-12-03 18:10:17 +02:00
|
|
|
|
2007-02-21 16:31:42 +02:00
|
|
|
|
2008-05-21 14:17:31 +03:00
|
|
|
union Ctx;
|
2007-02-21 16:31:42 +02:00
|
|
|
|
2018-03-30 01:56:13 +03:00
|
|
|
struct AbstractHashSink : virtual Sink
|
|
|
|
{
|
|
|
|
virtual HashResult finish() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HashSink : public BufferedSink, public AbstractHashSink
|
2007-02-21 16:31:42 +02:00
|
|
|
{
|
|
|
|
private:
|
2023-11-28 15:20:27 +02:00
|
|
|
HashAlgorithm ha;
|
2007-02-21 16:31:42 +02:00
|
|
|
Ctx * ctx;
|
2020-07-30 14:10:49 +03:00
|
|
|
uint64_t bytes;
|
2007-02-21 16:31:42 +02:00
|
|
|
|
|
|
|
public:
|
2023-11-28 15:20:27 +02:00
|
|
|
HashSink(HashAlgorithm ha);
|
2010-03-09 16:32:03 +02:00
|
|
|
HashSink(const HashSink & h);
|
2007-02-21 16:31:42 +02:00
|
|
|
~HashSink();
|
2023-04-07 10:16:40 +03:00
|
|
|
void writeUnbuffered(std::string_view data) override;
|
2018-03-30 01:56:13 +03:00
|
|
|
HashResult finish() override;
|
2011-12-15 18:19:53 +02:00
|
|
|
HashResult currentHash();
|
2007-02-21 16:31:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|