MessageDigest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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 <sstream>
  37. #include <iterator>
  38. #include "MessageDigestImpl.h"
  39. #include "util.h"
  40. #include "array_fun.h"
  41. namespace aria2 {
  42. namespace {
  43. struct HashTypeEntry {
  44. std::string hashType;
  45. int strength;
  46. HashTypeEntry(std::string hashType, int strength)
  47. : hashType(std::move(hashType)), strength(strength)
  48. {
  49. }
  50. };
  51. } // namespace
  52. namespace {
  53. HashTypeEntry hashTypes[] = {
  54. HashTypeEntry("sha-1", 1), HashTypeEntry("sha-224", 2),
  55. HashTypeEntry("sha-256", 3), HashTypeEntry("sha-384", 4),
  56. HashTypeEntry("sha-512", 5), HashTypeEntry("md5", 0),
  57. HashTypeEntry("adler32", 0),
  58. };
  59. } // namespace aria2
  60. MessageDigest::MessageDigest(std::unique_ptr<MessageDigestImpl> impl)
  61. : pImpl_{std::move(impl)}
  62. {
  63. }
  64. MessageDigest::~MessageDigest() {}
  65. std::unique_ptr<MessageDigest> MessageDigest::sha1()
  66. {
  67. return make_unique<MessageDigest>(MessageDigestImpl::sha1());
  68. }
  69. std::unique_ptr<MessageDigest>
  70. MessageDigest::create(const std::string& hashType)
  71. {
  72. return make_unique<MessageDigest>(MessageDigestImpl::create(hashType));
  73. }
  74. bool MessageDigest::supports(const std::string& hashType)
  75. {
  76. return MessageDigestImpl::supports(hashType);
  77. }
  78. std::vector<std::string> MessageDigest::getSupportedHashTypes()
  79. {
  80. std::vector<std::string> rv;
  81. for (const auto& i : hashTypes) {
  82. if (MessageDigestImpl::supports(i.hashType)) {
  83. rv.push_back(i.hashType);
  84. }
  85. }
  86. return rv;
  87. }
  88. std::string MessageDigest::getSupportedHashTypeString()
  89. {
  90. auto ht = getSupportedHashTypes();
  91. std::stringstream ss;
  92. std::copy(std::begin(ht), std::end(ht),
  93. std::ostream_iterator<std::string>(ss, ", "));
  94. auto res = ss.str();
  95. if (!res.empty()) {
  96. res.erase(ss.str().length() - 2);
  97. }
  98. return res;
  99. }
  100. size_t MessageDigest::getDigestLength(const std::string& hashType)
  101. {
  102. return MessageDigestImpl::getDigestLength(hashType);
  103. }
  104. bool MessageDigest::isStronger(const std::string& lhs, const std::string& rhs)
  105. {
  106. auto lEntry = std::find_if(
  107. std::begin(hashTypes), std::end(hashTypes),
  108. [&lhs](const HashTypeEntry& entry) { return lhs == entry.hashType; });
  109. auto rEntry = std::find_if(
  110. std::begin(hashTypes), std::end(hashTypes),
  111. [&rhs](const HashTypeEntry& entry) { return rhs == entry.hashType; });
  112. if (lEntry == std::end(hashTypes)) {
  113. return false;
  114. }
  115. if (rEntry == std::end(hashTypes)) {
  116. return true;
  117. }
  118. return lEntry->strength > rEntry->strength;
  119. }
  120. bool MessageDigest::isValidHash(const std::string& hashType,
  121. const std::string& hexDigest)
  122. {
  123. return util::isHexDigit(hexDigest) && supports(hashType) &&
  124. getDigestLength(hashType) * 2 == hexDigest.size();
  125. }
  126. std::string MessageDigest::getCanonicalHashType(const std::string& hashType)
  127. {
  128. // This is really backward compatibility for Metalink3. aria2 only
  129. // supported sha-1, sha-256 and md5 at Metalink3 era. So we don't
  130. // add alias for sha-224, sha-384 and sha-512.
  131. if ("sha1" == hashType) {
  132. return "sha-1";
  133. }
  134. else if ("sha256" == hashType) {
  135. return "sha-256";
  136. }
  137. else {
  138. return hashType;
  139. }
  140. }
  141. size_t MessageDigest::getDigestLength() const
  142. {
  143. return pImpl_->getDigestLength();
  144. }
  145. void MessageDigest::reset() { pImpl_->reset(); }
  146. MessageDigest& MessageDigest::update(const void* data, size_t length)
  147. {
  148. pImpl_->update(data, length);
  149. return *this;
  150. }
  151. void MessageDigest::digest(unsigned char* md) { pImpl_->digest(md); }
  152. std::string MessageDigest::digest()
  153. {
  154. size_t length = pImpl_->getDigestLength();
  155. auto buf = make_unique<unsigned char[]>(length);
  156. pImpl_->digest(buf.get());
  157. return std::string(&buf[0], &buf[length]);
  158. }
  159. } // namespace aria2