messageDigest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "messageDigest.h"
  36. #include "util.h"
  37. #include "array_fun.h"
  38. namespace aria2 {
  39. const std::string MessageDigestContext::SHA1("sha-1");
  40. const std::string MessageDigestContext::SHA256("sha-256");
  41. const std::string MessageDigestContext::MD5("md5");
  42. namespace {
  43. struct DigestAlgoEntry {
  44. MessageDigestContext::DigestAlgo algo;
  45. int strength;
  46. DigestAlgoEntry(const MessageDigestContext::DigestAlgo& algo, int strength):
  47. algo(algo), strength(strength) {}
  48. };
  49. }
  50. typedef std::map<std::string, DigestAlgoEntry>
  51. DigestAlgoMap;
  52. static const DigestAlgoMap& getDigestAlgos()
  53. {
  54. enum AlgoStrength {
  55. STRENGTH_MD5 = 0,
  56. STRENGTH_SHA_1 = 1,
  57. STRENGTH_SHA_256 = 2
  58. };
  59. static const DigestAlgoMap::value_type digests[] = {
  60. #ifdef HAVE_LIBSSL
  61. DigestAlgoMap::value_type("md5", DigestAlgoEntry(EVP_md5(), STRENGTH_MD5)),
  62. DigestAlgoMap::value_type
  63. ("sha-1", DigestAlgoEntry(EVP_sha1(), STRENGTH_SHA_1)),
  64. # ifdef HAVE_EVP_SHA256
  65. DigestAlgoMap::value_type
  66. ("sha-256", DigestAlgoEntry(EVP_sha256(), STRENGTH_SHA_256)),
  67. # endif // HAVE_EVP_SHA256
  68. #elif HAVE_LIBGCRYPT
  69. DigestAlgoMap::value_type
  70. ("md5", DigestAlgoEntry(GCRY_MD_MD5, STRENGTH_MD5)),
  71. DigestAlgoMap::value_type
  72. ("sha-1", DigestAlgoEntry(GCRY_MD_SHA1, STRENGTH_SHA_1)),
  73. DigestAlgoMap::value_type
  74. ("sha-256", DigestAlgoEntry(GCRY_MD_SHA256, STRENGTH_SHA_256)),
  75. #endif // HAVE_LIBGCRYPT
  76. };
  77. static const DigestAlgoMap algomap(vbegin(digests), vend(digests));
  78. return algomap;
  79. }
  80. std::string MessageDigestContext::getCanonicalAlgo
  81. (const std::string& algostring)
  82. {
  83. if(strcasecmp("sha-1", algostring.c_str()) == 0 ||
  84. strcasecmp("sha1", algostring.c_str()) == 0) {
  85. return SHA1;
  86. } else if(strcasecmp("sha-256", algostring.c_str()) == 0 ||
  87. strcasecmp("sha256", algostring.c_str()) == 0) {
  88. return SHA256;
  89. } else if(strcasecmp("md5", algostring.c_str()) == 0) {
  90. return MD5;
  91. } else {
  92. return algostring;
  93. }
  94. }
  95. std::string MessageDigestContext::digestFinal()
  96. {
  97. size_t length = digestLength(algo_);
  98. unsigned char* rawMD = new unsigned char[length];
  99. digestFinal(rawMD);
  100. std::string rawMDString(&rawMD[0], &rawMD[length]);
  101. delete [] rawMD;
  102. return rawMDString;
  103. }
  104. bool MessageDigestContext::supports(const std::string& algostring)
  105. {
  106. const DigestAlgoMap& allAlgos = getDigestAlgos();
  107. DigestAlgoMap::const_iterator itr = allAlgos.find(algostring);
  108. if(itr == allAlgos.end()) {
  109. return false;
  110. } else {
  111. return true;
  112. }
  113. }
  114. MessageDigestContext::DigestAlgo
  115. MessageDigestContext::getDigestAlgo(const std::string& algostring)
  116. {
  117. const DigestAlgoMap& allAlgos = getDigestAlgos();
  118. DigestAlgoMap::const_iterator itr = allAlgos.find(algostring);
  119. if(itr == allAlgos.end()) {
  120. throw DL_ABORT_EX
  121. (StringFormat("Digest algorithm %s is not supported.",
  122. algostring.c_str()).str());
  123. }
  124. return (*itr).second.algo;
  125. }
  126. std::string MessageDigestContext::getSupportedAlgoString()
  127. {
  128. const DigestAlgoMap& allAlgos = getDigestAlgos();
  129. std::string algos;
  130. for(DigestAlgoMap::const_iterator itr = allAlgos.begin(),
  131. eoi = allAlgos.end(); itr != eoi; ++itr) {
  132. algos += (*itr).first;
  133. algos += ", ";
  134. }
  135. return util::trim(algos, ", ");
  136. }
  137. bool MessageDigestContext::isStronger
  138. (const std::string& lhs, const std::string& rhs)
  139. {
  140. const DigestAlgoMap& allAlgos = getDigestAlgos();
  141. DigestAlgoMap::const_iterator lhsitr = allAlgos.find(lhs);
  142. DigestAlgoMap::const_iterator rhsitr = allAlgos.find(rhs);
  143. if(lhsitr == allAlgos.end() || rhsitr == allAlgos.end()) {
  144. return false;
  145. }
  146. return (*lhsitr).second.strength > (*rhsitr).second.strength;
  147. }
  148. bool MessageDigestContext::isValidHash
  149. (const std::string& algostring, const std::string& hashstring)
  150. {
  151. return util::isHexDigit(hashstring) &&
  152. supports(algostring) && digestLength(algostring)*2 == hashstring.size();
  153. }
  154. } // namespace aria2