messageDigest.h 5.2 KB

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