Use std::unique_ptr for MessageDigest

pull/103/head
Tatsuhiro Tsujikawa 2013-07-03 01:13:13 +09:00
parent f2d85c38cc
commit 74d6d64271
30 changed files with 124 additions and 122 deletions

View File

@ -76,7 +76,7 @@ std::string DHTTokenTracker::generateToken
memcpy(src, infoHash, DHT_ID_LENGTH); memcpy(src, infoHash, DHT_ID_LENGTH);
memcpy(src+DHT_ID_LENGTH+COMPACT_LEN_IPV6, secret, SECRET_SIZE); memcpy(src+DHT_ID_LENGTH+COMPACT_LEN_IPV6, secret, SECRET_SIZE);
unsigned char md[20]; unsigned char md[20];
message_digest::digest(md, sizeof(md), MessageDigest::sha1(), message_digest::digest(md, sizeof(md), MessageDigest::sha1().get(),
src, sizeof(src)); src, sizeof(src));
return std::string(&md[0], &md[sizeof(md)]); return std::string(&md[0], &md[sizeof(md)]);
} }

View File

@ -57,7 +57,7 @@ private:
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
std::shared_ptr<MessageDigest> messageDigest_; std::unique_ptr<MessageDigest> messageDigest_;
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST

View File

@ -130,7 +130,7 @@ std::string createWebSocketServerKey(const std::string& clientKey)
std::string src = clientKey; std::string src = clientKey;
src += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; src += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
unsigned char digest[20]; 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()); src.c_str(), src.size());
return base64::encode(&digest[0], &digest[sizeof(digest)]); return base64::encode(&digest[0], &digest[sizeof(digest)]);
} }

View File

@ -54,7 +54,7 @@ private:
int64_t currentOffset_; int64_t currentOffset_;
std::shared_ptr<MessageDigest> ctx_; std::unique_ptr<MessageDigest> ctx_;
public: public:
IteratableChecksumValidator(const std::shared_ptr<DownloadContext>& dctx, IteratableChecksumValidator(const std::shared_ptr<DownloadContext>& dctx,
const std::shared_ptr<PieceStorage>& pieceStorage); const std::shared_ptr<PieceStorage>& pieceStorage);

View File

@ -54,7 +54,7 @@ private:
std::shared_ptr<PieceStorage> pieceStorage_; std::shared_ptr<PieceStorage> pieceStorage_;
std::shared_ptr<BitfieldMan> bitfield_; std::shared_ptr<BitfieldMan> bitfield_;
size_t currentIndex_; size_t currentIndex_;
std::shared_ptr<MessageDigest> ctx_; std::unique_ptr<MessageDigest> ctx_;
std::string calculateActualChecksum(); std::string calculateActualChecksum();

View File

@ -38,10 +38,11 @@
#include "array_fun.h" #include "array_fun.h"
#include "HashFuncEntry.h" #include "HashFuncEntry.h"
#include "a2functional.h"
namespace aria2 { namespace aria2 {
MessageDigestImpl::MessageDigestImpl(int hashFunc):hashFunc_(hashFunc) MessageDigestImpl::MessageDigestImpl(int hashFunc) : hashFunc_{hashFunc}
{ {
gcry_md_open(&ctx_, hashFunc_, 0); gcry_md_open(&ctx_, hashFunc_, 0);
} }
@ -51,9 +52,9 @@ MessageDigestImpl::~MessageDigestImpl()
gcry_md_close(ctx_); gcry_md_close(ctx_);
} }
std::shared_ptr<MessageDigestImpl> MessageDigestImpl::sha1() std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
{ {
return std::shared_ptr<MessageDigestImpl>(new MessageDigestImpl(GCRY_MD_SHA1)); return make_unique<MessageDigestImpl>(GCRY_MD_SHA1);
} }
typedef HashFuncEntry<int> CHashFuncEntry; typedef HashFuncEntry<int> CHashFuncEntry;
@ -70,12 +71,12 @@ CHashFuncEntry hashFuncs[] = {
}; };
} // namespace } // namespace
std::shared_ptr<MessageDigestImpl> MessageDigestImpl::create std::unique_ptr<MessageDigestImpl> MessageDigestImpl::create
(const std::string& hashType) (const std::string& hashType)
{ {
int hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), int hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs),
hashType); hashType);
return std::shared_ptr<MessageDigestImpl>(new MessageDigestImpl(hashFunc)); return make_unique<MessageDigestImpl>(hashFunc);
} }
bool MessageDigestImpl::supports(const std::string& hashType) bool MessageDigestImpl::supports(const std::string& hashType)

View File

@ -45,20 +45,18 @@
namespace aria2 { namespace aria2 {
class MessageDigestImpl { class MessageDigestImpl {
private: public:
int hashFunc_;
gcry_md_hd_t ctx_;
MessageDigestImpl(int hashFunc); MessageDigestImpl(int hashFunc);
// We don't implement copy ctor. // We don't implement copy ctor.
MessageDigestImpl(const MessageDigestImpl&); MessageDigestImpl(const MessageDigestImpl&) = delete;
// We don't implement assignment operator. // We don't implement assignment operator.
MessageDigestImpl& operator==(const MessageDigestImpl&); MessageDigestImpl& operator==(const MessageDigestImpl&) = delete;
public:
~MessageDigestImpl(); ~MessageDigestImpl();
static std::shared_ptr<MessageDigestImpl> sha1(); static std::unique_ptr<MessageDigestImpl> sha1();
static std::shared_ptr<MessageDigestImpl> create(const std::string& hashType); static std::unique_ptr<MessageDigestImpl> create
(const std::string& hashType);
static bool supports(const std::string& hashType); static bool supports(const std::string& hashType);
static size_t getDigestLength(const std::string& hashType); static size_t getDigestLength(const std::string& hashType);
@ -67,6 +65,9 @@ public:
void reset(); void reset();
void update(const void* data, size_t length); void update(const void* data, size_t length);
void digest(unsigned char* md); void digest(unsigned char* md);
private:
int hashFunc_;
gcry_md_hd_t ctx_;
}; };
} // namespace aria2 } // namespace aria2

View File

@ -38,12 +38,13 @@
#include "array_fun.h" #include "array_fun.h"
#include "HashFuncEntry.h" #include "HashFuncEntry.h"
#include "a2functional.h"
namespace aria2 { namespace aria2 {
MessageDigestImpl::MessageDigestImpl(const nettle_hash* hashInfo) MessageDigestImpl::MessageDigestImpl(const nettle_hash* hashInfo)
: hashInfo_(hashInfo), : hashInfo_{hashInfo},
ctx_(new char[hashInfo->context_size]) ctx_{new char[hashInfo->context_size]}
{ {
reset(); reset();
} }
@ -53,9 +54,9 @@ MessageDigestImpl::~MessageDigestImpl()
delete [] ctx_; delete [] ctx_;
} }
std::shared_ptr<MessageDigestImpl> MessageDigestImpl::sha1() std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
{ {
return std::shared_ptr<MessageDigestImpl>(new MessageDigestImpl(&nettle_sha1)); return make_unique<MessageDigestImpl>(&nettle_sha1);
} }
typedef HashFuncEntry<const nettle_hash*> CHashFuncEntry; typedef HashFuncEntry<const nettle_hash*> CHashFuncEntry;
@ -72,12 +73,12 @@ CHashFuncEntry hashFuncs[] = {
}; };
} // namespace } // namespace
std::shared_ptr<MessageDigestImpl> MessageDigestImpl::create std::unique_ptr<MessageDigestImpl> MessageDigestImpl::create
(const std::string& hashType) (const std::string& hashType)
{ {
const nettle_hash* hashInfo = auto hashInfo =
getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), hashType); getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), hashType);
return std::shared_ptr<MessageDigestImpl>(new MessageDigestImpl(hashInfo)); return make_unique<MessageDigestImpl>(hashInfo);
} }
bool MessageDigestImpl::supports(const std::string& hashType) 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) size_t MessageDigestImpl::getDigestLength(const std::string& hashType)
{ {
const nettle_hash* hashInfo = auto hashInfo =
getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), hashType); getHashFunc(std::begin(hashFuncs), std::end(hashFuncs), hashType);
return hashInfo->digest_size; return hashInfo->digest_size;
} }

View File

@ -45,20 +45,18 @@
namespace aria2 { namespace aria2 {
class MessageDigestImpl { class MessageDigestImpl {
private: public:
const nettle_hash* hashInfo_;
char* ctx_;
MessageDigestImpl(const nettle_hash* hashInfo); MessageDigestImpl(const nettle_hash* hashInfo);
// We don't implement copy ctor. // We don't implement copy ctor.
MessageDigestImpl(const MessageDigestImpl&); MessageDigestImpl(const MessageDigestImpl&) = delete;
// We don't implement assignment operator. // We don't implement assignment operator.
MessageDigestImpl& operator==(const MessageDigestImpl&); MessageDigestImpl& operator==(const MessageDigestImpl&) = delete;
public:
~MessageDigestImpl(); ~MessageDigestImpl();
static std::shared_ptr<MessageDigestImpl> sha1(); static std::unique_ptr<MessageDigestImpl> sha1();
static std::shared_ptr<MessageDigestImpl> create(const std::string& hashType); static std::unique_ptr<MessageDigestImpl> create
(const std::string& hashType);
static bool supports(const std::string& hashType); static bool supports(const std::string& hashType);
static size_t getDigestLength(const std::string& hashType); static size_t getDigestLength(const std::string& hashType);
@ -67,6 +65,9 @@ public:
void reset(); void reset();
void update(const void* data, size_t length); void update(const void* data, size_t length);
void digest(unsigned char* md); void digest(unsigned char* md);
private:
const nettle_hash* hashInfo_;
char* ctx_;
}; };
} // namespace aria2 } // namespace aria2

View File

@ -38,10 +38,12 @@
#include "array_fun.h" #include "array_fun.h"
#include "HashFuncEntry.h" #include "HashFuncEntry.h"
#include "a2functional.h"
namespace aria2 { namespace aria2 {
MessageDigestImpl::MessageDigestImpl(const EVP_MD* hashFunc):hashFunc_(hashFunc) MessageDigestImpl::MessageDigestImpl(const EVP_MD* hashFunc)
: hashFunc_{hashFunc}
{ {
EVP_MD_CTX_init(&ctx_); EVP_MD_CTX_init(&ctx_);
reset(); reset();
@ -52,9 +54,9 @@ MessageDigestImpl::~MessageDigestImpl()
EVP_MD_CTX_cleanup(&ctx_); EVP_MD_CTX_cleanup(&ctx_);
} }
std::shared_ptr<MessageDigestImpl> MessageDigestImpl::sha1() std::unique_ptr<MessageDigestImpl> MessageDigestImpl::sha1()
{ {
return std::shared_ptr<MessageDigestImpl>(new MessageDigestImpl(EVP_sha1())); return make_unique<MessageDigestImpl>(EVP_sha1());
} }
typedef HashFuncEntry<const EVP_MD*> CHashFuncEntry; typedef HashFuncEntry<const EVP_MD*> CHashFuncEntry;
@ -79,13 +81,12 @@ CHashFuncEntry hashFuncs[] = {
}; };
} // namespace } // namespace
std::shared_ptr<MessageDigestImpl> MessageDigestImpl::create std::unique_ptr<MessageDigestImpl> MessageDigestImpl::create
(const std::string& hashType) (const std::string& hashType)
{ {
const EVP_MD* hashFunc = getHashFunc(std::begin(hashFuncs), auto hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs),
std::end(hashFuncs), hashType);
hashType); return make_unique<MessageDigestImpl>(hashFunc);
return std::shared_ptr<MessageDigestImpl>(new MessageDigestImpl(hashFunc));
} }
bool MessageDigestImpl::supports(const std::string& hashType) 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) size_t MessageDigestImpl::getDigestLength(const std::string& hashType)
{ {
const EVP_MD* hashFunc = getHashFunc(std::begin(hashFuncs), auto hashFunc = getHashFunc(std::begin(hashFuncs), std::end(hashFuncs),
std::end(hashFuncs), hashType);
hashType);
return EVP_MD_size(hashFunc); return EVP_MD_size(hashFunc);
} }

View File

