2008-05-14 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

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
pull/1/head
Tatsuhiro Tsujikawa 2008-05-14 13:02:43 +00:00
parent 48c96ac441
commit 9bd1fc10af
9 changed files with 43 additions and 13 deletions

View File

@ -1,3 +1,15 @@
2008-05-14 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
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 <tujikawa at rednoah dot com> 2008-05-14 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Made string literals used in SAX parser static const std::string. Made string literals used in SAX parser static const std::string.

View File

@ -70,7 +70,7 @@ std::string DHTTokenTracker::generateToken(const unsigned char* infoHash,
memcpy(src, infoHash, DHT_ID_LENGTH); memcpy(src, infoHash, DHT_ID_LENGTH);
memcpy(src+DHT_ID_LENGTH+6, secret, SECRET_SIZE); memcpy(src+DHT_ID_LENGTH+6, secret, SECRET_SIZE);
unsigned char md[20]; 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)]); return std::string(&md[0], &md[sizeof(md)]);
} }

View File

@ -50,7 +50,7 @@ void DHTUtil::generateRandomKey(unsigned char* key)
{ {
unsigned char bytes[40]; unsigned char bytes[40];
generateRandomData(bytes, sizeof(bytes)); 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) void DHTUtil::generateRandomData(unsigned char* data, size_t length)

View File

@ -304,7 +304,8 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
// retrieve infoHash // retrieve infoHash
BencodeVisitor v; BencodeVisitor v;
infoDic->accept(&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().c_str(),
v.getBencodedData().size()); v.getBencodedData().size());
infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH); infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH);
@ -426,7 +427,7 @@ void DefaultBtContext::computeFastSet
} }
memcpy(tx+4, infoHash, 20); memcpy(tx+4, infoHash, 20);
unsigned char x[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) { 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;
@ -439,7 +440,7 @@ void DefaultBtContext::computeFastSet
} }
} }
unsigned char temp[20]; 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)); memcpy(x, temp, sizeof(x));
} }
} }

View File

@ -155,14 +155,16 @@ void MSEHandshake::initCipher(const unsigned char* infoHash)
memcpy(s+4+KEY_LENGTH, infoHash, INFO_HASH_LENGTH); memcpy(s+4+KEY_LENGTH, infoHash, INFO_HASH_LENGTH);
unsigned char localCipherKey[20]; unsigned char localCipherKey[20];
MessageDigestHelper::digest(localCipherKey, sizeof(localCipherKey), "sha1", MessageDigestHelper::digest(localCipherKey, sizeof(localCipherKey),
MessageDigestContext::SHA1,
s, sizeof(s)); s, sizeof(s));
_encryptor.reset(new ARC4Encryptor()); _encryptor.reset(new ARC4Encryptor());
_encryptor->init(localCipherKey, sizeof(localCipherKey)); _encryptor->init(localCipherKey, sizeof(localCipherKey));
unsigned char peerCipherKey[20]; unsigned char peerCipherKey[20];
memcpy(s, _initiator?"keyB":"keyA", 4); memcpy(s, _initiator?"keyB":"keyA", 4);
MessageDigestHelper::digest(peerCipherKey, sizeof(peerCipherKey), "sha1", MessageDigestHelper::digest(peerCipherKey, sizeof(peerCipherKey),
MessageDigestContext::SHA1,
s, sizeof(s)); s, sizeof(s));
_decryptor.reset(new ARC4Decryptor()); _decryptor.reset(new ARC4Decryptor());
_decryptor->init(peerCipherKey, sizeof(peerCipherKey)); _decryptor->init(peerCipherKey, sizeof(peerCipherKey));
@ -202,7 +204,8 @@ void MSEHandshake::createReq1Hash(unsigned char* md) const
unsigned char buffer[100]; unsigned char buffer[100];
memcpy(buffer, "req1", 4); memcpy(buffer, "req1", 4);
memcpy(buffer+4, _secret, KEY_LENGTH); 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 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, "req2", 4);
memcpy(x+4, infoHash, INFO_HASH_LENGTH); memcpy(x+4, infoHash, INFO_HASH_LENGTH);
unsigned char xh[20]; 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]; 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];
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) { for(size_t i = 0; i < 20; ++i) {
md[i] = xh[i]^yh[i]; md[i] = xh[i]^yh[i];

View File

@ -49,7 +49,7 @@ void MessageDigestHelper::staticSHA1DigestInit()
{ {
staticSHA1DigestFree(); staticSHA1DigestFree();
_sha1Ctx = new MessageDigestContext(); _sha1Ctx = new MessageDigestContext();
_sha1Ctx->trySetAlgo("sha1"); _sha1Ctx->trySetAlgo(MessageDigestContext::SHA1);
_sha1Ctx->digestInit(); _sha1Ctx->digestInit();
} }

View File

@ -55,7 +55,7 @@ Peer::Peer(std::string ipaddr, uint16_t port):
resetStatus(); resetStatus();
std::string idSeed = ipaddr+":"+Util::uitos(port); std::string idSeed = ipaddr+":"+Util::uitos(port);
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
id = MessageDigestHelper::digestString("sha1", idSeed); id = MessageDigestHelper::digestString(MessageDigestContext::SHA1, idSeed);
#else #else
id = idSeed; id = idSeed;
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST

View File

@ -37,6 +37,12 @@
namespace aria2 { 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[] = { static MessageDigestContext::DigestAlgoMap::value_type digests[] = {
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
MessageDigestContext::DigestAlgoMap::value_type("md5", EVP_md5()), MessageDigestContext::DigestAlgoMap::value_type("md5", EVP_md5()),

View File

@ -60,6 +60,12 @@ public:
typedef int DigestAlgo; typedef int DigestAlgo;
#endif // HAVE_LIBGCRYPT #endif // HAVE_LIBGCRYPT
typedef std::map<std::string, MessageDigestContext::DigestAlgo> DigestAlgoMap; typedef std::map<std::string, MessageDigestContext::DigestAlgo> DigestAlgoMap;
static const std::string SHA1;
static const std::string SHA256;
static const std::string MD5;
private: private:
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
EVP_MD_CTX ctx; EVP_MD_CTX ctx;
@ -71,7 +77,7 @@ private:
static DigestAlgoMap digestAlgos; static DigestAlgoMap digestAlgos;
public: public:
MessageDigestContext():algo(getDigestAlgo("sha1")) MessageDigestContext():algo(getDigestAlgo(MessageDigestContext::SHA1))
{} {}
~MessageDigestContext() ~MessageDigestContext()