MessageDigestHelper.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "MessageDigestHelper.h"
  36. #include "messageDigest.h"
  37. #include "DlAbortEx.h"
  38. #include "message.h"
  39. #include "DefaultDiskWriter.h"
  40. #include "util.h"
  41. #include "StringFormat.h"
  42. #include <cerrno>
  43. #include <cstring>
  44. #include <cstdlib>
  45. namespace aria2 {
  46. MessageDigestContext* MessageDigestHelper::sha1Ctx_ = 0;
  47. void MessageDigestHelper::staticSHA1DigestInit()
  48. {
  49. staticSHA1DigestFree();
  50. sha1Ctx_ = new MessageDigestContext();
  51. sha1Ctx_->trySetAlgo(MessageDigestContext::SHA1);
  52. sha1Ctx_->digestInit();
  53. }
  54. void MessageDigestHelper::staticSHA1DigestFree()
  55. {
  56. delete sha1Ctx_;
  57. }
  58. std::string MessageDigestHelper::staticSHA1Digest(const BinaryStreamHandle& bs,
  59. off_t offset,
  60. uint64_t length)
  61. {
  62. sha1Ctx_->digestReset();
  63. return digest(sha1Ctx_, bs, offset, length);
  64. }
  65. std::string MessageDigestHelper::digest(const std::string& algo,
  66. const BinaryStreamHandle& bs,
  67. off_t offset,
  68. uint64_t length)
  69. {
  70. MessageDigestContext ctx;
  71. ctx.trySetAlgo(algo);
  72. ctx.digestInit();
  73. return digest(&ctx, bs, offset, length);
  74. }
  75. std::string MessageDigestHelper::digest(MessageDigestContext* ctx,
  76. const SharedHandle<BinaryStream>& bs,
  77. off_t offset, uint64_t length)
  78. {
  79. size_t BUFSIZE = 4096;
  80. unsigned char BUF[BUFSIZE];
  81. lldiv_t res = lldiv(length, BUFSIZE);
  82. uint64_t iteration = res.quot;
  83. size_t tail = res.rem;
  84. for(uint64_t i = 0; i < iteration; ++i) {
  85. ssize_t readLength = bs->readData(BUF, BUFSIZE, offset);
  86. if((size_t)readLength != BUFSIZE) {
  87. throw DL_ABORT_EX
  88. (StringFormat(EX_FILE_READ, "n/a", strerror(errno)).str());
  89. }
  90. ctx->digestUpdate(BUF, readLength);
  91. offset += readLength;
  92. }
  93. if(tail) {
  94. ssize_t readLength = bs->readData(BUF, tail, offset);
  95. if((size_t)readLength != tail) {
  96. throw DL_ABORT_EX
  97. (StringFormat(EX_FILE_READ, "n/a", strerror(errno)).str());
  98. }
  99. ctx->digestUpdate(BUF, readLength);
  100. }
  101. std::string rawMD = ctx->digestFinal();
  102. return util::toHex(rawMD);
  103. }
  104. std::string MessageDigestHelper::digest(const std::string& algo, const std::string& filename)
  105. {
  106. DiskWriterHandle writer(new DefaultDiskWriter(filename));
  107. writer->openExistingFile();
  108. return digest(algo, writer, 0, writer->size());
  109. }
  110. std::string MessageDigestHelper::digest(const std::string& algo, const void* data, size_t length)
  111. {
  112. MessageDigestContext ctx;
  113. ctx.trySetAlgo(algo);
  114. ctx.digestInit();
  115. ctx.digestUpdate(data, length);
  116. std::string rawMD = ctx.digestFinal();
  117. return util::toHex(rawMD);
  118. }
  119. void MessageDigestHelper::digest(unsigned char* md, size_t mdLength,
  120. const std::string& algo, const void* data, size_t length)
  121. {
  122. if(mdLength < MessageDigestContext::digestLength(algo)) {
  123. throw DL_ABORT_EX
  124. (StringFormat("Insufficient space for storing message digest: %d required, but only %d is allocated",
  125. MessageDigestContext::digestLength(algo), mdLength).str());
  126. }
  127. MessageDigestContext ctx;
  128. ctx.trySetAlgo(algo);
  129. ctx.digestInit();
  130. ctx.digestUpdate(data, length);
  131. ctx.digestFinal(md);
  132. }
  133. } // namespace aria2