hashes.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. FileFd fd;
  48. MD5Summation MD5;
  49. SHA1Summation SHA1;
  50. SHA256Summation SHA256;
  51. SHA256Summation SHA512;
  52. string fileHash;
  53. FileFd Fd(filename, FileFd::ReadOnly);
  54. if(Type == "MD5Sum")
  55. {
  56. MD5.AddFD(Fd.Fd(), Fd.Size());
  57. fileHash = (string)MD5.Result();
  58. }
  59. else if (Type == "SHA1")
  60. {
  61. SHA1.AddFD(Fd.Fd(), Fd.Size());
  62. fileHash = (string)SHA1.Result();
  63. }
  64. else if (Type == "SHA256")
  65. {
  66. SHA256.AddFD(Fd.Fd(), Fd.Size());
  67. fileHash = (string)SHA256.Result();
  68. }
  69. else if (Type == "SHA512")
  70. {
  71. SHA512.AddFD(Fd.Fd(), Fd.Size());
  72. fileHash = (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. string HashString::toStr() const
  89. {
  90. return Type+string(":")+Hash;
  91. }
  92. // Hashes::AddFD - Add the contents of the FD /*{{{*/
  93. // ---------------------------------------------------------------------
  94. /* */
  95. bool Hashes::AddFD(int Fd,unsigned long Size)
  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. MD5.Add(Buf,Res);
  111. SHA1.Add(Buf,Res);
  112. SHA256.Add(Buf,Res);
  113. SHA512.Add(Buf,Res);
  114. }
  115. return true;
  116. }
  117. /*}}}*/