From 74d6d6427119591a9a81f64777d90691a221f690 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 3 Jul 2013 01:13:13 +0900 Subject: [PATCH] Use std::unique_ptr for MessageDigest --- src/DHTTokenTracker.cc | 2 +- src/DownloadCommand.h | 2 +- src/HttpServerCommand.cc | 2 +- src/IteratableChecksumValidator.h | 2 +- src/IteratableChunkChecksumValidator.h | 2 +- src/LibgcryptMessageDigestImpl.cc | 11 ++++--- src/LibgcryptMessageDigestImpl.h | 19 ++++++------ src/LibnettleMessageDigestImpl.cc | 17 +++++----- src/LibnettleMessageDigestImpl.h | 19 ++++++------ src/LibsslMessageDigestImpl.cc | 22 ++++++------- src/LibsslMessageDigestImpl.h | 19 ++++++------ src/MSEHandshake.cc | 10 +++--- src/MSEHandshake.h | 2 +- src/MessageDigest.cc | 36 ++++++++++------------ src/MessageDigest.h | 22 ++++++------- src/Piece.cc | 10 +++--- src/Piece.h | 2 +- src/RpcMethodImpl.cc | 2 +- src/UTMetadataDataExtensionMessage.cc | 2 +- src/bittorrent_helper.cc | 8 ++--- src/message_digest_helper.cc | 4 +-- src/message_digest_helper.h | 4 +-- src/util.cc | 3 +- test/GZipDecoderTest.cc | 2 +- test/MessageDigestHelperTest.cc | 10 +++--- test/MessageDigestTest.cc | 2 +- test/TestUtil.cc | 3 +- test/TestUtil.h | 3 +- test/UTMetadataDataExtensionMessageTest.cc | 2 +- test/UTMetadataPostDownloadHandlerTest.cc | 2 +- 30 files changed, 124 insertions(+), 122 deletions(-) diff --git a/src/DHTTokenTracker.cc b/src/DHTTokenTracker.cc index 625ef40e..7139f517 100644 --- a/src/DHTTokenTracker.cc +++ b/src/DHTTokenTracker.cc @@ -76,7 +76,7 @@ std::string DHTTokenTracker::generateToken memcpy(src, infoHash, DHT_ID_LENGTH); memcpy(src+DHT_ID_LENGTH+COMPACT_LEN_IPV6, secret, SECRET_SIZE); unsigned char md[20]; - message_digest::digest(md, sizeof(md), MessageDigest::sha1(), + message_digest::digest(md, sizeof(md), MessageDigest::sha1().get(), src, sizeof(src)); return std::string(&md[0], &md[sizeof(md)]); } diff --git a/src/DownloadCommand.h b/src/DownloadCommand.h index a36722b2..1f38568d 100644 --- a/src/DownloadCommand.h +++ b/src/DownloadCommand.h @@ -57,7 +57,7 @@ private: #ifdef ENABLE_MESSAGE_DIGEST - std::shared_ptr messageDigest_; + std::unique_ptr messageDigest_; #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/HttpServerCommand.cc b/src/HttpServerCommand.cc index 106d1b97..096b3d1f 100644 --- a/src/HttpServerCommand.cc +++ b/src/HttpServerCommand.cc @@ -130,7 +130,7 @@ std::string createWebSocketServerKey(const std::string& clientKey) std::string src = clientKey; src += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; unsigned char digest[20]; - message_digest::digest(digest, sizeof(digest), MessageDigest::sha1(), + message_digest::digest(digest, sizeof(digest), MessageDigest::sha1().get(), src.c_str(), src.size()); return base64::encode(&digest[0], &digest[sizeof(digest)]); } diff --git a/src/IteratableChecksumValidator.h b/src/IteratableChecksumValidator.h index e747c57d..57d1f78b 100644 --- a/src/IteratableChecksumValidator.h +++ b/src/IteratableChecksumValidator.h @@ -54,7 +54,7 @@ private: int64_t currentOffset_; - std::shared_ptr ctx_; + std::unique_ptr ctx_; public: IteratableChecksumValidator(const std::shared_ptr& dctx, const std::shared_ptr& pieceStorage); diff --git a/src/IteratableChunkChecksumValidator.h b/src/IteratableChunkChecksumValidator.h index ef42d9e9..3e7375bd 100644 --- a/src/IteratableChunkChecksumValidator.h +++ b/src/IteratableChunkChecksumValidator.h @@ -54,7 +54,7 @@ private: std::shared_ptr pieceStorage_; std::shared_ptr bitfield_; size_t currentIndex_; - std::shared_ptr ctx_; + std::unique_ptr ctx_; std::string calculateActualChecksum(); diff --git a/src/LibgcryptMessageDigestImpl.cc b/src/LibgcryptMessageDigestImpl.cc index 4460c684..d6a52e57 100644 --- a/src/LibgcryptMessageDigestImpl.cc +++ b/src/LibgcryptMessageDigestImpl.cc @@ -38,10 +38,11 @@ #include "array_fun.h" #include "HashFuncEntry.h" +#include "a2functional.h" namespace aria2 { -MessageDigestImpl::MessageDigestImpl(int hashFunc):hashFunc_(hashFunc) +MessageDigestImpl::MessageDigestImpl(int hashFunc) : hashFunc_{hashFunc} { gcry_md_open(&ctx_, hashFunc_, 0); } @@ -51,9 +52,9 @@ MessageDigestImpl::~MessageDigestImpl() gcry_md_close(ctx_); } -std::shared_ptr MessageDigestImpl::sha1() +std::unique_ptr MessageDigestImpl::sha1() { - return std::shared_ptr(new MessageDigestImpl(GCRY_MD_SHA1)); + return make_unique(GCRY_MD_SHA1); } typedef HashFuncEntry CHashFuncEntry; @@ -70,12 +71,12 @@ CHashFuncEntry hashFuncs[] = { }; } // namespace -std::shared_ptr MessageDigestImpl::create +std::unique_ptr MessageDigestImpl::create (const std::string& hashType) { int hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), hashType); - return std::shared_ptr(new MessageDigestImpl(hashFunc)); + return make_unique(hashFunc); } bool MessageDigestImpl::supports(const std::string& hashType) diff --git a/src/LibgcryptMessageDigestImpl.h b/src/LibgcryptMessageDigestImpl.h index 212e09a7..56cadf14 100644 --- a/src/LibgcryptMessageDigestImpl.h +++ b/src/LibgcryptMessageDigestImpl.h @@ -45,20 +45,18 @@ namespace aria2 { class MessageDigestImpl { -private: - int hashFunc_; - gcry_md_hd_t ctx_; - +public: MessageDigestImpl(int hashFunc); // We don't implement copy ctor. - MessageDigestImpl(const MessageDigestImpl&); + MessageDigestImpl(const MessageDigestImpl&) = delete; // We don't implement assignment operator. - MessageDigestImpl& operator==(const MessageDigestImpl&); -public: + MessageDigestImpl& operator==(const MessageDigestImpl&) = delete; + ~MessageDigestImpl(); - static std::shared_ptr sha1(); - static std::shared_ptr create(const std::string& hashType); + static std::unique_ptr sha1(); + static std::unique_ptr create + (const std::string& hashType); static bool supports(const std::string& hashType); static size_t getDigestLength(const std::string& hashType); @@ -67,6 +65,9 @@ public: void reset(); void update(const void* data, size_t length); void digest(unsigned char* md); +private: + int hashFunc_; + gcry_md_hd_t ctx_; }; } // namespace aria2 diff --git a/src/LibnettleMessageDigestImpl.cc b/src/LibnettleMessageDigestImpl.cc index 9919ffcd..5bb1326a 100644 --- a/src/LibnettleMessageDigestImpl.cc +++ b/src/LibnettleMessageDigestImpl.cc @@ -38,12 +38,13 @@ #include "array_fun.h" #include "HashFuncEntry.h" +#include "a2functional.h" namespace aria2 { MessageDigestImpl::MessageDigestImpl(const nettle_hash* hashInfo) - : hashInfo_(hashInfo), - ctx_(new char[hashInfo->context_size]) + : hashInfo_{hashInfo}, + ctx_{new char[hashInfo->context_size]} { reset(); } @@ -53,9 +54,9 @@ MessageDigestImpl::~MessageDigestImpl() delete [] ctx_; } -std::shared_ptr MessageDigestImpl::sha1() +std::unique_ptr MessageDigestImpl::sha1() { - return std::shared_ptr(new MessageDigestImpl(&nettle_sha1)); + return make_unique(&nettle_sha1); } typedef HashFuncEntry CHashFuncEntry; @@ -72,12 +73,12 @@ CHashFuncEntry hashFuncs[] = { }; } // namespace -std::shared_ptr MessageDigestImpl::create +std::unique_ptr MessageDigestImpl::create (const std::string& hashType) { - const nettle_hash* hashInfo = + auto hashInfo = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), hashType); - return std::shared_ptr(new MessageDigestImpl(hashInfo)); + return make_unique(hashInfo); } bool MessageDigestImpl::supports(const std::string& hashType) @@ -89,7 +90,7 @@ bool MessageDigestImpl::supports(const std::string& hashType) size_t MessageDigestImpl::getDigestLength(const std::string& hashType) { - const nettle_hash* hashInfo = + auto hashInfo = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), hashType); return hashInfo->digest_size; } diff --git a/src/LibnettleMessageDigestImpl.h b/src/LibnettleMessageDigestImpl.h index 2028885f..4d0b3d4c 100644 --- a/src/LibnettleMessageDigestImpl.h +++ b/src/LibnettleMessageDigestImpl.h @@ -45,20 +45,18 @@ namespace aria2 { class MessageDigestImpl { -private: - const nettle_hash* hashInfo_; - char* ctx_; - +public: MessageDigestImpl(const nettle_hash* hashInfo); // We don't implement copy ctor. - MessageDigestImpl(const MessageDigestImpl&); + MessageDigestImpl(const MessageDigestImpl&) = delete; // We don't implement assignment operator. - MessageDigestImpl& operator==(const MessageDigestImpl&); -public: + MessageDigestImpl& operator==(const MessageDigestImpl&) = delete; + ~MessageDigestImpl(); - static std::shared_ptr sha1(); - static std::shared_ptr create(const std::string& hashType); + static std::unique_ptr sha1(); + static std::unique_ptr create + (const std::string& hashType); static bool supports(const std::string& hashType); static size_t getDigestLength(const std::string& hashType); @@ -67,6 +65,9 @@ public: void reset(); void update(const void* data, size_t length); void digest(unsigned char* md); +private: + const nettle_hash* hashInfo_; + char* ctx_; }; } // namespace aria2 diff --git a/src/LibsslMessageDigestImpl.cc b/src/LibsslMessageDigestImpl.cc index 00f7f596..eea12505 100644 --- a/src/LibsslMessageDigestImpl.cc +++ b/src/LibsslMessageDigestImpl.cc @@ -38,10 +38,12 @@ #include "array_fun.h" #include "HashFuncEntry.h" +#include "a2functional.h" namespace aria2 { -MessageDigestImpl::MessageDigestImpl(const EVP_MD* hashFunc):hashFunc_(hashFunc) +MessageDigestImpl::MessageDigestImpl(const EVP_MD* hashFunc) + : hashFunc_{hashFunc} { EVP_MD_CTX_init(&ctx_); reset(); @@ -52,9 +54,9 @@ MessageDigestImpl::~MessageDigestImpl() EVP_MD_CTX_cleanup(&ctx_); } -std::shared_ptr MessageDigestImpl::sha1() +std::unique_ptr MessageDigestImpl::sha1() { - return std::shared_ptr(new MessageDigestImpl(EVP_sha1())); + return make_unique(EVP_sha1()); } typedef HashFuncEntry CHashFuncEntry; @@ -79,13 +81,12 @@ CHashFuncEntry hashFuncs[] = { }; } // namespace -std::shared_ptr MessageDigestImpl::create +std::unique_ptr MessageDigestImpl::create (const std::string& hashType) { - const EVP_MD* hashFunc = getHashFunc(std::begin(hashFuncs), - std::end(hashFuncs), - hashType); - return std::shared_ptr(new MessageDigestImpl(hashFunc)); + auto hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), + hashType); + return make_unique(hashFunc); } bool MessageDigestImpl::supports(const std::string& hashType) @@ -97,9 +98,8 @@ bool MessageDigestImpl::supports(const std::string& hashType) size_t MessageDigestImpl::getDigestLength(const std::string& hashType) { - const EVP_MD* hashFunc = getHashFunc(std::begin(hashFuncs), - std::end(hashFuncs), - hashType); + auto hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), + hashType); return EVP_MD_size(hashFunc); } diff --git a/src/LibsslMessageDigestImpl.h b/src/LibsslMessageDigestImpl.h index 6d3a89f8..d739ec37 100644 --- a/src/LibsslMessageDigestImpl.h +++ b/src/LibsslMessageDigestImpl.h @@ -45,20 +45,18 @@ namespace aria2 { class MessageDigestImpl { -private: - const EVP_MD* hashFunc_; - EVP_MD_CTX ctx_; - +public: MessageDigestImpl(const EVP_MD* hashFunc); // We don't implement copy ctor. - MessageDigestImpl(const MessageDigestImpl&); + MessageDigestImpl(const MessageDigestImpl&) = delete; // We don't implement assignment operator. - MessageDigestImpl& operator==(const MessageDigestImpl&); -public: + MessageDigestImpl& operator==(const MessageDigestImpl&) = delete; + ~MessageDigestImpl(); - static std::shared_ptr sha1(); - static std::shared_ptr create(const std::string& hashType); + static std::unique_ptr sha1(); + static std::unique_ptr create + (const std::string& hashType); static bool supports(const std::string& hashType); static size_t getDigestLength(const std::string& hashType); @@ -67,6 +65,9 @@ public: void reset(); void update(const void* data, size_t length); void digest(unsigned char* md); +private: + const EVP_MD* hashFunc_; + EVP_MD_CTX ctx_; }; } // namespace aria2 diff --git a/src/MSEHandshake.cc b/src/MSEHandshake.cc index b78a8b2e..a6e43f21 100644 --- a/src/MSEHandshake.cc +++ b/src/MSEHandshake.cc @@ -194,7 +194,7 @@ void MSEHandshake::initCipher(const unsigned char* infoHash) unsigned char localCipherKey[20]; sha1_->reset(); message_digest::digest(localCipherKey, sizeof(localCipherKey), - sha1_, s, sizeof(s)); + sha1_.get(), s, sizeof(s)); encryptor_.reset(new ARC4Encryptor()); encryptor_->init(localCipherKey, sizeof(localCipherKey)); @@ -202,7 +202,7 @@ void MSEHandshake::initCipher(const unsigned char* infoHash) memcpy(s, initiator_?"keyB":"keyA", 4); sha1_->reset(); message_digest::digest(peerCipherKey, sizeof(peerCipherKey), - sha1_, s, sizeof(s)); + sha1_.get(), s, sizeof(s)); decryptor_.reset(new ARC4Encryptor()); decryptor_->init(peerCipherKey, sizeof(peerCipherKey)); @@ -234,7 +234,7 @@ void MSEHandshake::createReq1Hash(unsigned char* md) const memcpy(buffer, "req1", 4); memcpy(buffer+4, secret_, KEY_LENGTH); sha1_->reset(); - message_digest::digest(md, 20, sha1_, buffer, 4+KEY_LENGTH); + message_digest::digest(md, 20, sha1_.get(), buffer, 4+KEY_LENGTH); } void MSEHandshake::createReq23Hash(unsigned char* md, const unsigned char* infoHash) const @@ -244,14 +244,14 @@ void MSEHandshake::createReq23Hash(unsigned char* md, const unsigned char* infoH memcpy(x+4, infoHash, INFO_HASH_LENGTH); unsigned char xh[20]; sha1_->reset(); - message_digest::digest(xh, sizeof(xh), sha1_, x, sizeof(x)); + message_digest::digest(xh, sizeof(xh), sha1_.get(), x, sizeof(x)); unsigned char y[4+96]; memcpy(y, "req3", 4); memcpy(y+4, secret_, KEY_LENGTH); unsigned char yh[20]; sha1_->reset(); - message_digest::digest(yh, sizeof(yh), sha1_, y, sizeof(y)); + message_digest::digest(yh, sizeof(yh), sha1_.get(), y, sizeof(y)); for(size_t i = 0; i < 20; ++i) { md[i] = xh[i]^yh[i]; diff --git a/src/MSEHandshake.h b/src/MSEHandshake.h index 68b00ef1..307969dc 100644 --- a/src/MSEHandshake.h +++ b/src/MSEHandshake.h @@ -98,7 +98,7 @@ private: uint16_t padLength_; uint16_t iaLength_; unsigned char* ia_; - std::shared_ptr sha1_; + std::unique_ptr sha1_; void encryptAndSendData(unsigned char* data, size_t length); diff --git a/src/MessageDigest.cc b/src/MessageDigest.cc index e4557f8c..01189c7f 100644 --- a/src/MessageDigest.cc +++ b/src/MessageDigest.cc @@ -63,24 +63,21 @@ HashTypeEntry hashTypes[] = { }; } // namespace aria2 -MessageDigest::MessageDigest() +MessageDigest::MessageDigest(std::unique_ptr impl) + : pImpl_{std::move(impl)} {} -MessageDigest::~MessageDigest() -{} +MessageDigest::~MessageDigest() {} -std::shared_ptr MessageDigest::sha1() +std::unique_ptr MessageDigest::sha1() { - std::shared_ptr md(new MessageDigest()); - md->pImpl_ = MessageDigestImpl::sha1(); - return md; + return make_unique(MessageDigestImpl::sha1()); } -std::shared_ptr MessageDigest::create(const std::string& hashType) +std::unique_ptr MessageDigest::create +(const std::string& hashType) { - std::shared_ptr md(new MessageDigest()); - md->pImpl_ = MessageDigestImpl::create(hashType); - return md; + return make_unique(MessageDigestImpl::create(hashType)); } bool MessageDigest::supports(const std::string& hashType) @@ -101,10 +98,11 @@ std::vector MessageDigest::getSupportedHashTypes() std::string MessageDigest::getSupportedHashTypeString() { - std::vector ht = getSupportedHashTypes(); + auto ht = getSupportedHashTypes(); std::stringstream ss; - std::copy(ht.begin(), ht.end(), std::ostream_iterator(ss, ", ")); - std::string res = ss.str(); + std::copy(std::begin(ht), std::end(ht), + std::ostream_iterator(ss, ", ")); + auto res = ss.str(); if(!res.empty()) { res.erase(ss.str().length()-2); } @@ -133,12 +131,10 @@ public: bool MessageDigest::isStronger(const std::string& lhs, const std::string& rhs) { - HashTypeEntry* lEntry = std::find_if(std::begin(hashTypes), - std::end(hashTypes), - FindHashTypeEntry(lhs)); - HashTypeEntry* rEntry = std::find_if(std::begin(hashTypes), - std::end(hashTypes), - FindHashTypeEntry(rhs)); + auto lEntry = std::find_if(std::begin(hashTypes), std::end(hashTypes), + FindHashTypeEntry(lhs)); + auto rEntry = std::find_if(std::begin(hashTypes), std::end(hashTypes), + FindHashTypeEntry(rhs)); if(lEntry == std::end(hashTypes) || rEntry == std::end(hashTypes)) { return false; } diff --git a/src/MessageDigest.h b/src/MessageDigest.h index d9c619b8..705a7c7b 100644 --- a/src/MessageDigest.h +++ b/src/MessageDigest.h @@ -46,24 +46,22 @@ namespace aria2 { class MessageDigestImpl; class MessageDigest { -private: - std::shared_ptr pImpl_; - - MessageDigest(); - - // We don't implement copy ctor. - MessageDigest(const MessageDigest&); - // We don't implement assignment operator. - MessageDigest& operator=(const MessageDigest&); public: + // Made public for make_unique + MessageDigest(std::unique_ptr impl); + // We don't implement copy ctor. + MessageDigest(const MessageDigest&) = delete; + // We don't implement assignment operator. + MessageDigest& operator=(const MessageDigest&) = delete; + ~MessageDigest(); // Factory functions - static std::shared_ptr sha1(); + static std::unique_ptr sha1(); // Factory function which takes hashType as string. Throws // exception if hashType is not supported. - static std::shared_ptr create(const std::string& hashType); + static std::unique_ptr create(const std::string& hashType); // Returns true if hashType is supported. Otherwise returns false. static bool supports(const std::string& hashType); @@ -107,6 +105,8 @@ public: // Returns raw digest, not hex digest. This call can only be called // once. To reuse this object, call reset(). std::string digest(); +private: + std::unique_ptr pImpl_; }; } // namespace aria2 diff --git a/src/Piece.cc b/src/Piece.cc index 521788f2..24e5beff 100644 --- a/src/Piece.cc +++ b/src/Piece.cc @@ -245,7 +245,7 @@ std::string Piece::getDigest() } namespace { -void updateHashWithRead(const std::shared_ptr& mdctx, +void updateHashWithRead(MessageDigest* mdctx, const std::shared_ptr& adaptor, int64_t offset, size_t len) { @@ -274,7 +274,7 @@ void updateHashWithRead(const std::shared_ptr& mdctx, std::string Piece::getDigestWithWrCache (size_t pieceLength, const std::shared_ptr& adaptor) { - std::shared_ptr mdctx(MessageDigest::create(hashType_)); + auto mdctx = MessageDigest::create(hashType_); int64_t start = static_cast(index_)*pieceLength; int64_t goff = start; if(wrCache_) { @@ -282,14 +282,14 @@ std::string Piece::getDigestWithWrCache for(WrDiskCacheEntry::DataCellSet::iterator i = dataSet.begin(), eoi = dataSet.end(); i != eoi; ++i) { if(goff < (*i)->goff) { - updateHashWithRead(mdctx, adaptor, goff, (*i)->goff - goff); + updateHashWithRead(mdctx.get(), adaptor, goff, (*i)->goff - goff); } mdctx->update((*i)->data+(*i)->offset, (*i)->len); goff = (*i)->goff + (*i)->len; } - updateHashWithRead(mdctx, adaptor, goff, start+length_-goff); + updateHashWithRead(mdctx.get(), adaptor, goff, start+length_-goff); } else { - updateHashWithRead(mdctx, adaptor, goff, length_); + updateHashWithRead(mdctx.get(), adaptor, goff, length_); } return mdctx->digest(); } diff --git a/src/Piece.h b/src/Piece.h index bdba7dd2..ea38b291 100644 --- a/src/Piece.h +++ b/src/Piece.h @@ -72,7 +72,7 @@ private: std::string hashType_; - std::shared_ptr mdctx_; + std::unique_ptr mdctx_; #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index dfd5f67a..43d8748d 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -252,7 +252,7 @@ namespace { std::string getHexSha1(const std::string& s) { unsigned char hash[20]; - message_digest::digest(hash, sizeof(hash), MessageDigest::sha1(), + message_digest::digest(hash, sizeof(hash), MessageDigest::sha1().get(), s.data(), s.size()); return util::toHex(hash, sizeof(hash)); } diff --git a/src/UTMetadataDataExtensionMessage.cc b/src/UTMetadataDataExtensionMessage.cc index 208749c0..c08ef287 100644 --- a/src/UTMetadataDataExtensionMessage.cc +++ b/src/UTMetadataDataExtensionMessage.cc @@ -90,7 +90,7 @@ void UTMetadataDataExtensionMessage::doReceivedAction() std::string metadata = util::toString(pieceStorage_->getDiskAdaptor()); unsigned char infoHash[INFO_HASH_LENGTH]; message_digest::digest(infoHash, INFO_HASH_LENGTH, - MessageDigest::sha1(), + MessageDigest::sha1().get(), metadata.data(), metadata.size()); if(memcmp(infoHash, bittorrent::getInfoHash(dctx_), INFO_HASH_LENGTH) == 0) { diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 6fc3fba1..b4f26d65 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -428,7 +428,7 @@ void processRootDictionary std::string encodedInfoDict = bencode2::encode(infoDict); unsigned char infoHash[INFO_HASH_LENGTH]; message_digest::digest(infoHash, INFO_HASH_LENGTH, - MessageDigest::sha1(), + MessageDigest::sha1().get(), encodedInfoDict.data(), encodedInfoDict.size()); torrent->infoHash.assign(&infoHash[0], &infoHash[INFO_HASH_LENGTH]); @@ -675,8 +675,8 @@ void computeFastSet } memcpy(tx+4, infoHash, 20); unsigned char x[20]; - std::shared_ptr sha1 = MessageDigest::sha1(); - message_digest::digest(x, sizeof(x), sha1, tx, 24); + auto sha1 = MessageDigest::sha1(); + message_digest::digest(x, sizeof(x), sha1.get(), tx, 24); while(fastSet.size() < fastSetSize) { for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; ++i) { size_t j = i*4; @@ -690,7 +690,7 @@ void computeFastSet } unsigned char temp[20]; sha1->reset(); - message_digest::digest(temp, sizeof(temp), sha1, x, sizeof(x)); + message_digest::digest(temp, sizeof(temp), sha1.get(), x, sizeof(x)); memcpy(x, temp, sizeof(x)); } } diff --git a/src/message_digest_helper.cc b/src/message_digest_helper.cc index 2f1eb2f6..80b41b42 100644 --- a/src/message_digest_helper.cc +++ b/src/message_digest_helper.cc @@ -49,7 +49,7 @@ namespace aria2 { namespace message_digest { std::string digest -(const std::shared_ptr& ctx, +(MessageDigest* ctx, const std::shared_ptr& bs, int64_t offset, int64_t length) { @@ -78,7 +78,7 @@ std::string digest void digest (unsigned char* md, size_t mdLength, - const std::shared_ptr& ctx, const void* data, size_t length) + MessageDigest* ctx, const void* data, size_t length) { size_t reqLength = ctx->getDigestLength(); if(mdLength < reqLength) { diff --git a/src/message_digest_helper.h b/src/message_digest_helper.h index 9a05eecf..199606ed 100644 --- a/src/message_digest_helper.h +++ b/src/message_digest_helper.h @@ -52,7 +52,7 @@ namespace message_digest { * Returns raw digest string, not hex digest */ std::string digest -(const std::shared_ptr& ctx, +(MessageDigest* ctx, const std::shared_ptr& bs, int64_t offset, int64_t length); @@ -62,7 +62,7 @@ std::string digest */ void digest (unsigned char* md, size_t mdLength, - const std::shared_ptr& ctx, + MessageDigest* ctx, const void* data, size_t length); } // namespace message_digest diff --git a/src/util.cc b/src/util.cc index 7931bfba..b59fecbc 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1602,7 +1602,8 @@ void generateRandomKey(unsigned char* key) #ifdef ENABLE_MESSAGE_DIGEST unsigned char bytes[40]; generateRandomData(bytes, sizeof(bytes)); - message_digest::digest(key, 20, MessageDigest::sha1(), bytes, sizeof(bytes)); + message_digest::digest(key, 20, MessageDigest::sha1().get(), bytes, + sizeof(bytes)); #else // !ENABLE_MESSAGE_DIGEST generateRandomData(key, 20); #endif // !ENABLE_MESSAGE_DIGEST diff --git a/test/GZipDecoderTest.cc b/test/GZipDecoderTest.cc index 463d5ce4..40bc512d 100644 --- a/test/GZipDecoderTest.cc +++ b/test/GZipDecoderTest.cc @@ -55,7 +55,7 @@ void GZipDecoderTest::testDecode() #ifdef ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT_EQUAL(std::string("8b577b33c0411b2be9d4fa74c7402d54a8d21f96"), - fileHexDigest(MessageDigest::sha1(), outfile)); + fileHexDigest(MessageDigest::sha1().get(), outfile)); #endif // ENABLE_MESSAGE_DIGEST } diff --git a/test/MessageDigestHelperTest.cc b/test/MessageDigestHelperTest.cc index 0cc45bdf..c345fd50 100644 --- a/test/MessageDigestHelperTest.cc +++ b/test/MessageDigestHelperTest.cc @@ -26,17 +26,19 @@ public: CPPUNIT_TEST_SUITE_REGISTRATION( MessageDigestHelperTest ); void MessageDigestHelperTest::testDigestDiskWriter() { - std::shared_ptr diskio - (new DefaultDiskWriter(A2_TEST_DIR"/4096chunk.txt")); + auto diskio = std::make_shared + (A2_TEST_DIR"/4096chunk.txt"); diskio->enableReadOnly(); diskio->openExistingFile(); CPPUNIT_ASSERT_EQUAL(std::string("608cabc0f2fa18c260cafd974516865c772363d5"), util::toHex(message_digest::digest - (MessageDigest::sha1(), diskio, 0, 4096))); + (MessageDigest::sha1().get(), + diskio, 0, 4096))); CPPUNIT_ASSERT_EQUAL(std::string("7a4a9ae537ebbbb826b1060e704490ad0f365ead"), util::toHex(message_digest::digest - (MessageDigest::sha1(), diskio, 5, 100))); + (MessageDigest::sha1().get(), + diskio, 5, 100))); } } // namespace aria2 diff --git a/test/MessageDigestTest.cc b/test/MessageDigestTest.cc index b1618afc..04170a0c 100644 --- a/test/MessageDigestTest.cc +++ b/test/MessageDigestTest.cc @@ -17,7 +17,7 @@ class MessageDigestTest:public CppUnit::TestFixture { CPPUNIT_TEST(testGetCanonicalHashType); CPPUNIT_TEST_SUITE_END(); - std::shared_ptr sha1_; + std::unique_ptr sha1_; public: void setUp() { diff --git a/test/TestUtil.cc b/test/TestUtil.cc index f3ca8e03..da70c5ef 100644 --- a/test/TestUtil.cc +++ b/test/TestUtil.cc @@ -81,8 +81,7 @@ std::string fromHex(const std::string& s) } #ifdef ENABLE_MESSAGE_DIGEST -std::string fileHexDigest -(const std::shared_ptr& ctx, const std::string& filename) +std::string fileHexDigest(MessageDigest* ctx, const std::string& filename) { std::shared_ptr writer(new DefaultDiskWriter(filename)); writer->openExistingFile(); diff --git a/test/TestUtil.h b/test/TestUtil.h index b1e95ad8..a559e6a9 100644 --- a/test/TestUtil.h +++ b/test/TestUtil.h @@ -52,8 +52,7 @@ std::string fromHex(const std::string& s); #ifdef ENABLE_MESSAGE_DIGEST // Returns hex digest of contents of file denoted by filename. -std::string fileHexDigest -(const std::shared_ptr& ctx, const std::string& filename); +std::string fileHexDigest(MessageDigest* ctx, const std::string& filename); #endif // ENABLE_MESSAGE_DIGEST WrDiskCacheEntry::DataCell* createDataCell(int64_t goff, diff --git a/test/UTMetadataDataExtensionMessageTest.cc b/test/UTMetadataDataExtensionMessageTest.cc index 9e1a4af8..a2388bc9 100644 --- a/test/UTMetadataDataExtensionMessageTest.cc +++ b/test/UTMetadataDataExtensionMessageTest.cc @@ -79,7 +79,7 @@ void UTMetadataDataExtensionMessageTest::testDoReceivedAction() unsigned char infoHash[INFO_HASH_LENGTH]; message_digest::digest(infoHash, INFO_HASH_LENGTH, - MessageDigest::sha1(), + MessageDigest::sha1().get(), metadata.data(), metadata.size()); { auto attrs = make_unique(); diff --git a/test/UTMetadataPostDownloadHandlerTest.cc b/test/UTMetadataPostDownloadHandlerTest.cc index 81b99115..f82b63d0 100644 --- a/test/UTMetadataPostDownloadHandlerTest.cc +++ b/test/UTMetadataPostDownloadHandlerTest.cc @@ -74,7 +74,7 @@ void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups() "6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCe"; unsigned char infoHash[20]; message_digest::digest - (infoHash, sizeof(infoHash), MessageDigest::sha1(), + (infoHash, sizeof(infoHash), MessageDigest::sha1().get(), reinterpret_cast(metadata.data()), metadata.size()); dctx_->getFirstFileEntry()->setLength(metadata.size()); std::vector > announceList;