25 #include "ns3/core-module.h"
151 bool Add (
const std::string phrase)
169 m_coll.push_back (std::make_pair (h, phrase));
180 m_dict.insert (std::make_pair (h, phrase));
190 std::string name =
m_name;
195 case Bits32: name +=
" (32-bit version)";
break;
196 case Bits64: name +=
" (64-bit version)";
break;
197 default: name +=
" (unknown!?!)";
206 std::cout << std::endl;
208 std::cout <<
GetName () <<
": " <<
m_coll.size () <<
" collisions:"
210 for (
auto collision :
m_coll)
212 uint64_t h = collision.first;
214 std::cout << std::setfill (
'0') << std::hex << std::setw (8) << h
215 << std::dec << std::setfill (
' ') <<
" "
216 << std::setw (20) << std::left
217 <<
m_dict.find (h)->second
258 typedef std::vector < std::pair<uint64_t, std::string> >
collision_t;
294 void Add (
const std::string phrase)
296 if (phrase.size () == 0)
304 newPhrases += collider.Add (phrase);
356 long double k32 = 0xFFFFFFFF;
357 long double k64 =
static_cast<long double> (0xFFFFFFFFFFFFFFFFULL);
360 long double Ec32 = n * (n - 1) / ( 2 * k32) * (1 - (n - 2) / (3 * k32));
361 long double Ec64 = n * (n - 1) / ( 2 * k64) * (1 - (n - 2) / (3 * k64));
364 std::cout <<
"" << std::endl;
365 std::cout <<
"Number of words or phrases: " << n << std::endl;
366 std::cout <<
"Expected number of collisions: (32-bit table) " << Ec32
368 std::cout <<
"Expected number of collisions: (64-bit table) " << Ec64
394 int start = clock ();
395 for (
auto const & word :
m_words)
397 for (uint32_t i = 0; i < reps; ++i)
403 double delta = stop -
start;
404 double per = 1e9 * delta / (
m_nphrases * reps * CLOCKS_PER_SEC);
406 std::cout << std::left
407 << std::setw (32) << collider.
GetName ()
410 << std::setw (10) << reps
411 << std::setw (10) << stop -
start
412 << std::setw (12) << per
420 std::cout <<
"" << std::endl;
421 std::cout << std::left
422 << std::setw (32) <<
"Hash timing"
424 << std::setw (10) <<
"Phrases"
425 << std::setw (10) <<
"Reps"
426 << std::setw (10) <<
"Ticks"
427 << std::setw (12) <<
"ns/hash"
430 for (
auto const & collider :
m_hashes)
471 return "/usr/share/dict/words";
486 std::cout <<
"Hashing the dictionar"
487 << (
m_files.size () == 1 ?
"y" :
"ies")
492 std::cout <<
"Dictionary file: " << dictFile << std::endl;
497 std::ifstream dictStream;
498 dictStream.open (dictFile.c_str () );
499 if (!dictStream.is_open () )
501 std::cerr <<
"Failed to open dictionary file."
502 <<
"'" << dictFile <<
"'"
507 while (dictStream.good () )
510 getline (dictStream, phrase);
536 main (
int argc,
char *argv[])
538 std::cout << std::endl;
539 std::cout <<
"Hasher" << std::endl;
545 cmd.Usage (
"Find hash collisions in the dictionary.");
546 cmd.AddValue (
"dict",
"Dictionary file to hash",
551 cmd.AddValue (
"time",
"Run timing test", timing);
552 cmd.Parse (argc, argv);
556 Hasher ( Create<Hash::Function::Fnv1a> () ),
559 Hasher ( Create<Hash::Function::Fnv1a> () ),
563 Hasher ( Create<Hash::Function::Murmur3> () ),
566 Hasher ( Create<Hash::Function::Murmur3> () ),
Parse command-line arguments.
Keep track of collisions.
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.
void Report() const
Print the collisions found.
Collider(const std::string name, Hasher hash, const enum Bits bits)
Constructor.
Bits
The size of hash function being tested.
@ Bits64
Use 64-bit hash function.
@ Bits32
Use 32-bit hash function.
std::vector< std::pair< uint64_t, std::string > > collision_t
Collision map of subsequent instances.
bool Add(const std::string phrase)
Add a string to the Collider.
enum Bits m_bits
Hash function.
std::string GetName() const
hashdict_t m_dict
The dictionary map, indexed by hash.
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.
void ReadInto(Dictionary &dict)
Add phrases from the files into the dict.
static std::string GetDefault(void)
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(void)
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, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...