@ -45,20 +45,18 @@
namespace aria2 { namespace aria2 {
class MessageDigestImpl { class MessageDigestImpl {
private: public:
const EVP_MD* hashFunc_;
EVP_MD_CTX ctx_;
MessageDigestImpl(const EVP_MD* hashFunc); MessageDigestImpl(const EVP_MD* hashFunc);
// We don't implement copy ctor. // We don't implement copy ctor.
MessageDigestImpl(const MessageDigestImpl&); MessageDigestImpl(const MessageDigestImpl&) = delete;
// We don't implement assignment operator. // We don't implement assignment operator.
MessageDigestImpl& operator==(const MessageDigestImpl&); MessageDigestImpl& operator==(const MessageDigestImpl&) = delete;
public:
~MessageDigestImpl(); ~MessageDigestImpl();
static std::shared_ptr<MessageDigestImpl> sha1(); static std::unique_ptr<MessageDigestImpl> sha1();
static std::shared_ptr<MessageDigestImpl> create(const std::string& hashType); static std::unique_ptr<MessageDigestImpl> create
(const std::string& hashType);
static bool supports(const std::string& hashType); static bool supports(const std::string& hashType);
static size_t getDigestLength(const std::string& hashType); static size_t getDigestLength(const std::string& hashType);
@ -67,6 +65,9 @@ public:
void reset(); void reset();
void update(const void* data, size_t length); void update(const void* data, size_t length);
void digest(unsigned char* md); void digest(unsigned char* md);
private:
const EVP_MD* hashFunc_;
EVP_MD_CTX ctx_;
}; };
} // namespace aria2 } // namespace aria2

View File

@ -194,7 +194,7 @@ void MSEHandshake::initCipher(const unsigned char* infoHash)
unsigned char localCipherKey[20]; unsigned char localCipherKey[20];
sha1_->reset(); sha1_->reset();
message_digest::digest(localCipherKey, sizeof(localCipherKey), message_digest::digest(localCipherKey, sizeof(localCipherKey),
sha1_, s, sizeof(s)); sha1_.get(), s, sizeof(s));
encryptor_.reset(new ARC4Encryptor()); encryptor_.reset(new ARC4Encryptor());
encryptor_->init(localCipherKey, sizeof(localCipherKey)); encryptor_->init(localCipherKey, sizeof(localCipherKey));
@ -202,7 +202,7 @@ void MSEHandshake::initCipher(const unsigned char* infoHash)
memcpy(s, initiator_?"keyB":"keyA", 4); memcpy(s, initiator_?"keyB":"keyA", 4);
sha1_->reset(); sha1_->reset();
message_digest::digest(peerCipherKey, sizeof(peerCipherKey), message_digest::digest(peerCipherKey, sizeof(peerCipherKey),
sha1_, s, sizeof(s)); sha1_.get(), s, sizeof(s));
decryptor_.reset(new ARC4Encryptor()); decryptor_.reset(new ARC4Encryptor());
decryptor_->init(peerCipherKey, sizeof(peerCipherKey)); decryptor_->init(peerCipherKey, sizeof(peerCipherKey));
@ -234,7 +234,7 @@ void MSEHandshake::createReq1Hash(unsigned char* md) const
memcpy(buffer, "req1", 4); memcpy(buffer, "req1", 4);
memcpy(buffer+4, secret_, KEY_LENGTH); memcpy(buffer+4, secret_, KEY_LENGTH);
sha1_->reset(); 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 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); memcpy(x+4, infoHash, INFO_HASH_LENGTH);
unsigned char xh[20]; unsigned char xh[20];
sha1_->reset(); 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]; unsigned char y[4+96];
memcpy(y, "req3", 4); memcpy(y, "req3", 4);
memcpy(y+4, secret_, KEY_LENGTH); memcpy(y+4, secret_, KEY_LENGTH);
unsigned char yh[20]; unsigned char yh[20];
sha1_->reset(); 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) { for(size_t i = 0; i < 20; ++i) {
md[i] = xh[i]^yh[i]; md[i] = xh[i]^yh[i];

View File

@ -98,7 +98,7 @@ private:
uint16_t padLength_; uint16_t padLength_;
uint16_t iaLength_; uint16_t iaLength_;
unsigned char* ia_; unsigned char* ia_;
std::shared_ptr<MessageDigest> sha1_; std::unique_ptr<MessageDigest> sha1_;
void encryptAndSendData(unsigned char* data, size_t length); void encryptAndSendData(unsigned char* data, size_t length);

View File

@ -63,24 +63,21 @@ HashTypeEntry hashTypes[] = {
}; };
} // namespace aria2 } // namespace aria2
MessageDigest::MessageDigest() MessageDigest::MessageDigest(std::unique_ptr<MessageDigestImpl> impl)
: pImpl_{std::move(impl)}
{} {}
MessageDigest::~MessageDigest() MessageDigest::~MessageDigest() {}
{}
std::shared_ptr<MessageDigest> MessageDigest::sha1() std::unique_ptr<MessageDigest> MessageDigest::sha1()
{ {
std::shared_ptr<MessageDigest> md(new MessageDigest()); return make_unique<MessageDigest>(MessageDigestImpl::sha1());
md->pImpl_ = MessageDigestImpl::sha1();
return md;
} }
std::shared_ptr<MessageDigest> MessageDigest::create(const std::string& hashType) std::unique_ptr<MessageDigest> MessageDigest::create
(const std::string& hashType)
{ {
std::shared_ptr<MessageDigest> md(new MessageDigest()); return make_unique<MessageDigest>(MessageDigestImpl::create(hashType));
md->pImpl_ = MessageDigestImpl::create(hashType);
return md;
} }
bool MessageDigest::supports(const std::string& hashType) bool MessageDigest::supports(const std::string& hashType)
@ -101,10 +98,11 @@ std::vector<std::string> MessageDigest::getSupportedHashTypes()
std::string MessageDigest::getSupportedHashTypeString() std::string MessageDigest::getSupportedHashTypeString()
{ {
std::vector<std::string> ht = getSupportedHashTypes(); auto ht = getSupportedHashTypes();
std::stringstream ss; std::stringstream ss;
std::copy(ht.begin(), ht.end(), std::ostream_iterator<std::string>(ss, ", ")); std::copy(std::begin(ht), std::end(ht),
std::string res = ss.str(); std::ostream_iterator<std::string>(ss, ", "));
auto res = ss.str();
if(!res.empty()) { if(!res.empty()) {
res.erase(ss.str().length()-2); res.erase(ss.str().length()-2);
} }
@ -133,12 +131,10 @@ public:
bool MessageDigest::isStronger(const std::string& lhs, const std::string& rhs) bool MessageDigest::isStronger(const std::string& lhs, const std::string& rhs)
{ {
HashTypeEntry* lEntry = std::find_if(std::begin(hashTypes), auto lEntry = std::find_if(std::begin(hashTypes), std::end(hashTypes),
std::end(hashTypes), FindHashTypeEntry(lhs));
FindHashTypeEntry(lhs)); auto rEntry = std::find_if(std::begin(hashTypes), std::end(hashTypes),
HashTypeEntry* rEntry = std::find_if(std::begin(hashTypes), FindHashTypeEntry(rhs));
std::end(hashTypes),
FindHashTypeEntry(rhs));
if(lEntry == std::end(hashTypes) || rEntry == std::end(hashTypes)) { if(lEntry == std::end(hashTypes) || rEntry == std::end(hashTypes)) {
return false; return false;
} }

View File

@ -46,24 +46,22 @@ namespace aria2 {
class MessageDigestImpl; class MessageDigestImpl;
class MessageDigest { class MessageDigest {
private:
std::shared_ptr<MessageDigestImpl> pImpl_;
MessageDigest();
// We don't implement copy ctor.
MessageDigest(const MessageDigest&);
// We don't implement assignment operator.
MessageDigest& operator=(const MessageDigest&);
public: public:
// Made public for make_unique
MessageDigest(std::unique_ptr<MessageDigestImpl> impl);
// We don't implement copy ctor.
MessageDigest(const MessageDigest&) = delete;
// We don't implement assignment operator.
MessageDigest& operator=(const MessageDigest&) = delete;
~MessageDigest(); ~MessageDigest();
// Factory functions // Factory functions
static std::shared_ptr<MessageDigest> sha1(); static std::unique_ptr<MessageDigest> sha1();
// Factory function which takes hashType as string. Throws // Factory function which takes hashType as string. Throws
// exception if hashType is not supported. // exception if hashType is not supported.
static std::shared_ptr<MessageDigest> create(const std::string& hashType); static std::unique_ptr<MessageDigest> create(const std::string& hashType);
// Returns true if hashType is supported. Otherwise returns false. // Returns true if hashType is supported. Otherwise returns false.
static bool supports(const std::string& hashType); static bool supports(const std::string& hashType);
@ -107,6 +105,8 @@ public:
// Returns raw digest, not hex digest. This call can only be called // Returns raw digest, not hex digest. This call can only be called
// once. To reuse this object, call reset(). // once. To reuse this object, call reset().
std::string digest(); std::string digest();
private:
std::unique_ptr<MessageDigestImpl> pImpl_;
}; };
} // namespace aria2 } // namespace aria2

View File

@ -245,7 +245,7 @@ std::string Piece::getDigest()
} }
namespace { namespace {
void updateHashWithRead(const std::shared_ptr<MessageDigest>& mdctx, void updateHashWithRead(MessageDigest* mdctx,
const std::shared_ptr<DiskAdaptor>& adaptor, const std::shared_ptr<DiskAdaptor>& adaptor,
int64_t offset, size_t len) int64_t offset, size_t len)
{ {
@ -274,7 +274,7 @@ void updateHashWithRead(const std::shared_ptr<MessageDigest>& mdctx,
std::string Piece::getDigestWithWrCache std::string Piece::getDigestWithWrCache
(size_t pieceLength, const std::shared_ptr<DiskAdaptor>& adaptor) (size_t pieceLength, const std::shared_ptr<DiskAdaptor>& adaptor)
{ {
std::shared_ptr<MessageDigest> mdctx(MessageDigest::create(hashType_)); auto mdctx = MessageDigest::create(hashType_);
int64_t start = static_cast<int64_t>(index_)*pieceLength; int64_t start = static_cast<int64_t>(index_)*pieceLength;
int64_t goff = start; int64_t goff = start;
if(wrCache_) { if(wrCache_) {
@ -282,14 +282,14 @@ std::string Piece::getDigestWithWrCache
for(WrDiskCacheEntry::DataCellSet::iterator i = dataSet.begin(), for(WrDiskCacheEntry::DataCellSet::iterator i = dataSet.begin(),
eoi = dataSet.end(); i != eoi; ++i) { eoi = dataSet.end(); i != eoi; ++i) {
if(goff < (*i)->goff) { 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); mdctx->update((*i)->data+(*i)->offset, (*i)->len);
goff = (*i)->goff + (*i)->len; goff = (*i)->goff + (*i)->len;
} }
updateHashWithRead(mdctx, adaptor, goff, start+length_-goff); updateHashWithRead(mdctx.get(), adaptor, goff, start+length_-goff);
} else { } else {
updateHashWithRead(mdctx, adaptor, goff, length_); updateHashWithRead(mdctx.get(), adaptor, goff, length_);
} }
return mdctx->digest(); return mdctx->digest();
} }

View File

@ -72,7 +72,7 @@ private:
std::string hashType_; std::string hashType_;
std::shared_ptr<MessageDigest> mdctx_; std::unique_ptr<MessageDigest> mdctx_;
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST

View File

@ -252,7 +252,7 @@ namespace {
std::string getHexSha1(const std::string& s) std::string getHexSha1(const std::string& s)
{ {
unsigned char hash[20]; 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()); s.data(), s.size());
return util::toHex(hash, sizeof(hash)); return util::toHex(hash, sizeof(hash));
} }

View File

@ -90,7 +90,7 @@ void UTMetadataDataExtensionMessage::doReceivedAction()
std::string metadata = util::toString(pieceStorage_->getDiskAdaptor()); std::string metadata = util::toString(pieceStorage_->getDiskAdaptor());
unsigned char infoHash[INFO_HASH_LENGTH]; unsigned char infoHash[INFO_HASH_LENGTH];
message_digest::digest(infoHash, INFO_HASH_LENGTH, message_digest::digest(infoHash, INFO_HASH_LENGTH,
MessageDigest::sha1(), MessageDigest::sha1().get(),
metadata.data(), metadata.size()); metadata.data(), metadata.size());
if(memcmp(infoHash, bittorrent::getInfoHash(dctx_), if(memcmp(infoHash, bittorrent::getInfoHash(dctx_),
INFO_HASH_LENGTH) == 0) { INFO_HASH_LENGTH) == 0) {

View File

@ -428,7 +428,7 @@ void processRootDictionary
std::string encodedInfoDict = bencode2::encode(infoDict); std::string encodedInfoDict = bencode2::encode(infoDict);
unsigned char infoHash[INFO_HASH_LENGTH]; unsigned char infoHash[INFO_HASH_LENGTH];
message_digest::digest(infoHash, INFO_HASH_LENGTH, message_digest::digest(infoHash, INFO_HASH_LENGTH,
MessageDigest::sha1(), MessageDigest::sha1().get(),
encodedInfoDict.data(), encodedInfoDict.data(),
encodedInfoDict.size()); encodedInfoDict.size());
torrent->infoHash.assign(&infoHash[0], &infoHash[INFO_HASH_LENGTH]); torrent->infoHash.assign(&infoHash[0], &infoHash[INFO_HASH_LENGTH]);
@ -675,8 +675,8 @@ void computeFastSet
} }
memcpy(tx+4, infoHash, 20); memcpy(tx+4, infoHash, 20);
unsigned char x[20]; unsigned char x[20];
std::shared_ptr<MessageDigest> sha1 = MessageDigest::sha1(); auto sha1 = MessageDigest::sha1();
message_digest::digest(x, sizeof(x), sha1, tx, 24); message_digest::digest(x, sizeof(x), sha1.get(), tx, 24);
while(fastSet.size() < fastSetSize) { while(fastSet.size() < fastSetSize) {
for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; ++i) { for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; ++i) {
size_t j = i*4; size_t j = i*4;
@ -690,7 +690,7 @@ void computeFastSet
} }
unsigned char temp[20]; unsigned char temp[20];
sha1->reset(); 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)); memcpy(x, temp, sizeof(x));
} }
} }

