hashes.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <unistd.h>
  15. #include <system.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. bool HashString::VerifyFile(string filename) const
  45. {
  46. FileFd fd;
  47. MD5Summation MD5;
  48. SHA1Summation SHA1;
  49. SHA256Summation SHA256;
  50. string fileHash;
  51. FileFd Fd(filename, FileFd::ReadOnly);
  52. if(Type == "MD5Sum")
  53. {
  54. MD5.AddFD(Fd.Fd(), Fd.Size());
  55. fileHash = (string)MD5.Result();
  56. }
  57. else if (Type == "SHA1")
  58. {
  59. SHA1.AddFD(Fd.Fd(), Fd.Size());
  60. fileHash = (string)SHA1.Result();
  61. }
  62. else if (Type == "SHA256")
  63. {
  64. SHA256.AddFD(Fd.Fd(), Fd.Size());
  65. fileHash = (string)SHA256.Result();
  66. }
  67. Fd.Close();
  68. if(_config->FindB("Debug::Hashes",false) == true)
  69. std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
  70. return (fileHash == Hash);
  71. }
  72. const char** HashString::SupportedHashes()
  73. {
  74. return _SupportedHashes;
  75. }
  76. bool HashString::empty() const
  77. {
  78. return (Type.empty() || Hash.empty());
  79. }
  80. string HashString::toStr() const
  81. {
  82. return Type+string(":")+Hash;
  83. }
  84. // Hashes::AddFD - Add the contents of the FD /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* */
  87. bool Hashes::AddFD(int Fd,unsigned long Size)
  88. {
  89. unsigned char Buf[64*64];
  90. int Res = 0;
  91. while (Size != 0)
  92. {
  93. Res = read(Fd,Buf,min(Size,(unsigned long)sizeof(Buf)));
  94. if (Res < 0 || (unsigned)Res != min(Size,(unsigned long)sizeof(Buf)))
  95. return false;
  96. Size -= Res;
  97. MD5.Add(Buf,Res);
  98. SHA1.Add(Buf,Res);
  99. SHA256.Add(Buf,Res);
  100. }
  101. return true;
  102. }
  103. /*}}}*/