messageDigest.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. typedef std::map<std::string, MessageDigestContext::DigestAlgo> DigestAlgoMap;
  59. static const std::string SHA1;
  60. static const std::string SHA256;
  61. static const std::string MD5;
  62. private:
  63. #ifdef HAVE_LIBSSL
  64. EVP_MD_CTX ctx;
  65. #endif // HAVE_LIBSSL
  66. #ifdef HAVE_LIBGCRYPT
  67. gcry_md_hd_t ctx;
  68. #endif // HAVE_LIBGCRYPT
  69. DigestAlgo algo;
  70. static DigestAlgoMap digestAlgos;
  71. public:
  72. MessageDigestContext():algo(getDigestAlgo(MessageDigestContext::SHA1))
  73. {}
  74. ~MessageDigestContext()
  75. {
  76. digestFree();
  77. }
  78. void trySetAlgo(const std::string& algostring)
  79. {
  80. algo = getDigestAlgo(algostring);
  81. }
  82. static bool supports(const std::string& algostring)
  83. {
  84. DigestAlgoMap::const_iterator itr = digestAlgos.find(algostring);
  85. if(itr == digestAlgos.end()) {
  86. return false;
  87. } else {
  88. return true;
  89. }
  90. }
  91. static DigestAlgo getDigestAlgo(const std::string& algostring)
  92. {
  93. DigestAlgoMap::const_iterator itr = digestAlgos.find(algostring);
  94. if(itr == digestAlgos.end()) {
  95. throw DL_ABORT_EX
  96. (StringFormat("Digest algorithm %s is not supported.",
  97. algostring.c_str()).str());
  98. }
  99. return (*itr).second;
  100. }
  101. static std::string getSupportedAlgoString();
  102. static size_t digestLength(const std::string& algostring)
  103. {
  104. return digestLength(getDigestAlgo(algostring));
  105. }
  106. std::string digestFinal();
  107. #if defined(HAVE_OLD_LIBSSL)
  108. void digestInit() {EVP_DigestInit(&ctx, algo);}
  109. void digestReset() {EVP_DigestInit(&ctx, algo);}
  110. void digestUpdate(const void* data, size_t length) {EVP_DigestUpdate(&ctx, data, length);}
  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_