hash.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include "config.h"
  2. #include <iostream>
  3. #include <cstring>
  4. #include "hash.hh"
  5. #include "archive.hh"
  6. #include "util.hh"
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. namespace nix {
  11. Hash::Hash()
  12. {
  13. type = htUnknown;
  14. hashSize = 0;
  15. memset(hash, 0, maxHashSize);
  16. }
  17. Hash::Hash(HashType type)
  18. {
  19. this->type = type;
  20. hashSize = gcry_md_get_algo_dlen(type);
  21. if (hashSize == 0) throw Error("unknown hash type");
  22. assert(hashSize <= maxHashSize);
  23. memset(hash, 0, maxHashSize);
  24. }
  25. bool Hash::operator == (const Hash & h2) const
  26. {
  27. if (hashSize != h2.hashSize) return false;
  28. for (unsigned int i = 0; i < hashSize; i++)
  29. if (hash[i] != h2.hash[i]) return false;
  30. return true;
  31. }
  32. bool Hash::operator != (const Hash & h2) const
  33. {
  34. return !(*this == h2);
  35. }
  36. bool Hash::operator < (const Hash & h) const
  37. {
  38. for (unsigned int i = 0; i < hashSize; i++) {
  39. if (hash[i] < h.hash[i]) return true;
  40. if (hash[i] > h.hash[i]) return false;
  41. }
  42. return false;
  43. }
  44. const string base16Chars = "0123456789abcdef";
  45. string printHash(const Hash & hash)
  46. {
  47. char buf[hash.hashSize * 2];
  48. for (unsigned int i = 0; i < hash.hashSize; i++) {
  49. buf[i * 2] = base16Chars[hash.hash[i] >> 4];
  50. buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f];
  51. }
  52. return string(buf, hash.hashSize * 2);
  53. }
  54. Hash parseHash(HashType ht, const string & s)
  55. {
  56. Hash hash(ht);
  57. if (s.length() != hash.hashSize * 2) {
  58. string algo = gcry_md_algo_name(ht);
  59. throw Error(format("invalid %1% hash '%2%' (%3% bytes but expected %4%)")
  60. % algo % s % (s.length() / 2) % hash.hashSize);
  61. }
  62. for (unsigned int i = 0; i < hash.hashSize; i++) {
  63. string s2(s, i * 2, 2);
  64. if (!isxdigit(s2[0]) || !isxdigit(s2[1]))
  65. throw Error(format("invalid hash `%1%'") % s);
  66. std::istringstream str(s2);
  67. int n;
  68. str >> std::hex >> n;
  69. hash.hash[i] = n;
  70. }
  71. return hash;
  72. }
  73. unsigned int hashLength32(const Hash & hash)
  74. {
  75. return (hash.hashSize * 8 - 1) / 5 + 1;
  76. }
  77. // omitted: E O U T
  78. const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz";
  79. string printHash32(const Hash & hash)
  80. {
  81. Hash hash2(hash);
  82. unsigned int len = hashLength32(hash);
  83. string s;
  84. s.reserve(len);
  85. for (int n = len - 1; n >= 0; n--) {
  86. unsigned int b = n * 5;
  87. unsigned int i = b / 8;
  88. unsigned int j = b % 8;
  89. unsigned char c =
  90. (hash.hash[i] >> j)
  91. | (i >= hash.hashSize - 1 ? 0 : hash.hash[i + 1] << (8 - j));
  92. s.push_back(base32Chars[c & 0x1f]);
  93. }
  94. return s;
  95. }
  96. string printHash16or32(const Hash & hash)
  97. {
  98. return hash.type == htMD5 ? printHash(hash) : printHash32(hash);
  99. }
  100. Hash parseHash32(HashType ht, const string & s)
  101. {
  102. Hash hash(ht);
  103. unsigned int len = hashLength32(ht);
  104. assert(s.size() == len);
  105. for (unsigned int n = 0; n < len; ++n) {
  106. char c = s[len - n - 1];
  107. unsigned char digit;
  108. for (digit = 0; digit < base32Chars.size(); ++digit) /* !!! slow */
  109. if (base32Chars[digit] == c) break;
  110. if (digit >= 32)
  111. throw Error(format("invalid base-32 hash '%1%'") % s);
  112. unsigned int b = n * 5;
  113. unsigned int i = b / 8;
  114. unsigned int j = b % 8;
  115. hash.hash[i] |= digit << j;
  116. if (i < hash.hashSize - 1) hash.hash[i + 1] |= digit >> (8 - j);
  117. }
  118. return hash;
  119. }
  120. Hash parseHash16or32(HashType ht, const string & s)
  121. {
  122. Hash hash(ht);
  123. if (s.size() == hash.hashSize * 2)
  124. /* hexadecimal representation */
  125. hash = parseHash(ht, s);
  126. else if (s.size() == hashLength32(hash))
  127. /* base-32 representation */
  128. hash = parseHash32(ht, s);
  129. else
  130. throw Error(format("hash `%1%' has wrong length for hash type `%2%'")
  131. % s % printHashType(ht));
  132. return hash;
  133. }
  134. bool isHash(const string & s)
  135. {
  136. if (s.length() != 32) return false;
  137. for (int i = 0; i < 32; i++) {
  138. char c = s[i];
  139. if (!((c >= '0' && c <= '9') ||
  140. (c >= 'a' && c <= 'f')))
  141. return false;
  142. }
  143. return true;
  144. }
  145. /* The "hash context". */
  146. struct Ctx
  147. {
  148. /* This copy constructor is needed in 'HashSink::currentHash()' where we
  149. expect the copy of a 'Ctx' object to yield a truly different context. */
  150. Ctx(Ctx &ref)
  151. {
  152. if (ref.md_handle == NULL)
  153. md_handle = NULL;
  154. else
  155. gcry_md_copy (&md_handle, ref.md_handle);
  156. }
  157. /* Make sure 'md_handle' is always initialized. */
  158. Ctx(): md_handle (NULL) { };
  159. gcry_md_hd_t md_handle;
  160. };
  161. static void start(HashType ht, Ctx & ctx)
  162. {
  163. gcry_error_t err;
  164. err = gcry_md_open (&ctx.md_handle, ht, 0);
  165. assert (err == GPG_ERR_NO_ERROR);
  166. }
  167. static void update(HashType ht, Ctx & ctx,
  168. const unsigned char * bytes, unsigned int len)
  169. {
  170. gcry_md_write (ctx.md_handle, bytes, len);
  171. }
  172. static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
  173. {
  174. memcpy (hash, gcry_md_read (ctx.md_handle, ht),
  175. gcry_md_get_algo_dlen (ht));
  176. gcry_md_close (ctx.md_handle);
  177. ctx.md_handle = NULL;
  178. }
  179. Hash hashString(HashType ht, const string & s)
  180. {
  181. Ctx ctx;
  182. Hash hash(ht);
  183. start(ht, ctx);
  184. update(ht, ctx, (const unsigned char *) s.data(), s.length());
  185. finish(ht, ctx, hash.hash);
  186. return hash;
  187. }
  188. Hash hashFile(HashType ht, const Path & path)
  189. {
  190. Ctx ctx;
  191. Hash hash(ht);
  192. start(ht, ctx);
  193. AutoCloseFD fd = open(path.c_str(), O_RDONLY);
  194. if (fd == -1) throw SysError(format("computing hash of file `%1%'") % path);
  195. unsigned char buf[8192];
  196. ssize_t n;
  197. while ((n = read(fd, buf, sizeof(buf)))) {
  198. checkInterrupt();
  199. if (n == -1) throw SysError(format("reading file `%1%'") % path);
  200. update(ht, ctx, buf, n);
  201. }
  202. finish(ht, ctx, hash.hash);
  203. return hash;
  204. }
  205. HashSink::HashSink(HashType ht) : ht(ht)
  206. {
  207. ctx = new Ctx;
  208. bytes = 0;
  209. start(ht, *ctx);
  210. }
  211. HashSink::~HashSink()
  212. {
  213. bufPos = 0;
  214. delete ctx;
  215. }
  216. void HashSink::write(const unsigned char * data, size_t len)
  217. {
  218. bytes += len;
  219. update(ht, *ctx, data, len);
  220. }
  221. HashResult HashSink::finish()
  222. {
  223. flush();
  224. Hash hash(ht);
  225. nix::finish(ht, *ctx, hash.hash);
  226. return HashResult(hash, bytes);
  227. }
  228. HashResult HashSink::currentHash()
  229. {
  230. flush();
  231. Ctx ctx2 = *ctx;
  232. Hash hash(ht);
  233. nix::finish(ht, ctx2, hash.hash);
  234. return HashResult(hash, bytes);
  235. }
  236. HashResult hashPath(
  237. HashType ht, const Path & path, PathFilter & filter)
  238. {
  239. HashSink sink(ht);
  240. dumpPath(path, sink, filter);
  241. return sink.finish();
  242. }
  243. Hash compressHash(const Hash & hash, unsigned int newSize)
  244. {
  245. Hash h;
  246. h.hashSize = newSize;
  247. for (unsigned int i = 0; i < hash.hashSize; ++i)
  248. h.hash[i % newSize] ^= hash.hash[i];
  249. return h;
  250. }
  251. HashType parseHashType(const string & s)
  252. {
  253. if (s == "md5") return htMD5;
  254. else if (s == "sha1") return htSHA1;
  255. else if (s == "sha256") return htSHA256;
  256. else if (s == "sha512") return htSHA512;
  257. else if (s == "sha3-256") return htSHA3_256;
  258. else if (s == "sha3-512") return htSHA3_512;
  259. else if (s == "blake2s-256") return htBLAKE2s_256;
  260. else return htUnknown;
  261. }
  262. string printHashType(HashType ht)
  263. {
  264. if (ht == htMD5) return "md5";
  265. else if (ht == htSHA1) return "sha1";
  266. else if (ht == htSHA256) return "sha256";
  267. else if (ht == htSHA512) return "sha512";
  268. else if (ht == htSHA3_256) return "sha3-256";
  269. else if (ht == htSHA3_512) return "sha3-512";
  270. else if (ht == htBLAKE2s_256) return "blake2s-256";
  271. else throw Error("cannot print unknown hash type");
  272. }
  273. }