hash.cc 7.4 KB

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