View File

@ -49,7 +49,7 @@ namespace aria2 {
namespace message_digest { namespace message_digest {
std::string digest std::string digest
(const std::shared_ptr<MessageDigest>& ctx, (MessageDigest* ctx,
const std::shared_ptr<BinaryStream>& bs, const std::shared_ptr<BinaryStream>& bs,
int64_t offset, int64_t length) int64_t offset, int64_t length)
{ {
@ -78,7 +78,7 @@ std::string digest
void digest void digest
(unsigned char* md, size_t mdLength, (unsigned char* md, size_t mdLength,
const std::shared_ptr<MessageDigest>& ctx, const void* data, size_t length) MessageDigest* ctx, const void* data, size_t length)
{ {
size_t reqLength = ctx->getDigestLength(); size_t reqLength = ctx->getDigestLength();
if(mdLength < reqLength) { if(mdLength < reqLength) {

View File

@ -52,7 +52,7 @@ namespace message_digest {
* Returns raw digest string, not hex digest * Returns raw digest string, not hex digest
*/ */
std::string digest std::string digest
(const std::shared_ptr<MessageDigest>& ctx, (MessageDigest* ctx,
const std::shared_ptr<BinaryStream>& bs, const std::shared_ptr<BinaryStream>& bs,
int64_t offset, int64_t length); int64_t offset, int64_t length);
@ -62,7 +62,7 @@ std::string digest
*/ */
void digest void digest
(unsigned char* md, size_t mdLength, (unsigned char* md, size_t mdLength,
const std::shared_ptr<MessageDigest>& ctx, MessageDigest* ctx,
const void* data, size_t length); const void* data, size_t length);
} // namespace message_digest } // namespace message_digest

View File

@ -1602,7 +1602,8 @@ void generateRandomKey(unsigned char* key)
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
unsigned char bytes[40]; unsigned char bytes[40];
generateRandomData(bytes, sizeof(bytes)); 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 #else // !ENABLE_MESSAGE_DIGEST
generateRandomData(key, 20); generateRandomData(key, 20);
#endif // !ENABLE_MESSAGE_DIGEST #endif // !ENABLE_MESSAGE_DIGEST

View File

@ -55,7 +55,7 @@ void GZipDecoderTest::testDecode()
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
CPPUNIT_ASSERT_EQUAL(std::string("8b577b33c0411b2be9d4fa74c7402d54a8d21f96"), CPPUNIT_ASSERT_EQUAL(std::string("8b577b33c0411b2be9d4fa74c7402d54a8d21f96"),
fileHexDigest(MessageDigest::sha1(), outfile)); fileHexDigest(MessageDigest::sha1().get(), outfile));
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
} }

View File

@ -26,17 +26,19 @@ public:
CPPUNIT_TEST_SUITE_REGISTRATION( MessageDigestHelperTest ); CPPUNIT_TEST_SUITE_REGISTRATION( MessageDigestHelperTest );
void MessageDigestHelperTest::testDigestDiskWriter() { void MessageDigestHelperTest::testDigestDiskWriter() {
std::shared_ptr<DefaultDiskWriter> diskio auto diskio = std::make_shared<DefaultDiskWriter>
(new DefaultDiskWriter(A2_TEST_DIR"/4096chunk.txt")); (A2_TEST_DIR"/4096chunk.txt");
diskio->enableReadOnly(); diskio->enableReadOnly();
diskio->openExistingFile(); diskio->openExistingFile();
CPPUNIT_ASSERT_EQUAL(std::string("608cabc0f2fa18c260cafd974516865c772363d5"), CPPUNIT_ASSERT_EQUAL(std::string("608cabc0f2fa18c260cafd974516865c772363d5"),
util::toHex(message_digest::digest util::toHex(message_digest::digest
(MessageDigest::sha1(), diskio, 0, 4096))); (MessageDigest::sha1().get(),
diskio, 0, 4096)));
CPPUNIT_ASSERT_EQUAL(std::string("7a4a9ae537ebbbb826b1060e704490ad0f365ead"), CPPUNIT_ASSERT_EQUAL(std::string("7a4a9ae537ebbbb826b1060e704490ad0f365ead"),
util::toHex(message_digest::digest util::toHex(message_digest::digest
(MessageDigest::sha1(), diskio, 5, 100))); (MessageDigest::sha1().get(),
diskio, 5, 100)));
} }
} // namespace aria2 } // namespace aria2

View File

@ -17,7 +17,7 @@ class MessageDigestTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testGetCanonicalHashType); CPPUNIT_TEST(testGetCanonicalHashType);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
std::shared_ptr<MessageDigest> sha1_; std::unique_ptr<MessageDigest> sha1_;
public: public:
void setUp() void setUp()
{ {

View File

@ -81,8 +81,7 @@ std::string fromHex(const std::string& s)
} }
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
std::string fileHexDigest std::string fileHexDigest(MessageDigest* ctx, const std::string& filename)
(const std::shared_ptr<MessageDigest>& ctx, const std::string& filename)
{ {
std::shared_ptr<DiskWriter> writer(new DefaultDiskWriter(filename)); std::shared_ptr<DiskWriter> writer(new DefaultDiskWriter(filename));
writer->openExistingFile(); writer->openExistingFile();

View File

@ -52,8 +52,7 @@ std::string fromHex(const std::string& s);
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
// Returns hex digest of contents of file denoted by filename. // Returns hex digest of contents of file denoted by filename.
std::string fileHexDigest std::string fileHexDigest(MessageDigest* ctx, const std::string& filename);
(const std::shared_ptr<MessageDigest>& ctx, const std::string& filename);
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
WrDiskCacheEntry::DataCell* createDataCell(int64_t goff, WrDiskCacheEntry::DataCell* createDataCell(int64_t goff,

View File

@ -79,7 +79,7 @@ void UTMetadataDataExtensionMessageTest::testDoReceivedAction()
unsigned char infoHash[INFO_HASH_LENGTH]; unsigned char infoHash[INFO_HASH_LENGTH];
message_digest::digest(infoHash, INFO_HASH_LENGTH, message_digest::digest(infoHash, INFO_HASH_LENGTH,
MessageDigest::sha1(), MessageDigest::sha1().get(),
metadata.data(), metadata.size()); metadata.data(), metadata.size());
{ {
auto attrs = make_unique<TorrentAttribute>(); auto attrs = make_unique<TorrentAttribute>();

View File

@ -74,7 +74,7 @@ void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups()
"6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCe"; "6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCe";
unsigned char infoHash[20]; unsigned char infoHash[20];
message_digest::digest message_digest::digest
(infoHash, sizeof(infoHash), MessageDigest::sha1(), (infoHash, sizeof(infoHash), MessageDigest::sha1().get(),
reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size()); reinterpret_cast<const unsigned char*>(metadata.data()), metadata.size());
dctx_->getFirstFileEntry()->setLength(metadata.size()); dctx_->getFirstFileEntry()->setLength(metadata.size());
std::vector<std::vector<std::string> > announceList; std::vector<std::vector<std::string> > announceList;