hash.cc 7.4 KB

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