2012-07-18 20:59:03 +02:00
|
|
|
#pragma once
|
2003-06-15 15:41:32 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
#include "types.hh"
|
2007-02-21 15:31:42 +01:00
|
|
|
#include "serialise.hh"
|
2003-06-15 15:41:32 +02:00
|
|
|
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
namespace nix {
|
2003-06-15 15:41:32 +02:00
|
|
|
|
|
|
|
|
2015-11-04 16:31:06 +01:00
|
|
|
typedef enum { htUnknown, htMD5, htSHA1, htSHA256, htSHA512 } HashType;
|
2005-01-13 16:44:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
const int md5HashSize = 16;
|
|
|
|
const int sha1HashSize = 20;
|
2005-01-14 13:03:04 +01:00
|
|
|
const int sha256HashSize = 32;
|
2015-11-04 16:31:06 +01:00
|
|
|
const int sha512HashSize = 64;
|
2005-01-13 16:44:44 +01:00
|
|
|
|
2005-11-16 09:27:06 +01:00
|
|
|
extern const string base32Chars;
|
|
|
|
|
2005-01-13 16:44:44 +01:00
|
|
|
|
2003-06-15 15:41:32 +02:00
|
|
|
struct Hash
|
|
|
|
{
|
2015-11-04 16:31:06 +01:00
|
|
|
static const unsigned int maxHashSize = 64;
|
2005-01-13 16:44:44 +01:00
|
|
|
unsigned int hashSize;
|
|
|
|
unsigned char hash[maxHashSize];
|
|
|
|
|
|
|
|
HashType type;
|
2003-06-15 15:41:32 +02:00
|
|
|
|
2005-01-14 17:04:03 +01:00
|
|
|
/* Create an unusable hash object. */
|
|
|
|
Hash();
|
|
|
|
|
|
|
|
/* Create a zero-filled hash object. */
|
2005-01-13 16:44:44 +01:00
|
|
|
Hash(HashType type);
|
2003-06-16 16:19:32 +02:00
|
|
|
|
|
|
|
/* Check whether two hash are equal. */
|
2003-07-16 00:28:27 +02:00
|
|
|
bool operator == (const Hash & h2) const;
|
2003-06-16 16:19:32 +02:00
|
|
|
|
|
|
|
/* Check whether two hash are not equal. */
|
2003-07-16 00:28:27 +02:00
|
|
|
bool operator != (const Hash & h2) const;
|
2003-06-16 16:19:32 +02:00
|
|
|
|
2003-07-15 23:24:05 +02:00
|
|
|
/* For sorting. */
|
|
|
|
bool operator < (const Hash & h) const;
|
2003-06-15 15:41:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-01-14 17:04:03 +01:00
|
|
|
/* Convert a hash to a hexadecimal representation. */
|
|
|
|
string printHash(const Hash & hash);
|
|
|
|
|
2003-06-16 16:19:32 +02:00
|
|
|
/* Parse a hexadecimal representation of a hash code. */
|
2005-01-14 17:04:03 +01:00
|
|
|
Hash parseHash(HashType ht, const string & s);
|
|
|
|
|
2006-09-20 18:15:32 +02:00
|
|
|
/* Returns the length of a base-32 hash representation. */
|
|
|
|
unsigned int hashLength32(const Hash & hash);
|
|
|
|
|
2005-01-14 17:04:03 +01:00
|
|
|
/* Convert a hash to a base-32 representation. */
|
|
|
|
string printHash32(const Hash & hash);
|
|
|
|
|
2012-10-23 18:05:50 +02:00
|
|
|
/* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
|
|
|
|
string printHash16or32(const Hash & hash);
|
|
|
|
|
2005-01-14 17:04:03 +01:00
|
|
|
/* Parse a base-32 representation of a hash code. */
|
|
|
|
Hash parseHash32(HashType ht, const string & s);
|
2003-06-16 16:19:32 +02:00
|
|
|
|
2011-12-02 12:47:06 +01:00
|
|
|
/* Parse a base-16 or base-32 representation of a hash code. */
|
|
|
|
Hash parseHash16or32(HashType ht, const string & s);
|
|
|
|
|
2003-06-16 16:19:32 +02:00
|
|
|
/* Verify that the given string is a valid hash code. */
|
2003-06-15 15:41:32 +02:00
|
|
|
bool isHash(const string & s);
|
2003-06-16 16:19:32 +02:00
|
|
|
|
|
|
|
/* Compute the hash of the given string. */
|
* 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 17:55:19 +01:00
|
|
|
Hash hashString(HashType ht, const string & s);
|
2003-06-16 16:19:32 +02: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 17:55:19 +01:00
|
|
|
Hash hashFile(HashType ht, const Path & path);
|
2003-06-15 15:41:32 +02:00
|
|
|
|
2003-06-16 16:19:32 +02:00
|
|
|
/* Compute the hash of the given path. The hash is defined as
|
2006-02-01 17:48:49 +01:00
|
|
|
(essentially) hashString(ht, dumpPath(path)). */
|
2006-12-13 00:05:01 +01:00
|
|
|
struct PathFilter;
|
|
|
|
extern PathFilter defaultPathFilter;
|
2010-11-16 18:11:46 +01:00
|
|
|
typedef std::pair<Hash, unsigned long long> HashResult;
|
|
|
|
HashResult hashPath(HashType ht, const Path & path,
|
2006-12-13 00:05:01 +01:00
|
|
|
PathFilter & filter = defaultPathFilter);
|
2003-06-16 17:59:23 +02:00
|
|
|
|
2005-01-14 17:04:03 +01: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 17:55:19 +01:00
|
|
|
/* Parse a string representing a hash type. */
|
|
|
|
HashType parseHashType(const string & s);
|
|
|
|
|
2008-12-03 17:10:17 +01:00
|
|
|
/* And the reverse. */
|
|
|
|
string printHashType(HashType ht);
|
|
|
|
|
2007-02-21 15:31:42 +01:00
|
|
|
|
2008-05-21 13:17:31 +02:00
|
|
|
union Ctx;
|
2007-02-21 15:31:42 +01:00
|
|
|
|
2011-12-15 17:19:53 +01:00
|
|
|
class HashSink : public BufferedSink
|
2007-02-21 15:31:42 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
HashType ht;
|
|
|
|
Ctx * ctx;
|
2010-11-16 18:11:46 +01:00
|
|
|
unsigned long long bytes;
|
2007-02-21 15:31:42 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
HashSink(HashType ht);
|
2010-03-09 15:32:03 +01:00
|
|
|
HashSink(const HashSink & h);
|
2007-02-21 15:31:42 +01:00
|
|
|
~HashSink();
|
2011-12-15 17:19:53 +01:00
|
|
|
void write(const unsigned char * data, size_t len);
|
2010-11-16 18:11:46 +01:00
|
|
|
HashResult finish();
|
2011-12-15 17:19:53 +01:00
|
|
|
HashResult currentHash();
|
2007-02-21 15:31:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
}
|