messageDigest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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("md-5");
  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. DigestAlgoMap::value_type
  65. ("sha1", DigestAlgoEntry(EVP_sha1(), STRENGTH_SHA_1)),
  66. # ifdef HAVE_EVP_SHA256
  67. DigestAlgoMap::value_type
  68. ("sha-256", DigestAlgoEntry(EVP_sha256(), STRENGTH_SHA_256)),
  69. DigestAlgoMap::value_type
  70. ("sha256", DigestAlgoEntry(EVP_sha256(), STRENGTH_SHA_256)),
  71. # endif // HAVE_EVP_SHA256
  72. #elif HAVE_LIBGCRYPT
  73. DigestAlgoMap::value_type
  74. ("md5", DigestAlgoEntry(GCRY_MD_MD5, STRENGTH_MD5)),
  75. DigestAlgoMap::value_type
  76. ("sha-1", DigestAlgoEntry(GCRY_MD_SHA1, STRENGTH_SHA_1)),
  77. DigestAlgoMap::value_type
  78. ("sha1", DigestAlgoEntry(GCRY_MD_SHA1, STRENGTH_SHA_1)),
  79. DigestAlgoMap::value_type
  80. ("sha-256", DigestAlgoEntry(GCRY_MD_SHA256, STRENGTH_SHA_256)),
  81. DigestAlgoMap::value_type
  82. ("sha256", DigestAlgoEntry(GCRY_MD_SHA256, STRENGTH_SHA_256)),
  83. #endif // HAVE_LIBGCRYPT
  84. };
  85. static const DigestAlgoMap algomap
  86. (&digests[0], &digests[arrayLength(digests)]);
  87. return algomap;
  88. }
  89. std::string MessageDigestContext::digestFinal()
  90. {
  91. size_t length = digestLength(algo);
  92. unsigned char* rawMD = new unsigned char[length];
  93. digestFinal(rawMD);
  94. std::string rawMDString(&rawMD[0], &rawMD[length]);
  95. delete [] rawMD;
  96. return rawMDString;
  97. }
  98. bool MessageDigestContext::supports(const std::string& algostring)
  99. {
  100. const DigestAlgoMap& allAlgos = getDigestAlgos();
  101. DigestAlgoMap::const_iterator itr = allAlgos.find(algostring);
  102. if(itr == allAlgos.end()) {
  103. return false;
  104. } else {
  105. return true;
  106. }
  107. }
  108. MessageDigestContext::DigestAlgo
  109. MessageDigestContext::getDigestAlgo(const std::string& algostring)
  110. {
  111. const DigestAlgoMap& allAlgos = getDigestAlgos();
  112. DigestAlgoMap::const_iterator itr = allAlgos.find(algostring);
  113. if(itr == allAlgos.end()) {
  114. throw DL_ABORT_EX
  115. (StringFormat("Digest algorithm %s is not supported.",
  116. algostring.c_str()).str());
  117. }
  118. return (*itr).second.algo;
  119. }
  120. std::string MessageDigestContext::getSupportedAlgoString()
  121. {
  122. const DigestAlgoMap& allAlgos = getDigestAlgos();
  123. std::string algos;
  124. for(DigestAlgoMap::const_iterator itr = allAlgos.begin(),
  125. eoi = allAlgos.end(); itr != eoi; ++itr) {
  126. algos += (*itr).first;
  127. algos += ", ";
  128. }
  129. return util::trim(algos, ", ");
  130. }
  131. bool MessageDigestContext::isStronger
  132. (const std::string& lhs, const std::string& rhs)
  133. {
  134. const DigestAlgoMap& allAlgos = getDigestAlgos();
  135. DigestAlgoMap::const_iterator lhsitr = allAlgos.find(lhs);
  136. DigestAlgoMap::const_iterator rhsitr = allAlgos.find(rhs);
  137. if(lhsitr == allAlgos.end() || rhsitr == allAlgos.end()) {
  138. return false;
  139. }
  140. return (*lhsitr).second.strength > (*rhsitr).second.strength;
  141. }
  142. } // namespace aria2