hash.hh 2.8 KB

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