messageDigest.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "SharedHandle.h"
  39. #include "DlAbortEx.h"
  40. #include <map>
  41. #ifdef HAVE_LIBSSL
  42. #include <openssl/evp.h>
  43. #endif // HAVE_LIBSSL
  44. #ifdef HAVE_LIBGCRYPT
  45. #include <gcrypt.h>
  46. #endif // HAVE_LIBGCRYPT
  47. namespace aria2 {
  48. class MessageDigestContext {
  49. public:
  50. #ifdef HAVE_LIBSSL
  51. typedef const EVP_MD* DigestAlgo;
  52. #endif // HAVE_LIBSSL
  53. #ifdef HAVE_LIBGCRYPT
  54. typedef int DigestAlgo;
  55. #endif // HAVE_LIBGCRYPT
  56. typedef std::map<std::string, MessageDigestContext::DigestAlgo> DigestAlgoMap;
  57. private:
  58. #ifdef HAVE_LIBSSL
  59. EVP_MD_CTX ctx;
  60. #endif // HAVE_LIBSSL
  61. #ifdef HAVE_LIBGCRYPT
  62. gcry_md_hd_t ctx;
  63. #endif // HAVE_LIBGCRYPT
  64. DigestAlgo algo;
  65. static DigestAlgoMap digestAlgos;
  66. public:
  67. MessageDigestContext():algo(getDigestAlgo("sha1"))
  68. {}
  69. ~MessageDigestContext()
  70. {
  71. digestFree();
  72. }
  73. void trySetAlgo(const std::string& algostring)
  74. {
  75. algo = getDigestAlgo(algostring);
  76. }
  77. static bool supports(const std::string& algostring)
  78. {
  79. DigestAlgoMap::const_iterator itr = digestAlgos.find(algostring);
  80. if(itr == digestAlgos.end()) {
  81. return false;
  82. } else {
  83. return true;
  84. }
  85. }
  86. static DigestAlgo getDigestAlgo(const std::string& algostring)
  87. {
  88. DigestAlgoMap::const_iterator itr = digestAlgos.find(algostring);
  89. if(itr == digestAlgos.end()) {
  90. throw new DlAbortEx("Digest algorithm %s is not supported.", algostring.c_str());
  91. }
  92. return (*itr).second;
  93. }
  94. static std::string getSupportedAlgoString()
  95. {
  96. std::string algos;
  97. for(DigestAlgoMap::const_iterator itr = digestAlgos.begin();
  98. itr != digestAlgos.end(); ++itr) {
  99. algos += (*itr).first+" ";
  100. }
  101. return algos;
  102. }
  103. static size_t digestLength(const std::string& algostring)
  104. {
  105. return digestLength(getDigestAlgo(algostring));
  106. }
  107. std::string digestFinal();
  108. #if defined(HAVE_OLD_LIBSSL)
  109. void digestInit() {EVP_DigestInit(&ctx, algo);}
  110. void digestReset() {EVP_DigestInit(&ctx, algo);}
  111. void digestUpdate(const void* data, size_t length) {EVP_DigestUpdate(&ctx, data, length);}
  112. void digestFinal(unsigned char* md) {
  113. unsigned int len;
  114. EVP_DigestFinal(&ctx, md, &len);
  115. }
  116. void digestFree() {/*empty*/}
  117. size_t digestLength() const {
  118. return digestLength(algo);
  119. }
  120. static size_t digestLength(DigestAlgo algo) {
  121. return EVP_MD_size(algo);
  122. }
  123. #elif defined(HAVE_LIBSSL)
  124. void digestInit() {
  125. EVP_MD_CTX_init(&ctx);
  126. digestReset();
  127. }
  128. void digestReset() {
  129. EVP_DigestInit_ex(&ctx, algo, 0);
  130. }
  131. void digestUpdate(const void* data, size_t length) {
  132. EVP_DigestUpdate(&ctx, data, length);
  133. }
  134. void digestFinal(unsigned char* md) {
  135. unsigned int len;
  136. EVP_DigestFinal_ex(&ctx, md, &len);
  137. }
  138. void digestFree() {
  139. EVP_MD_CTX_cleanup(&ctx);
  140. }
  141. size_t digestLength() const {
  142. return digestLength(algo);
  143. }
  144. static size_t digestLength(DigestAlgo algo) {
  145. return EVP_MD_size(algo);
  146. }
  147. #elif defined(HAVE_LIBGCRYPT)
  148. void digestInit() {
  149. gcry_md_open(&ctx, algo, 0);
  150. }
  151. void digestReset() {
  152. gcry_md_reset(ctx);
  153. }
  154. void digestUpdate(const void* data, size_t length) {
  155. gcry_md_write(ctx, data, length);
  156. }
  157. void digestFinal(unsigned char* md) {
  158. gcry_md_final(ctx);
  159. memcpy(md, gcry_md_read(ctx, 0), gcry_md_get_algo_dlen(algo));
  160. }
  161. void digestFree() {
  162. gcry_md_close(ctx);
  163. }
  164. size_t digestLength() const {
  165. return digestLength(algo);
  166. }
  167. static size_t digestLength(DigestAlgo algo) {
  168. return gcry_md_get_algo_dlen(algo);
  169. }
  170. #endif // HAVE_LIBGCRYPT
  171. };
  172. typedef SharedHandle<MessageDigestContext> MessageDigestContextHandle;
  173. } // namespace aria2
  174. #endif // _D_MESSAGE_DIGEST_H_