messageDigest.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #ifndef _D_MESSAGE_DIGEST_H_
  36. #define _D_MESSAGE_DIGEST_H_
  37. #include "common.h"
  38. #include <cstring>
  39. #include <map>
  40. #include "SharedHandle.h"
  41. #include "DlAbortEx.h"
  42. #include "StringFormat.h"
  43. #ifdef HAVE_LIBSSL
  44. #include <openssl/evp.h>
  45. #endif // HAVE_LIBSSL
  46. #ifdef HAVE_LIBGCRYPT
  47. #include <gcrypt.h>
  48. #endif // HAVE_LIBGCRYPT
  49. namespace aria2 {
  50. class MessageDigestContext {
  51. public:
  52. #ifdef HAVE_LIBSSL
  53. typedef const EVP_MD* DigestAlgo;
  54. #endif // HAVE_LIBSSL
  55. #ifdef HAVE_LIBGCRYPT
  56. typedef int DigestAlgo;
  57. #endif // HAVE_LIBGCRYPT
  58. static const std::string SHA1;
  59. static const std::string SHA256;
  60. static const std::string MD5;
  61. private:
  62. #ifdef HAVE_LIBSSL
  63. EVP_MD_CTX _ctx;
  64. #endif // HAVE_LIBSSL
  65. #ifdef HAVE_LIBGCRYPT
  66. gcry_md_hd_t _ctx;
  67. #endif // HAVE_LIBGCRYPT
  68. DigestAlgo _algo;
  69. public:
  70. MessageDigestContext():_algo(getDigestAlgo(MessageDigestContext::SHA1))
  71. {}
  72. ~MessageDigestContext()
  73. {
  74. digestFree();
  75. }
  76. void trySetAlgo(const std::string& algostring)
  77. {
  78. _algo = getDigestAlgo(algostring);
  79. }
  80. static bool supports(const std::string& algostring);
  81. static DigestAlgo getDigestAlgo(const std::string& algostring);
  82. static std::string getSupportedAlgoString();
  83. static size_t digestLength(const std::string& algostring)
  84. {
  85. return digestLength(getDigestAlgo(algostring));
  86. }
  87. // Returns true if hash algorithm specified by lhs is stronger than
  88. // the one specified by rhs. If either lhs or rhs is not supported
  89. // or both are not supported, returns false.
  90. static bool isStronger(const std::string& lhs, const std::string& rhs);
  91. static bool isValidHash
  92. (const std::string& algostring, const std::string& hashstring);
  93. // Returns canonical hash algorithm name of given algostring. If
  94. // given algostring is not supported, then returns algostring
  95. // unchanged.
  96. static std::string getCanonicalAlgo(const std::string& algostring);
  97. std::string digestFinal();
  98. #if defined(HAVE_OLD_LIBSSL)
  99. void digestInit()
  100. {
  101. EVP_DigestInit(&_ctx, _algo);
  102. }
  103. void digestReset()
  104. {
  105. EVP_DigestInit(&_ctx, _algo);
  106. }
  107. void digestUpdate(const void* data, size_t length)
  108. {
  109. EVP_DigestUpdate(&_ctx, data, length);
  110. }
  111. void digestFinal(unsigned char* md) {
  112. unsigned int len;
  113. EVP_DigestFinal(&_ctx, md, &len);
  114. }
  115. void digestFree() {/*empty*/}
  116. size_t digestLength() const {
  117. return digestLength(_algo);
  118. }
  119. static size_t digestLength(DigestAlgo algo) {
  120. return EVP_MD_size(algo);
  121. }
  122. #elif defined(HAVE_LIBSSL)
  123. void digestInit() {
  124. EVP_MD_CTX_init(&_ctx);
  125. digestReset();
  126. }
  127. void digestReset() {
  128. EVP_DigestInit_ex(&_ctx, _algo, 0);
  129. }
  130. void digestUpdate(const void* data, size_t length) {
  131. EVP_DigestUpdate(&_ctx, data, length);
  132. }
  133. void digestFinal(unsigned char* md) {
  134. unsigned int len;
  135. EVP_DigestFinal_ex(&_ctx, md, &len);
  136. }
  137. void digestFree() {
  138. EVP_MD_CTX_cleanup(&_ctx);
  139. }
  140. size_t digestLength() const {
  141. return digestLength(_algo);
  142. }
  143. static size_t digestLength(DigestAlgo algo) {
  144. return EVP_MD_size(algo);
  145. }
  146. #elif defined(HAVE_LIBGCRYPT)
  147. void digestInit() {
  148. gcry_md_open(&_ctx, _algo, 0);
  149. }
  150. void digestReset() {
  151. gcry_md_reset(_ctx);
  152. }
  153. void digestUpdate(const void* data, size_t length) {
  154. gcry_md_write(_ctx, data, length);
  155. }
  156. void digestFinal(unsigned char* md) {
  157. gcry_md_final(_ctx);
  158. memcpy(md, gcry_md_read(_ctx, 0), gcry_md_get_algo_dlen(_algo));
  159. }
  160. void digestFree() {
  161. gcry_md_close(_ctx);
  162. }
  163. size_t digestLength() const {
  164. return digestLength(_algo);
  165. }
  166. static size_t digestLength(DigestAlgo algo) {
  167. return gcry_md_get_algo_dlen(algo);
  168. }
  169. #endif // HAVE_LIBGCRYPT
  170. };
  171. typedef SharedHandle<MessageDigestContext> MessageDigestContextHandle;
  172. } // namespace aria2
  173. #endif // _D_MESSAGE_DIGEST_H_