MessageDigestHelper.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <cstring>
  37. #include <cstdlib>
  38. #include "MessageDigest.h"
  39. #include "DlAbortEx.h"
  40. #include "message.h"
  41. #include "DefaultDiskWriter.h"
  42. #include "util.h"
  43. #include "fmt.h"
  44. namespace aria2 {
  45. SharedHandle<MessageDigest> MessageDigestHelper::sha1Ctx_;
  46. void MessageDigestHelper::staticSHA1DigestInit()
  47. {
  48. staticSHA1DigestFree();
  49. sha1Ctx_ = MessageDigest::sha1();
  50. }
  51. void MessageDigestHelper::staticSHA1DigestFree()
  52. {
  53. sha1Ctx_.reset();
  54. }
  55. std::string MessageDigestHelper::staticSHA1DigestHexDigest
  56. (const BinaryStreamHandle& bs, off_t offset, uint64_t length)
  57. {
  58. sha1Ctx_->reset();
  59. return hexDigest(sha1Ctx_, bs, offset, length);
  60. }
  61. std::string MessageDigestHelper::hexDigest
  62. (const SharedHandle<MessageDigest>& ctx,
  63. const SharedHandle<BinaryStream>& bs,
  64. off_t offset, uint64_t length)
  65. {
  66. size_t BUFSIZE = 4096;
  67. unsigned char BUF[BUFSIZE];
  68. lldiv_t res = lldiv(length, BUFSIZE);
  69. uint64_t iteration = res.quot;
  70. size_t tail = res.rem;
  71. for(uint64_t i = 0; i < iteration; ++i) {
  72. ssize_t readLength = bs->readData(BUF, BUFSIZE, offset);
  73. if((size_t)readLength != BUFSIZE) {
  74. throw DL_ABORT_EX(fmt(EX_FILE_READ, "n/a", "data is too short"));
  75. }
  76. ctx->update(BUF, readLength);
  77. offset += readLength;
  78. }
  79. if(tail) {
  80. ssize_t readLength = bs->readData(BUF, tail, offset);
  81. if((size_t)readLength != tail) {
  82. throw DL_ABORT_EX(fmt(EX_FILE_READ, "n/a", "data is too short"));
  83. }
  84. ctx->update(BUF, readLength);
  85. }
  86. return ctx->hexDigest();
  87. }
  88. void MessageDigestHelper::digest
  89. (unsigned char* md, size_t mdLength,
  90. const SharedHandle<MessageDigest>& ctx, const void* data, size_t length)
  91. {
  92. size_t reqLength = ctx->getDigestLength();
  93. if(mdLength < reqLength) {
  94. throw DL_ABORT_EX
  95. (fmt("Insufficient space for storing message digest:"
  96. " %lu required, but only %lu is allocated",
  97. static_cast<unsigned long>(reqLength),
  98. static_cast<unsigned long>(mdLength)));
  99. }
  100. ctx->update(data, length);
  101. ctx->digest(md);
  102. }
  103. } // namespace aria2