messageDigest.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "DlAbortEx.h"
  39. #include <map>
  40. #ifdef HAVE_LIBSSL
  41. #include <openssl/evp.h>
  42. #endif // HAVE_LIBSSL
  43. #ifdef HAVE_LIBGCRYPT
  44. #include <gcrypt.h>
  45. #endif // HAVE_LIBGCRYPT
  46. class MessageDigestContext {
  47. public:
  48. #ifdef HAVE_LIBSSL
  49. typedef const EVP_MD* DigestAlgo;
  50. #endif // HAVE_LIBSSL
  51. #ifdef HAVE_LIBGCRYPT
  52. typedef int32_t DigestAlgo;
  53. #endif // HAVE_LIBGCRYPT
  54. typedef map<string, MessageDigestContext::DigestAlgo> DigestAlgoMap;
  55. private:
  56. #ifdef HAVE_LIBSSL
  57. EVP_MD_CTX ctx;
  58. #endif // HAVE_LIBSSL
  59. #ifdef HAVE_LIBGCRYPT
  60. gcry_md_hd_t ctx;
  61. #endif // HAVE_LIBGCRYPT
  62. DigestAlgo algo;
  63. static DigestAlgoMap digestAlgos;
  64. public:
  65. MessageDigestContext():algo(getDigestAlgo("sha1"))
  66. {}
  67. ~MessageDigestContext()
  68. {
  69. digestFree();
  70. }
  71. void trySetAlgo(const string& algostring)
  72. {
  73. algo = getDigestAlgo(algostring);
  74. }
  75. static bool supports(const string& algostring)
  76. {
  77. DigestAlgoMap::const_iterator itr = digestAlgos.find(algostring);
  78. if(itr == digestAlgos.end()) {
  79. return false;
  80. } else {
  81. return true;
  82. }
  83. }
  84. static DigestAlgo getDigestAlgo(const string& algostring)
  85. {
  86. DigestAlgoMap::const_iterator itr = digestAlgos.find(algostring);
  87. if(itr == digestAlgos.end()) {
  88. throw new DlAbortEx("Digest algorithm %s is not supported.", algostring.c_str());
  89. }
  90. return (*itr).second;
  91. }
  92. static string getSupportedAlgoString()
  93. {
  94. string algos;
  95. for(DigestAlgoMap::const_iterator itr = digestAlgos.begin();
  96. itr != digestAlgos.end(); ++itr) {
  97. algos += (*itr).first+" ";
  98. }
  99. return algos;
  100. }
  101. static int digestLength(const string& algostring)
  102. {
  103. return digestLength(getDigestAlgo(algostring));
  104. }
  105. 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, int32_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. int32_t digestLength() const {
  116. return digestLength(algo);
  117. }
  118. static int32_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, int32_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. int32_t digestLength() const {
  140. return digestLength(algo);
  141. }
  142. static int32_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, int32_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. int32_t digestLength() const {
  163. return digestLength(algo);
  164. }
  165. static int digestLength(DigestAlgo algo) {
  166. return gcry_md_get_algo_dlen(algo);
  167. }
  168. #endif // HAVE_LIBGCRYPT
  169. };
  170. typedef SharedHandle<MessageDigestContext> MessageDigestContextHandle;
  171. #endif // _D_MESSAGE_DIGEST_H_