crypto_utils.cxx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Filename: crypto_utils.cxx
  2. // Created by: drose (07Nov00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. // This file is compiled only if we have crypto++ installed.
  6. #include "crypto_utils.h"
  7. #include <md5.h>
  8. #include <files.h>
  9. #include <string>
  10. USING_NAMESPACE(CryptoPP);
  11. USING_NAMESPACE(std);
  12. uint
  13. read32(istream& is) {
  14. unsigned int ret = 0x0;
  15. unsigned char b1, b2, b3, b4;
  16. is >> b1;
  17. is >> b2;
  18. is >> b3;
  19. is >> b4;
  20. ret = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
  21. return ret;
  22. }
  23. void
  24. md5_a_file(const Filename &name, HashVal &ret) {
  25. ostringstream os;
  26. MD5 md5;
  27. string fs = name.get_fullpath();
  28. FileSource f(fs.c_str(), true, new HashFilter(md5, new FileSink(os)));
  29. istringstream is(os.str());
  30. ret[0] = read32(is);
  31. ret[1] = read32(is);
  32. ret[2] = read32(is);
  33. ret[3] = read32(is);
  34. }
  35. void
  36. md5_a_buffer(unsigned char* buf, unsigned long len, HashVal& ret) {
  37. MD5 md5;
  38. HashFilter hash(md5);
  39. hash.Put((byte*)buf, len);
  40. hash.Close();
  41. unsigned char* outb;
  42. unsigned long outl = hash.MaxRetrieveable();
  43. outb = new uchar[outl];
  44. hash.Get((byte*)outb, outl);
  45. ret[0] = (outb[0] << 24) | (outb[1] << 16) | (outb[2] << 8) | outb[3];
  46. ret[1] = (outb[4] << 24) | (outb[5] << 16) | (outb[6] << 8) | outb[7];
  47. ret[2] = (outb[8] << 24) | (outb[9] << 16) | (outb[10] << 8) | outb[11];
  48. ret[3] = (outb[12] << 24) | (outb[13] << 16) | (outb[14] << 8) | outb[15];
  49. delete outb;
  50. }