messageDigest.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifdef ENABLE_SSL
  39. #define MAX_MD_LENGTH (16+20)
  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. # define DIGEST_ALGO_MD5 EVP_md5()
  51. # define DIGEST_ALGO_SHA1 EVP_sha1()
  52. #endif // HAVE_LIBSSL
  53. #ifdef HAVE_LIBGCRYPT
  54. typedef int DigestAlgo;
  55. # define DIGEST_ALGO_MD5 GCRY_MD_MD5
  56. # define DIGEST_ALGO_SHA1 GCRY_MD_SHA1
  57. #endif // HAVE_LIBGCRYPT
  58. private:
  59. #ifdef HAVE_LIBSSL
  60. EVP_MD_CTX ctx;
  61. #endif // HAVE_LIBSSL
  62. #ifdef HAVE_LIBGCRYPT
  63. gcry_md_hd_t ctx;
  64. #endif // HAVE_LIBGCRYPT
  65. DigestAlgo algo;
  66. public:
  67. MessageDigestContext():
  68. algo(DIGEST_ALGO_SHA1) {}
  69. MessageDigestContext(DigestAlgo algo):
  70. algo(algo) {}
  71. ~MessageDigestContext()
  72. {
  73. digestFree();
  74. }
  75. #if defined(HAVE_OLD_LIBSSL)
  76. void digestInit() {EVP_DigestInit(&ctx, algo);}
  77. void digestReset() {EVP_DigestInit(&ctx, algo);}
  78. void digestUpdate(const void* data, int length) {EVP_DigestUpdate(&ctx, data, length);}
  79. void digestFinal(unsigned char* md) {
  80. int len;
  81. EVP_DigestFinal(&ctx, md, (unsigned int*)&len);
  82. }
  83. void digestFree() {/*empty*/}
  84. int digestLength() const {
  85. return digestLength(algo);
  86. }
  87. static int digestLength(DigestAlgo algo) {
  88. return EVP_MD_size(algo);
  89. }
  90. #elif defined(HAVE_LIBSSL)
  91. void digestInit() {
  92. EVP_MD_CTX_init(&ctx);
  93. digestReset();
  94. }
  95. void digestReset() {
  96. EVP_DigestInit_ex(&ctx, algo, 0);
  97. }
  98. void digestUpdate(const void* data, int length) {
  99. EVP_DigestUpdate(&ctx, data, length);
  100. }
  101. void digestFinal(unsigned char* md) {
  102. int len;
  103. EVP_DigestFinal_ex(&ctx, md, (unsigned int*)&len);
  104. }
  105. void digestFree() {
  106. EVP_MD_CTX_cleanup(&ctx);
  107. }
  108. int digestLength() const {
  109. return digestLength(algo);
  110. }
  111. static int digestLength(DigestAlgo algo) {
  112. return EVP_MD_size(algo);
  113. }
  114. #elif defined(HAVE_LIBGCRYPT)
  115. void digestInit() {
  116. gcry_md_open(&ctx, algo, 0);
  117. }
  118. void digestReset() {
  119. gcry_md_reset(ctx);
  120. }
  121. void digestUpdate(const void* data, int length) {
  122. gcry_md_write(ctx, data, length);
  123. }
  124. void digestFinal(unsigned char* md) {
  125. gcry_md_final(ctx);
  126. memcpy(md, gcry_md_read(ctx, 0), gcry_md_get_algo_dlen(algo));
  127. }
  128. void digestFree() {
  129. gcry_md_close(ctx);
  130. }
  131. int digestLength() const {
  132. return digestLength(algo);
  133. }
  134. static int digestLength(DigestAlgo algo) {
  135. return gcry_md_get_algo_dlen(algo);
  136. }
  137. #endif // HAVE_LIBGCRYPT
  138. };
  139. typedef SharedHandle<MessageDigestContext> MessageDigestContextHandle;
  140. #endif // ENABLE_SSL
  141. #endif // _D_MESSAGE_DIGEST_H_