25 #include "ns3/core-module.h"
153 bool Add (
const std::string phrase)
171 m_coll.push_back (std::make_pair (h, phrase));
182 m_dict.insert (std::make_pair (h, phrase));
192 std::string name =
m_name;
197 case Bits32: name +=
" (32-bit version)";
break;
198 case Bits64: name +=
" (64-bit version)";
break;
199 default: name +=
" (unknown!?!)";
208 std::cout << std::endl;
210 std::cout <<
GetName () <<
": " <<
m_coll.size () <<
" collisions:"
212 for (
auto collision :
m_coll)
214 uint64_t h = collision.first;
216 std::cout << std::setfill (
'0') << std::hex << std::setw (8) << h
217 << std::dec << std::setfill (
' ') <<
" "
218 << std::setw (20) << std::left
219 <<
m_dict.find (h)->second
260 typedef std::vector < std::pair<uint64_t, std::string> >
collision_t;
296 void Add (
const std::string phrase)
298 if (phrase.size () == 0)
306 newPhrases += collider.Add (phrase);
358 long double k32 = 0xFFFFFFFF;
359 long double k64 =
static_cast<long double> (0xFFFFFFFFFFFFFFFFULL);
362 long double Ec32 =
n * (
n - 1) / ( 2 * k32) * (1 - (
n - 2) / (3 * k32));
363 long double Ec64 =
n * (
n - 1) / ( 2 * k64) * (1 - (
n - 2) / (3 * k64));
366 std::cout <<
"" << std::endl;
367 std::cout <<
"Number of words or phrases: " <<
n << std::endl;
368 std::cout <<
"Expected number of collisions: (32-bit table) " << Ec32
370 std::cout <<
"Expected number of collisions: (64-bit table) " << Ec64
396 int start = clock ();
397 for (
auto const & word :
m_words)
399 for (uint32_t i = 0; i < reps; ++i)
405 double delta = stop -
start;
406 double per = 1e9 * delta / (
m_nphrases * reps * CLOCKS_PER_SEC);
408 std::cout << std::left
409 << std::setw (32) << collider.
GetName ()
412 << std::setw (10) << reps
413 << std::setw (10) << stop -
start
414 << std::setw (12) << per
422 std::cout <<
"" << std::endl;
423 std::cout << std::left
424 << std::setw (32) <<
"Hash timing"
426 << std::setw (10) <<
"Phrases"
427 << std::setw (10) <<
"Reps"
428 << std::setw (10) <<
"Ticks"
429 << std::setw (12) <<
"ns/hash"
432 for (
auto const & collider :
m_hashes)
473 return "/usr/share/dict/words";
488 std::cout <<
"Hashing the dictionar"
489 << (
m_files.size () == 1 ?
"y" :
"ies")
494 std::cout <<
"Dictionary file: " << dictFile << std::endl;
499 std::ifstream dictStream;
500 dictStream.open (dictFile.c_str () );
501 if (!dictStream.is_open () )
503 std::cerr <<
"Failed to open dictionary file."
504 <<
"'" << dictFile <<
"'"
509 while (dictStream.good () )
512 getline (dictStream, phrase);
538 main (
int argc,
char *argv[])
540 std::cout << std::endl;
541 std::cout <<
"Hasher" << std::endl;
547 cmd.Usage (
"Find hash collisions in the dictionary.");
548 cmd.AddValue (
"dict",
"Dictionary file to hash",
553 cmd.AddValue (
"time",
"Run timing test", timing);
554 cmd.Parse (argc, argv);
558 Hasher ( Create<Hash::Function::Fnv1a> () ),
561 Hasher ( Create<Hash::Function::Fnv1a> () ),
565 Hasher ( Create<Hash::Function::Murmur3> () ),
568 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.
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...