16 #include "ns3/core-module.h"
153 bool Add(
const std::string phrase)
171 m_coll.emplace_back(h, phrase);
182 m_dict.insert(std::make_pair(h, phrase));
192 std::string name =
m_name;
197 name +=
" (32-bit version)";
200 name +=
" (64-bit version)";
203 name +=
" (unknown!?!)";
211 std::cout << std::endl;
213 std::cout <<
GetName() <<
": " <<
m_coll.size() <<
" collisions:" << std::endl;
214 for (
const auto& collision :
m_coll)
216 uint64_t h = collision.first;
218 std::cout << std::setfill(
'0') << std::hex << std::setw(8) << h << std::dec
219 << std::setfill(
' ') <<
" " << std::setw(20) << std::left
220 <<
m_dict.find(h)->second << collision.second << std::right << std::endl;
257 typedef std::vector<std::pair<uint64_t, std::string>>
collision_t;
292 void Add(
const std::string phrase)
299 bool newPhrases =
false;
302 newPhrases |= collider.Add(phrase);
354 long double k32 = 0xFFFFFFFF;
355 auto k64 =
static_cast<long double>(0xFFFFFFFFFFFFFFFFULL);
358 long double Ec32 = n * (n - 1) / (2 * k32) * (1 - (n - 2) / (3 * k32));
359 long double Ec64 = n * (n - 1) / (2 * k64) * (1 - (n - 2) / (3 * k64));
362 std::cout <<
"" << std::endl;
363 std::cout <<
"Number of words or phrases: " << n << std::endl;
364 std::cout <<
"Expected number of collisions: (32-bit table) " << Ec32 << std::endl;
365 std::cout <<
"Expected number of collisions: (64-bit table) " << Ec64 << std::endl;
373 for (
const auto& collider :
m_hashes)
390 for (
const auto& word :
m_words)
392 for (uint32_t i = 0; i < reps; ++i)
401 std::cout << std::left << std::setw(32) << collider.
GetName() << std::right << std::setw(10)
403 << std::setw(12) << per << std::endl;
410 std::cout <<
"" << std::endl;
411 std::cout << std::left << std::setw(32) <<
"Hash timing" << std::right << std::setw(10)
412 <<
"Phrases" << std::setw(10) <<
"Reps" << std::setw(10) <<
"Ticks"
413 << std::setw(12) <<
"ns/hash" << std::endl;
415 for (
const auto& collider :
m_hashes)
453 return "/usr/share/dict/words";
468 std::cout <<
"Hashing the dictionar" << (
m_files.size() == 1 ?
"y" :
"ies") << std::endl;
470 for (
const auto& dictFile :
m_files)
472 std::cout <<
"Dictionary file: " << dictFile << std::endl;
477 std::ifstream dictStream;
478 dictStream.open(dictFile);
479 if (!dictStream.is_open())
481 std::cerr <<
"Failed to open dictionary file."
482 <<
"'" << dictFile <<
"'" << std::endl;
486 while (dictStream.good())
489 getline(dictStream, phrase);
514 main(
int argc,
char* argv[])
516 std::cout << std::endl;
517 std::cout <<
"Hasher" << std::endl;
523 cmd.Usage(
"Find hash collisions in the dictionary.");
525 "Dictionary file to hash",
529 cmd.AddValue(
"time",
"Run timing test", timing);
530 cmd.Parse(argc, argv);
Parse command-line arguments.
Keep track of collisions.
Collider(const std::string name, Hasher hash, const Bits bits)
Constructor.
collision_t m_coll
The list of collisions.
std::map< uint64_t, std::string > hashdict_t
Hashed dictionary of first instance of each hash.
uint64_t GetHash(const std::string phrase)
Get the appropriate hash value.
std::string m_name
Name of this hash.
std::vector< std::pair< uint64_t, std::string > > collision_t
Collision map of subsequent instances.
void Report() const
Print the collisions found.
Bits
The size of hash function being tested.
@ Bits64
Use 64-bit hash function.
@ Bits32
Use 32-bit hash function.
bool Add(const std::string phrase)
Add a string to the Collider.
std::string GetName() const
hashdict_t m_dict
The dictionary map, indexed by hash.
Bits m_bits
Hash function.
std::vector< std::string > m_files
List of word files to use.
bool Add(const std::string &file)
CommandLine callback function to add a file argument to the list.
static std::string GetDefault()
void ReadInto(Dictionary &dict)
Add phrases from the files into the dict.
Word list and hashers to test.
void ReportExpectedCollisions() const
Report the expected number of collisions.
void Add(Collider c)
Add a Collider containing a hash function.
std::vector< std::string > m_words
List of unique words.
std::vector< Collider > m_hashes
List of hash Colliders.
void Report() const
Print the collisions for each Collider.
void TimeOne(const Collider &collider)
Time and report the execution of one hash across the entire Dictionary.
unsigned long m_nphrases
Number of strings hashed.
void Add(const std::string phrase)
Add a string to the dictionary.
void Time()
Report the execution time of each hash across the entire Dictionary.
Generic Hash function interface.
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Hasher & clear()
Restore initial state.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Namespace for hasher-example.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...