messageDigest.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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() {EVP_DigestInit(&ctx, algo);}
  100. void digestReset() {EVP_DigestInit(&ctx, algo);}
  101. void digestUpdate(const void* data, size_t length) {EVP_DigestUpdate(&ctx, data, length);}
  102. void digestFinal(unsigned char* md) {
  103. unsigned int len;
  104. EVP_DigestFinal(&ctx, md, &len);
  105. }
  106. void digestFree() {/*empty*/}
  107. size_t digestLength() const {
  108. return digestLength(algo);
  109. }
  110. static size_t digestLength(DigestAlgo algo) {
  111. return EVP_MD_size(algo);
  112. }
  113. #elif defined(HAVE_LIBSSL)
  114. void digestInit() {
  115. EVP_MD_CTX_init(&ctx);
  116. digestReset();
  117. }
  118. void digestReset() {
  119. EVP_DigestInit_ex(&ctx, algo, 0);
  120. }
  121. void digestUpdate(const void* data, size_t length) {
  122. EVP_DigestUpdate(&ctx, data, length);
  123. }
  124. void digestFinal(unsigned char* md) {
  125. unsigned int len;
  126. EVP_DigestFinal_ex(&ctx, md, &len);
  127. }
  128. void digestFree() {
  129. EVP_MD_CTX_cleanup(&ctx);
  130. }
  131. size_t digestLength() const {
  132. return digestLength(algo);
  133. }
  134. static size_t digestLength(DigestAlgo algo) {
  135. return EVP_MD_size(algo);
  136. }
  137. #elif defined(HAVE_LIBGCRYPT)
  138. void digestInit() {
  139. gcry_md_open(&ctx, algo, 0);
  140. }
  141. void digestReset() {
  142. gcry_md_reset(ctx);
  143. }
  144. void digestUpdate(const void* data, size_t length) {
  145. gcry_md_write(ctx, data, length);
  146. }
  147. void digestFinal(unsigned char* md) {
  148. gcry_md_final(ctx);
  149. memcpy(md, gcry_md_read(ctx, 0), gcry_md_get_algo_dlen(algo));
  150. }
  151. void digestFree() {
  152. gcry_md_close(ctx);
  153. }
  154. size_t digestLength() const {
  155. return digestLength(algo);
  156. }
  157. static size_t digestLength(DigestAlgo algo) {
  158. return gcry_md_get_algo_dlen(algo);
  159. }
  160. #endif // HAVE_LIBGCRYPT
  161. };
  162. typedef SharedHandle<MessageDigestContext> MessageDigestContextHandle;
  163. } // namespace aria2
  164. #endif // _D_MESSAGE_DIGEST_H_