hashes.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 jgg Exp $
  4. /* ######################################################################
  5. Hashes - Simple wrapper around the hash functions
  6. This is just used to make building the methods simpler, this is the
  7. only interface required..
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <apt-pkg/hashes.h>
  12. #include <apt-pkg/fileutl.h>
  13. #include <apt-pkg/configuration.h>
  14. #include <apt-pkg/macros.h>
  15. #include <unistd.h>
  16. #include <string>
  17. #include <iostream>
  18. /*}}}*/
  19. const char* HashString::_SupportedHashes[] =
  20. {
  21. "SHA256", "SHA1", "MD5Sum", NULL
  22. };
  23. HashString::HashString()
  24. {
  25. }
  26. HashString::HashString(string Type, string Hash) : Type(Type), Hash(Hash)
  27. {
  28. }
  29. HashString::HashString(string StringedHash) /*{{{*/
  30. {
  31. // legacy: md5sum without "MD5Sum:" prefix
  32. if (StringedHash.find(":") == string::npos && StringedHash.size() == 32)
  33. {
  34. Type = "MD5Sum";
  35. Hash = StringedHash;
  36. return;
  37. }
  38. string::size_type pos = StringedHash.find(":");
  39. Type = StringedHash.substr(0,pos);
  40. Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
  41. if(_config->FindB("Debug::Hashes",false) == true)
  42. std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
  43. }
  44. /*}}}*/
  45. bool HashString::VerifyFile(string filename) const /*{{{*/
  46. {
  47. FileFd fd;
  48. MD5Summation MD5;
  49. SHA1Summation SHA1;
  50. SHA256Summation SHA256;
  51. string fileHash;
  52. FileFd Fd(filename, FileFd::ReadOnly);
  53. if(Type == "MD5Sum")
  54. {
  55. MD5.AddFD(Fd.Fd(), Fd.Size());
  56. fileHash = (string)MD5.Result();
  57. }
  58. else if (Type == "SHA1")
  59. {
  60. SHA1.AddFD(Fd.Fd(), Fd.Size());
  61. fileHash = (string)SHA1.Result();
  62. }
  63. else if (Type == "SHA256")
  64. {
  65. SHA256.AddFD(Fd.Fd(), Fd.Size());
  66. fileHash = (string)SHA256.Result();
  67. }
  68. Fd.Close();
  69. if(_config->FindB("Debug::Hashes",false) == true)
  70. std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
  71. return (fileHash == Hash);
  72. }
  73. /*}}}*/
  74. const char** HashString::SupportedHashes()
  75. {
  76. return _SupportedHashes;
  77. }
  78. bool HashString::empty() const
  79. {
  80. return (Type.empty() || Hash.empty());
  81. }
  82. string HashString::toStr() const
  83. {
  84. return Type+string(":")+Hash;
  85. }
  86. // Hashes::AddFD - Add the contents of the FD /*{{{*/
  87. // ---------------------------------------------------------------------
  88. /* */
  89. bool Hashes::AddFD(int Fd,unsigned long Size)
  90. {
  91. unsigned char Buf[64*64];
  92. int Res = 0;
  93. int ToEOF = (Size == 0);
  94. while (Size != 0 || ToEOF)
  95. {
  96. unsigned n = sizeof(Buf);
  97. if (!ToEOF) n = min(Size,(unsigned long)n);
  98. Res = read(Fd,Buf,n);
  99. if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
  100. return false;
  101. if (ToEOF && Res == 0) // EOF
  102. break;
  103. Size -= Res;
  104. MD5.Add(Buf,Res);
  105. SHA1.Add(Buf,Res);
  106. SHA256.Add(Buf,Res);
  107. }
  108. return true;
  109. }
  110. /*}}}*/