From 9bd1fc10af54f20f30345cbe90456aef3f3ee81b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 14 May 2008 13:02:43 +0000 Subject: [PATCH] 2008-05-14 Tatsuhiro Tsujikawa Defined message digest algorithm name as static const std::string * src/DHTTokenTracker.cc * src/DHTUtil.cc * src/DefaultBtContext.cc * src/MSEHandshake.cc * src/MessageDigestHelper.cc * src/Peer.cc * src/messageDigest.cc * src/messageDigest.h --- ChangeLog | 12 ++++++++++++ src/DHTTokenTracker.cc | 2 +- src/DHTUtil.cc | 2 +- src/DefaultBtContext.cc | 7 ++++--- src/MSEHandshake.cc | 15 ++++++++++----- src/MessageDigestHelper.cc | 2 +- src/Peer.cc | 2 +- src/messageDigest.cc | 6 ++++++ src/messageDigest.h | 8 +++++++- 9 files changed, 43 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6d1b2479..1985aad5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-05-14 Tatsuhiro Tsujikawa + + Defined message digest algorithm name as static const std::string + * src/DHTTokenTracker.cc + * src/DHTUtil.cc + * src/DefaultBtContext.cc + * src/MSEHandshake.cc + * src/MessageDigestHelper.cc + * src/Peer.cc + * src/messageDigest.cc + * src/messageDigest.h + 2008-05-14 Tatsuhiro Tsujikawa Made string literals used in SAX parser static const std::string. diff --git a/src/DHTTokenTracker.cc b/src/DHTTokenTracker.cc index d92a8af0..b900f53b 100644 --- a/src/DHTTokenTracker.cc +++ b/src/DHTTokenTracker.cc @@ -70,7 +70,7 @@ std::string DHTTokenTracker::generateToken(const unsigned char* infoHash, memcpy(src, infoHash, DHT_ID_LENGTH); memcpy(src+DHT_ID_LENGTH+6, secret, SECRET_SIZE); unsigned char md[20]; - MessageDigestHelper::digest(md, sizeof(md), "sha1", src, sizeof(src)); + MessageDigestHelper::digest(md, sizeof(md), MessageDigestContext::SHA1, src, sizeof(src)); return std::string(&md[0], &md[sizeof(md)]); } diff --git a/src/DHTUtil.cc b/src/DHTUtil.cc index ee82429e..99cd7a7a 100644 --- a/src/DHTUtil.cc +++ b/src/DHTUtil.cc @@ -50,7 +50,7 @@ void DHTUtil::generateRandomKey(unsigned char* key) { unsigned char bytes[40]; generateRandomData(bytes, sizeof(bytes)); - MessageDigestHelper::digest(key, 20, "sha1", bytes, sizeof(bytes)); + MessageDigestHelper::digest(key, 20, MessageDigestContext::SHA1, bytes, sizeof(bytes)); } void DHTUtil::generateRandomData(unsigned char* data, size_t length) diff --git a/src/DefaultBtContext.cc b/src/DefaultBtContext.cc index a33a5ab5..e22e6872 100644 --- a/src/DefaultBtContext.cc +++ b/src/DefaultBtContext.cc @@ -304,7 +304,8 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st // retrieve infoHash BencodeVisitor v; infoDic->accept(&v); - MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH, "sha1", + MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH, + MessageDigestContext::SHA1, v.getBencodedData().c_str(), v.getBencodedData().size()); infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH); @@ -426,7 +427,7 @@ void DefaultBtContext::computeFastSet } memcpy(tx+4, infoHash, 20); unsigned char x[20]; - MessageDigestHelper::digest(x, sizeof(x), "sha1", tx, 24); + MessageDigestHelper::digest(x, sizeof(x), MessageDigestContext::SHA1, tx, 24); while(fastSet.size() < fastSetSize) { for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; i++) { size_t j = i*4; @@ -439,7 +440,7 @@ void DefaultBtContext::computeFastSet } } unsigned char temp[20]; - MessageDigestHelper::digest(temp, sizeof(temp), "sha1", x, sizeof(x)); + MessageDigestHelper::digest(temp, sizeof(temp), MessageDigestContext::SHA1, x, sizeof(x)); memcpy(x, temp, sizeof(x)); } } diff --git a/src/MSEHandshake.cc b/src/MSEHandshake.cc index 2d142550..918302ae 100644 --- a/src/MSEHandshake.cc +++ b/src/MSEHandshake.cc @@ -155,14 +155,16 @@ void MSEHandshake::initCipher(const unsigned char* infoHash) memcpy(s+4+KEY_LENGTH, infoHash, INFO_HASH_LENGTH); unsigned char localCipherKey[20]; - MessageDigestHelper::digest(localCipherKey, sizeof(localCipherKey), "sha1", + MessageDigestHelper::digest(localCipherKey, sizeof(localCipherKey), + MessageDigestContext::SHA1, s, sizeof(s)); _encryptor.reset(new ARC4Encryptor()); _encryptor->init(localCipherKey, sizeof(localCipherKey)); unsigned char peerCipherKey[20]; memcpy(s, _initiator?"keyB":"keyA", 4); - MessageDigestHelper::digest(peerCipherKey, sizeof(peerCipherKey), "sha1", + MessageDigestHelper::digest(peerCipherKey, sizeof(peerCipherKey), + MessageDigestContext::SHA1, s, sizeof(s)); _decryptor.reset(new ARC4Decryptor()); _decryptor->init(peerCipherKey, sizeof(peerCipherKey)); @@ -202,7 +204,8 @@ void MSEHandshake::createReq1Hash(unsigned char* md) const unsigned char buffer[100]; memcpy(buffer, "req1", 4); memcpy(buffer+4, _secret, KEY_LENGTH); - MessageDigestHelper::digest(md, 20, "sha1", buffer, 4+KEY_LENGTH); + MessageDigestHelper::digest(md, 20, MessageDigestContext::SHA1, + buffer, 4+KEY_LENGTH); } void MSEHandshake::createReq23Hash(unsigned char* md, const unsigned char* infoHash) const @@ -211,13 +214,15 @@ void MSEHandshake::createReq23Hash(unsigned char* md, const unsigned char* infoH memcpy(x, "req2", 4); memcpy(x+4, infoHash, INFO_HASH_LENGTH); unsigned char xh[20]; - MessageDigestHelper::digest(xh, sizeof(xh), "sha1", x, sizeof(x)); + MessageDigestHelper::digest(xh, sizeof(xh), MessageDigestContext::SHA1, + x, sizeof(x)); unsigned char y[4+96]; memcpy(y, "req3", 4); memcpy(y+4, _secret, KEY_LENGTH); unsigned char yh[20]; - MessageDigestHelper::digest(yh, sizeof(yh), "sha1", y, sizeof(y)); + MessageDigestHelper::digest(yh, sizeof(yh), MessageDigestContext::SHA1, + y, sizeof(y)); for(size_t i = 0; i < 20; ++i) { md[i] = xh[i]^yh[i]; diff --git a/src/MessageDigestHelper.cc b/src/MessageDigestHelper.cc index 5d23d3d1..01726248 100644 --- a/src/MessageDigestHelper.cc +++ b/src/MessageDigestHelper.cc @@ -49,7 +49,7 @@ void MessageDigestHelper::staticSHA1DigestInit() { staticSHA1DigestFree(); _sha1Ctx = new MessageDigestContext(); - _sha1Ctx->trySetAlgo("sha1"); + _sha1Ctx->trySetAlgo(MessageDigestContext::SHA1); _sha1Ctx->digestInit(); } diff --git a/src/Peer.cc b/src/Peer.cc index e702c67c..6449c4bd 100644 --- a/src/Peer.cc +++ b/src/Peer.cc @@ -55,7 +55,7 @@ Peer::Peer(std::string ipaddr, uint16_t port): resetStatus(); std::string idSeed = ipaddr+":"+Util::uitos(port); #ifdef ENABLE_MESSAGE_DIGEST - id = MessageDigestHelper::digestString("sha1", idSeed); + id = MessageDigestHelper::digestString(MessageDigestContext::SHA1, idSeed); #else id = idSeed; #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/messageDigest.cc b/src/messageDigest.cc index 251c690d..18fbd8d8 100644 --- a/src/messageDigest.cc +++ b/src/messageDigest.cc @@ -37,6 +37,12 @@ namespace aria2 { +const std::string MessageDigestContext::SHA1("sha1"); + +const std::string MessageDigestContext::SHA256("sha256"); + +const std::string MessageDigestContext::MD5("md5"); + static MessageDigestContext::DigestAlgoMap::value_type digests[] = { #ifdef HAVE_LIBSSL MessageDigestContext::DigestAlgoMap::value_type("md5", EVP_md5()), diff --git a/src/messageDigest.h b/src/messageDigest.h index 48177253..07ab456c 100644 --- a/src/messageDigest.h +++ b/src/messageDigest.h @@ -60,6 +60,12 @@ public: typedef int DigestAlgo; #endif // HAVE_LIBGCRYPT typedef std::map DigestAlgoMap; + + static const std::string SHA1; + + static const std::string SHA256; + + static const std::string MD5; private: #ifdef HAVE_LIBSSL EVP_MD_CTX ctx; @@ -71,7 +77,7 @@ private: static DigestAlgoMap digestAlgos; public: - MessageDigestContext():algo(getDigestAlgo("sha1")) + MessageDigestContext():algo(getDigestAlgo(MessageDigestContext::SHA1)) {} ~MessageDigestContext()