Created
February 3, 2012 10:48
-
-
Save jgxvx/1729633 to your computer and use it in GitHub Desktop.
Hash Codes with Qt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtCore/QCoreApplication> | |
#include <QByteArray> | |
#include <QSet> | |
#include <QDebug> | |
#include <QString> | |
#include <QTime> | |
#include <limits> | |
#include <math.h> | |
#include <iostream> | |
using namespace std; | |
typedef struct { | |
unsigned int length; | |
unsigned long long count; | |
bool uppercase; | |
bool lowercase; | |
bool numbers; | |
bool symbols; | |
} Configuration; | |
/* | |
* Forward declarations | |
*/ | |
QByteArray create_characters(const Configuration &config); | |
QString create_hash(const QByteArray &characters, const unsigned short &num_characters, const unsigned int &hash_length); | |
unsigned long long calculate_max_count(const unsigned short &num_characters,const unsigned int &has_length); | |
/** | |
* Program entry point | |
* | |
* @param argc The command line arguments count | |
* @param argv The command line arguments | |
* @return 0 on success, !0 on failure | |
*/ | |
int main(int argc, char *argv[]) { | |
QCoreApplication a(argc, argv); | |
QTime start = QTime::currentTime(); | |
Configuration config; | |
config.length = 128; | |
config.count = 100000; | |
config.uppercase = true; | |
config.lowercase = true; | |
config.numbers = true; | |
config.symbols = true; | |
unsigned long long iterations = 0; | |
QByteArray characters = create_characters(config); | |
unsigned short num_characters = characters.size(); | |
cout << "Number of characters: " << num_characters << endl; | |
unsigned long long max_count = calculate_max_count(num_characters,config.length); | |
cout << "Max count: " << max_count << endl; | |
if(config.count > max_count) { | |
cout << "Tried to create " | |
<< config.count | |
<< " hashes, but can only create " | |
<< max_count << '.' | |
<< endl; | |
return -1; | |
} | |
QSet<QString> hashes; | |
QTime create = QTime::currentTime(); | |
do { | |
++iterations; | |
hashes << create_hash(characters, num_characters, config.length); | |
} while(hashes.size() < (int)config.count); | |
int time_to_create = create.elapsed(); | |
QSetIterator<QString> i(hashes); | |
while(i.hasNext()) { | |
cout << i.next().toStdString() << endl; | |
} | |
cout << endl << "Created " << hashes.size() | |
<< " hashes, taking " << iterations | |
<< " iterations in " << time_to_create | |
<< "ms (" << start.elapsed() << "ms total)" << endl << endl; | |
return 0; | |
return a.exec(); | |
} | |
/** | |
* Creates a collection of characters valid based on the hash configuration | |
* | |
* @param config The hash configuration | |
* @return The collection of valid characters | |
*/ | |
QByteArray create_characters(const Configuration &config) { | |
QByteArray characters; | |
if(true == config.uppercase) { | |
characters.reserve(26); | |
for(char c=0x41;c<=0x5A;++c) { | |
characters.append(c); | |
} | |
} | |
if(true == config.lowercase) { | |
characters.reserve(26); | |
for(char c=0x61;c<=0x7A;++c) { | |
characters.append(c); | |
} | |
} | |
if(true == config.numbers) { | |
characters.reserve(10); | |
for(char c=0x30;c<=0x39;++c) { | |
characters.append(c); | |
} | |
} | |
if(true == config.symbols) { | |
characters.reserve(0x2F-0x21); | |
for(char c=0x21;c<=0x2F;++c) { | |
characters.append(c); | |
} | |
} | |
return characters; | |
} | |
/** | |
* Creates a hash | |
* | |
* @param characters The characters available for the hash to be created | |
* @param hash_length The length of the hash to be created | |
*/ | |
QString create_hash(const QByteArray &characters, | |
const unsigned short &num_characters, | |
const unsigned int &hash_length) { | |
QString hash; | |
for(unsigned int i=0;i<hash_length;++i) { | |
unsigned int pos = arc4random()%num_characters; | |
hash += characters.at(pos); | |
} | |
return hash; | |
} | |
/** | |
* Calculates the maximum possible number of hashes that can be generated | |
* with a given combination of number of characters and hash length. | |
* | |
* @param num_characters The number of characters | |
* @param hash_length The hash length | |
* @return The maximum possible number of hashes | |
*/ | |
unsigned long long calculate_max_count(const unsigned short &num_characters, | |
const unsigned int &hash_length) { | |
double max_count = pow(num_characters,hash_length); | |
if(max_count > ULLONG_MAX) { | |
return ULLONG_MAX; | |
} | |
return (unsigned long long)max_count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment