MessageDigest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "MessageDigestImpl.h"
  37. #include "util.h"
  38. #include "array_fun.h"
  39. namespace aria2 {
  40. namespace {
  41. struct HashTypeEntry {
  42. std::string hashType;
  43. int strength;
  44. HashTypeEntry(const std::string& hashType, int strength):
  45. hashType(hashType), strength(strength) {}
  46. };
  47. } // namespace
  48. namespace {
  49. HashTypeEntry hashTypes[] = {
  50. HashTypeEntry("sha-1", 1),
  51. HashTypeEntry("sha-224", 2),
  52. HashTypeEntry("sha-256", 3),
  53. HashTypeEntry("sha-384", 4),
  54. HashTypeEntry("sha-512", 5),
  55. HashTypeEntry("md5", 0)
  56. };
  57. } // namespace aria2
  58. MessageDigest::MessageDigest()
  59. {}
  60. MessageDigest::~MessageDigest()
  61. {}
  62. SharedHandle<MessageDigest> MessageDigest::sha1()
  63. {
  64. SharedHandle<MessageDigest> md(new MessageDigest());
  65. md->pImpl_ = MessageDigestImpl::sha1();
  66. return md;
  67. }
  68. SharedHandle<MessageDigest> MessageDigest::create(const std::string& hashType)
  69. {
  70. SharedHandle<MessageDigest> md(new MessageDigest());
  71. md->pImpl_ = MessageDigestImpl::create(hashType);
  72. return md;
  73. }
  74. bool MessageDigest::supports(const std::string& hashType)
  75. {
  76. return MessageDigestImpl::supports(hashType);
  77. }
  78. std::string MessageDigest::getSupportedHashTypeString()
  79. {
  80. std::string s;
  81. for(HashTypeEntry* i = vbegin(hashTypes), *eoi = vend(hashTypes); i != eoi;
  82. ++i) {
  83. if(MessageDigestImpl::supports(i->hashType)) {
  84. if(!s.empty()) {
  85. s += ", ";
  86. }
  87. s += i->hashType;
  88. }
  89. }
  90. return s;
  91. }
  92. size_t MessageDigest::getDigestLength(const std::string& hashType)
  93. {
  94. return MessageDigestImpl::getDigestLength(hashType);
  95. }
  96. namespace {
  97. class FindHashTypeEntry {
  98. private:
  99. const std::string& hashType_;
  100. public:
  101. FindHashTypeEntry(const std::string& hashType):hashType_(hashType)
  102. {}
  103. bool operator()(const HashTypeEntry& entry) const
  104. {
  105. return hashType_ == entry.hashType;
  106. }
  107. };
  108. } // namespace
  109. bool MessageDigest::isStronger(const std::string& lhs, const std::string& rhs)
  110. {
  111. HashTypeEntry* lEntry = std::find_if(vbegin(hashTypes), vend(hashTypes),
  112. FindHashTypeEntry(lhs));
  113. HashTypeEntry* rEntry = std::find_if(vbegin(hashTypes), vend(hashTypes),
  114. FindHashTypeEntry(rhs));
  115. if(lEntry == vend(hashTypes) || rEntry == vend(hashTypes)) {
  116. return false;
  117. }
  118. return lEntry->strength > rEntry->strength;
  119. }
  120. bool MessageDigest::isValidHash
  121. (const std::string& hashType, const std::string& hexDigest)
  122. {
  123. return util::isHexDigit(hexDigest) &&
  124. supports(hashType) && 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. } else if("sha256" == hashType) {
  134. return "sha-256";
  135. } else {
  136. return hashType;
  137. }
  138. }
  139. size_t MessageDigest::getDigestLength() const
  140. {
  141. return pImpl_->getDigestLength();
  142. }
  143. void MessageDigest::reset()
  144. {
  145. pImpl_->reset();
  146. }
  147. void MessageDigest::update(const void* data, size_t length)
  148. {
  149. pImpl_->update(data, length);
  150. }
  151. void MessageDigest::digest(unsigned char* md)
  152. {
  153. pImpl_->digest(md);
  154. }
  155. std::string MessageDigest::digest()
  156. {
  157. size_t length = pImpl_->getDigestLength();
  158. array_ptr<unsigned char> buf(new unsigned char[length]);
  159. pImpl_->digest(buf);
  160. std::string hd(&buf[0], &buf[length]);
  161. return hd;
  162. }
  163. } // namespace aria2