messageDigest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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(vbegin(digests), vend(digests));
  86. return algomap;
  87. }
  88. std::string MessageDigestContext::digestFinal()
  89. {
  90. size_t length = digestLength(algo);
  91. unsigned char* rawMD = new unsigned char[length];
  92. digestFinal(rawMD);
  93. std::string rawMDString(&rawMD[0], &rawMD[length]);
  94. delete [] rawMD;
  95. return rawMDString;
  96. }
  97. bool MessageDigestContext::supports(const std::string& algostring)
  98. {
  99. const DigestAlgoMap& allAlgos = getDigestAlgos();
  100. DigestAlgoMap::const_iterator itr = allAlgos.find(algostring);
  101. if(itr == allAlgos.end()) {
  102. return false;
  103. } else {
  104. return true;
  105. }
  106. }
  107. MessageDigestContext::DigestAlgo
  108. MessageDigestContext::getDigestAlgo(const std::string& algostring)
  109. {
  110. const DigestAlgoMap& allAlgos = getDigestAlgos();
  111. DigestAlgoMap::const_iterator itr = allAlgos.find(algostring);
  112. if(itr == allAlgos.end()) {
  113. throw DL_ABORT_EX
  114. (StringFormat("Digest algorithm %s is not supported.",
  115. algostring.c_str()).str());
  116. }
  117. return (*itr).second.algo;
  118. }
  119. std::string MessageDigestContext::getSupportedAlgoString()
  120. {
  121. const DigestAlgoMap& allAlgos = getDigestAlgos();
  122. std::string algos;
  123. for(DigestAlgoMap::const_iterator itr = allAlgos.begin(),
  124. eoi = allAlgos.end(); itr != eoi; ++itr) {
  125. algos += (*itr).first;
  126. algos += ", ";
  127. }
  128. return util::trim(algos, ", ");
  129. }
  130. bool MessageDigestContext::isStronger
  131. (const std::string& lhs, const std::string& rhs)
  132. {
  133. const DigestAlgoMap& allAlgos = getDigestAlgos();
  134. DigestAlgoMap::const_iterator lhsitr = allAlgos.find(lhs);
  135. DigestAlgoMap::const_iterator rhsitr = allAlgos.find(rhs);
  136. if(lhsitr == allAlgos.end() || rhsitr == allAlgos.end()) {
  137. return false;
  138. }
  139. return (*lhsitr).second.strength > (*rhsitr).second.strength;
  140. }
  141. } // namespace aria2