浏览代码

2010-01-17 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Moved generateRandomKey() from bittorrent_helper to util.
	* src/DHTBucket.cc
	* src/DHTNode.cc
	* src/bittorrent_helper.cc
	* src/bittorrent_helper.h
	* src/util.cc
	* src/util.h
Tatsuhiro Tsujikawa 15 年之前
父节点
当前提交
87b18019b4
共有 7 个文件被更改,包括 26 次插入12 次删除
  1. 10 0
      ChangeLog
  2. 2 2
      src/DHTBucket.cc
  3. 1 1
      src/DHTNode.cc
  4. 0 7
      src/bittorrent_helper.cc
  5. 0 2
      src/bittorrent_helper.h
  6. 9 0
      src/util.cc
  7. 4 0
      src/util.h

+ 10 - 0
ChangeLog

@@ -1,3 +1,13 @@
+2010-01-17  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Moved generateRandomKey() from bittorrent_helper to util.
+	* src/DHTBucket.cc
+	* src/DHTNode.cc
+	* src/bittorrent_helper.cc
+	* src/bittorrent_helper.h
+	* src/util.cc
+	* src/util.h
+
 2010-01-17  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Updated doc about GID.

+ 2 - 2
src/DHTBucket.cc

@@ -74,10 +74,10 @@ DHTBucket::~DHTBucket() {}
 void DHTBucket::getRandomNodeID(unsigned char* nodeID) const
 {
   if(_prefixLength == 0) {
-    bittorrent::generateRandomKey(nodeID);
+    util::generateRandomKey(nodeID);
   } else {
     size_t lastByteIndex = (_prefixLength-1)/8;
-    bittorrent::generateRandomKey(nodeID);
+    util::generateRandomKey(nodeID);
     memcpy(nodeID, _min, lastByteIndex+1);
   }
 }

+ 1 - 1
src/DHTNode.cc

@@ -54,7 +54,7 @@ DHTNode::DHTNode(const unsigned char* id):_port(0), _rtt(0), _condition(1), _las
 
 void DHTNode::generateID()
 {
-  bittorrent::generateRandomKey(_id);
+  util::generateRandomKey(_id);
 }
 
 bool DHTNode::operator==(const DHTNode& node) const

+ 0 - 7
src/bittorrent_helper.cc

@@ -853,13 +853,6 @@ void assertID
   }
 }
 
-void generateRandomKey(unsigned char* key)
-{
-  unsigned char bytes[40];
-  util::generateRandomData(bytes, sizeof(bytes));
-  MessageDigestHelper::digest(key, 20, MessageDigestContext::SHA1, bytes, sizeof(bytes));
-}
-
 BDE parseMagnet(const std::string& magnet)
 {
   BDE result;

+ 0 - 2
src/bittorrent_helper.h

@@ -228,8 +228,6 @@ void assertPayloadLengthEqual
 void assertID
 (uint8_t expected, const unsigned char* data, const std::string& msgName);
 
-void generateRandomKey(unsigned char* key);
-
 // Converts attrs into torrent data. attrs must be a BDE::dict.  This
 // function does not guarantee the returned string is valid torrent
 // data.

+ 9 - 0
src/util.cc

@@ -75,6 +75,7 @@
 #include "A2STR.h"
 #include "array_fun.h"
 #include "a2functional.h"
+#include "MessageDigestHelper.h"
 
 // For libc6 which doesn't define ULLONG_MAX properly because of broken limits.h
 #ifndef ULLONG_MAX
@@ -1017,6 +1018,14 @@ std::string fixTaintedBasename(const std::string& src)
                  A2STR::BACK_SLASH_C, A2STR::UNDERSCORE_C);
 }
 
+void generateRandomKey(unsigned char* key)
+{
+  unsigned char bytes[40];
+  generateRandomData(bytes, sizeof(bytes));
+  MessageDigestHelper::digest
+    (key, 20, MessageDigestContext::SHA1, bytes, sizeof(bytes));
+}
+
 } // namespace util
 
 } // namespace aria2

+ 4 - 0
src/util.h

@@ -371,6 +371,10 @@ std::string applyDir(const std::string& dir, const std::string& relPath);
 // replaces '/' and '\' with '_'.
 std::string fixTaintedBasename(const std::string& src);
 
+// Generates 20 bytes random key and store it to the address pointed
+// by key.  Caller must allocate at least 20 bytes for generated key.
+void generateRandomKey(unsigned char* key);
+
 } // namespace util
 
 } // namespace aria2