hash.hh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #include "types.hh"
  3. #include "serialise.hh"
  4. namespace nix {
  5. typedef enum { htUnknown, htMD5, htSHA1, htSHA256, htSHA512 } HashType;
  6. const int md5HashSize = 16;
  7. const int sha1HashSize = 20;
  8. const int sha256HashSize = 32;
  9. const int sha512HashSize = 64;
  10. extern const string base32Chars;
  11. struct Hash
  12. {
  13. static const unsigned int maxHashSize = 64;
  14. unsigned int hashSize;
  15. unsigned char hash[maxHashSize];
  16. HashType type;
  17. /* Create an unusable hash object. */
  18. Hash();
  19. /* Create a zero-filled hash object. */
  20. Hash(HashType type);
  21. /* Check whether two hash are equal. */
  22. bool operator == (const Hash & h2) const;
  23. /* Check whether two hash are not equal. */
  24. bool operator != (const Hash & h2) const;
  25. /* For sorting. */
  26. bool operator < (const Hash & h) const;
  27. };
  28. /* Convert a hash to a hexadecimal representation. */
  29. string printHash(const Hash & hash);
  30. /* Parse a hexadecimal representation of a hash code. */
  31. Hash parseHash(HashType ht, const string & s);
  32. /* Returns the length of a base-32 hash representation. */
  33. unsigned int hashLength32(const Hash & hash);
  34. /* Convert a hash to a base-32 representation. */
  35. string printHash32(const Hash & hash);
  36. /* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
  37. string printHash16or32(const Hash & hash);
  38. /* Parse a base-32 representation of a hash code. */
  39. Hash parseHash32(HashType ht, const string & s);
  40. /* Parse a base-16 or base-32 representation of a hash code. */
  41. Hash parseHash16or32(HashType ht, const string & s);
  42. /* Verify that the given string is a valid hash code. */
  43. bool isHash(const string & s);
  44. /* Compute the hash of the given string. */
  45. Hash hashString(HashType ht, const string & s);
  46. /* Compute the hash of the given file. */
  47. Hash hashFile(HashType ht, const Path & path);
  48. /* Compute the hash of the given path. The hash is defined as
  49. (essentially) hashString(ht, dumpPath(path)). */
  50. struct PathFilter;
  51. extern PathFilter defaultPathFilter;
  52. typedef std::pair<Hash, unsigned long long> HashResult;
  53. HashResult hashPath(HashType ht, const Path & path,
  54. PathFilter & filter = defaultPathFilter);
  55. /* Compress a hash to the specified number of bytes by cyclically
  56. XORing bytes together. */
  57. Hash compressHash(const Hash & hash, unsigned int newSize);
  58. /* Parse a string representing a hash type. */
  59. HashType parseHashType(const string & s);
  60. /* And the reverse. */
  61. string printHashType(HashType ht);
  62. struct Ctx;
  63. class HashSink : public BufferedSink
  64. {
  65. private:
  66. HashType ht;
  67. Ctx * ctx;
  68. unsigned long long bytes;
  69. public:
  70. HashSink(HashType ht);
  71. HashSink(const HashSink & h);
  72. ~HashSink();
  73. void write(const unsigned char * data, size_t len);
  74. HashResult finish();
  75. HashResult currentHash();
  76. };
  77. }