messageDigest.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_MESSAGE_DIGEST_H_
  23. #define _D_MESSAGE_DIGEST_H_
  24. #include "common.h"
  25. #ifdef ENABLE_SSL
  26. #define MAX_MD_LENGTH (16+20)
  27. #ifdef HAVE_LIBSSL
  28. #include <openssl/evp.h>
  29. #endif // HAVE_LIBSSL
  30. #ifdef HAVE_LIBGCRYPT
  31. #include <gcrypt.h>
  32. #endif // HAVE_LIBGCRYPT
  33. class MessageDigestContext {
  34. public:
  35. #ifdef HAVE_LIBSSL
  36. typedef const EVP_MD* DigestAlgo;
  37. # define DIGEST_ALGO_MD5 EVP_md5()
  38. # define DIGEST_ALGO_SHA1 EVP_sha1()
  39. #endif // HAVE_LIBSSL
  40. #ifdef HAVE_LIBGCRYPT
  41. typedef int DigestAlgo;
  42. # define DIGEST_ALGO_MD5 GCRY_MD_MD5
  43. # define DIGEST_ALGO_SHA1 GCRY_MD_SHA1
  44. #endif // HAVE_LIBGCRYPT
  45. private:
  46. #ifdef HAVE_LIBSSL
  47. EVP_MD_CTX ctx;
  48. #endif // HAVE_LIBSSL
  49. #ifdef HAVE_LIBGCRYPT
  50. gcry_md_hd_t ctx;
  51. #endif // HAVE_LIBGCRYPT
  52. DigestAlgo algo;
  53. public:
  54. MessageDigestContext():
  55. algo(DIGEST_ALGO_SHA1) {}
  56. MessageDigestContext(DigestAlgo algo):
  57. algo(algo) {}
  58. #ifdef HAVE_LIBSSL
  59. void digestInit() {
  60. EVP_MD_CTX_init(&ctx);
  61. digestReset();
  62. }
  63. void digestReset() {
  64. EVP_DigestInit_ex(&ctx, algo, 0);
  65. }
  66. void digestUpdate(const void* data, int length) {
  67. EVP_DigestUpdate(&ctx, data, length);
  68. }
  69. void digestFinal(unsigned char* md) {
  70. int len;
  71. EVP_DigestFinal_ex(&ctx, md, (unsigned int*)&len);
  72. }
  73. void digestFree() {
  74. EVP_MD_CTX_cleanup(&ctx);
  75. }
  76. int digestLength() const {
  77. return digestLength(algo);
  78. }
  79. static int digestLength(DigestAlgo algo) {
  80. return EVP_MD_size(algo);
  81. }
  82. #endif // HAVE_LIBSSL
  83. #ifdef HAVE_LIBGCRYPT
  84. void digestInit() {
  85. gcry_md_open(&ctx, algo, 0);
  86. }
  87. void digestReset() {
  88. gcry_md_reset(ctx);
  89. }
  90. void digestUpdate(const void* data, int length) {
  91. gcry_md_write(ctx, data, length);
  92. }
  93. void digestFinal(unsigned char* md) {
  94. gcry_md_final(ctx);
  95. memcpy(md, gcry_md_read(ctx, 0), gcry_md_get_algo_dlen(algo));
  96. }
  97. void digestFree() {
  98. gcry_md_close(ctx);
  99. }
  100. int digestLength() const {
  101. return digestLength(algo);
  102. }
  103. static int digestLength(DigestAlgo algo) {
  104. return gcry_md_get_algo_dlen(algo);
  105. }
  106. #endif // HAVE_LIBGCRYPT
  107. };
  108. #endif // ENABLE_SSL
  109. #endif // _D_MESSAGE_DIGEST_H_