hashes.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <config.h>
  12. #include <apt-pkg/hashes.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/macros.h>
  16. #include <unistd.h>
  17. #include <string>
  18. #include <iostream>
  19. /*}}}*/
  20. const char* HashString::_SupportedHashes[] =
  21. {
  22. "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
  23. };
  24. HashString::HashString()
  25. {
  26. }
  27. HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
  28. {
  29. }
  30. HashString::HashString(std::string StringedHash) /*{{{*/
  31. {
  32. // legacy: md5sum without "MD5Sum:" prefix
  33. if (StringedHash.find(":") == std::string::npos && StringedHash.size() == 32)
  34. {
  35. Type = "MD5Sum";
  36. Hash = StringedHash;
  37. return;
  38. }
  39. std::string::size_type pos = StringedHash.find(":");
  40. Type = StringedHash.substr(0,pos);
  41. Hash = StringedHash.substr(pos+1, StringedHash.size() - pos);
  42. if(_config->FindB("Debug::Hashes",false) == true)
  43. std::clog << "HashString(string): " << Type << " : " << Hash << std::endl;
  44. }
  45. /*}}}*/
  46. bool HashString::VerifyFile(std::string filename) const /*{{{*/
  47. {
  48. std::string fileHash;
  49. FileFd Fd(filename, FileFd::ReadOnly);
  50. if(Type == "MD5Sum")
  51. {
  52. MD5Summation MD5;
  53. MD5.AddFD(Fd.Fd(), Fd.Size());
  54. fileHash = (std::string)MD5.Result();
  55. }
  56. else if (Type == "SHA1")
  57. {
  58. SHA1Summation SHA1;
  59. SHA1.AddFD(Fd.Fd(), Fd.Size());
  60. fileHash = (std::string)SHA1.Result();
  61. }
  62. else if (Type == "SHA256")
  63. {
  64. SHA256Summation SHA256;
  65. SHA256.AddFD(Fd.Fd(), Fd.Size());
  66. fileHash = (std::string)SHA256.Result();
  67. }
  68. else if (Type == "SHA512")
  69. {
  70. SHA512Summation SHA512;
  71. SHA512.AddFD(Fd.Fd(), Fd.Size());
  72. fileHash = (std::string)SHA512.Result();
  73. }
  74. Fd.Close();
  75. if(_config->FindB("Debug::Hashes",false) == true)
  76. std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
  77. return (fileHash == Hash);
  78. }
  79. /*}}}*/
  80. const char** HashString::SupportedHashes()
  81. {
  82. return _SupportedHashes;
  83. }
  84. bool HashString::empty() const
  85. {
  86. return (Type.empty() || Hash.empty());
  87. }
  88. std::string HashString::toStr() const
  89. {
  90. return Type + std::string(":") + Hash;
  91. }
  92. // Hashes::AddFD - Add the contents of the FD /*{{{*/
  93. // ---------------------------------------------------------------------
  94. /* */
  95. bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5,
  96. bool const addSHA1, bool const addSHA256, bool const addSHA512)
  97. {
  98. unsigned char Buf[64*64];
  99. ssize_t Res = 0;
  100. int ToEOF = (Size == 0);
  101. while (Size != 0 || ToEOF)
  102. {
  103. unsigned long long n = sizeof(Buf);
  104. if (!ToEOF) n = std::min(Size, n);
  105. Res = read(Fd,Buf,n);
  106. if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
  107. return false;
  108. if (ToEOF && Res == 0) // EOF
  109. break;
  110. Size -= Res;
  111. if (addMD5 == true)
  112. MD5.Add(Buf,Res);
  113. if (addSHA1 == true)
  114. SHA1.Add(Buf,Res);
  115. if (addSHA256 == true)
  116. SHA256.Add(Buf,Res);
  117. if (addSHA512 == true)
  118. SHA512.Add(Buf,Res);
  119. }
  120. return true;
  121. }
  122. /*}}}*/