hashes.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. "SHA512", "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. string fileHash;
  48. FileFd Fd(filename, FileFd::ReadOnly);
  49. if(Type == "MD5Sum")
  50. {
  51. MD5Summation MD5;
  52. MD5.AddFD(Fd.Fd(), Fd.Size());
  53. fileHash = (string)MD5.Result();
  54. }
  55. else if (Type == "SHA1")
  56. {
  57. SHA1Summation SHA1;
  58. SHA1.AddFD(Fd.Fd(), Fd.Size());
  59. fileHash = (string)SHA1.Result();
  60. }
  61. else if (Type == "SHA256")
  62. {
  63. SHA256Summation SHA256;
  64. SHA256.AddFD(Fd.Fd(), Fd.Size());
  65. fileHash = (string)SHA256.Result();
  66. }
  67. else if (Type == "SHA512")
  68. {
  69. SHA512Summation SHA512;
  70. SHA512.AddFD(Fd.Fd(), Fd.Size());
  71. fileHash = (string)SHA512.Result();
  72. }
  73. Fd.Close();
  74. if(_config->FindB("Debug::Hashes",false) == true)
  75. std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
  76. return (fileHash == Hash);
  77. }
  78. /*}}}*/
  79. const char** HashString::SupportedHashes()
  80. {
  81. return _SupportedHashes;
  82. }
  83. bool HashString::empty() const
  84. {
  85. return (Type.empty() || Hash.empty());
  86. }
  87. string HashString::toStr() const
  88. {
  89. return Type+string(":")+Hash;
  90. }
  91. // Hashes::AddFD - Add the contents of the FD /*{{{*/
  92. // ---------------------------------------------------------------------
  93. /* */
  94. bool Hashes::AddFD(int const Fd,unsigned long Size, bool const addMD5,
  95. bool const addSHA1, bool const addSHA256, bool const addSHA512)
  96. {
  97. unsigned char Buf[64*64];
  98. int Res = 0;
  99. int ToEOF = (Size == 0);
  100. while (Size != 0 || ToEOF)
  101. {
  102. unsigned n = sizeof(Buf);
  103. if (!ToEOF) n = min(Size,(unsigned long)n);
  104. Res = read(Fd,Buf,n);
  105. if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
  106. return false;
  107. if (ToEOF && Res == 0) // EOF
  108. break;
  109. Size -= Res;
  110. if (addMD5 == true)
  111. MD5.Add(Buf,Res);
  112. if (addSHA1 == true)
  113. SHA1.Add(Buf,Res);
  114. if (addSHA256 == true)
  115. SHA256.Add(Buf,Res);
  116. if (addSHA512 == true)
  117. SHA512.Add(Buf,Res);
  118. }
  119. return true;
  120. }
  121. /*}}}*/