Commit 8847632f authored by Clément Février's avatar Clément Février

Makefile -std=gnu++14 (auto in headers), templates

parent 57bad1ec
......@@ -30,22 +30,22 @@ all:ex
# Other flags are added.
# -pipe decreases compilation time.
#
CC= g++ -std=gnu++11 -Ofast -Wall -Wextra -pipe -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -frename-registers -funroll-loops -fopenmp -D_GLIBCXX_PARALLEL
#CC= g++ -std=gnu++14 -Ofast -Wall -Wextra -pipe -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -frename-registers -funroll-loops -fopenmp -D_GLIBCXX_PARALLEL
#
# Same as above with flags specific for AMD Piledriver Family 15h.
#
#CC= g++ -std=gnu++11 -Ofast -Wall -Wextra -pipe -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -frename-registers -funroll-loops -fopenmp -D_GLIBCXX_PARALLEL -march=bdver2 -msse -msse2 -msse3 -mmmx -m3dnow
CC= g++ -std=gnu++14 -Ofast -Wall -Wextra -pipe -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -frename-registers -funroll-loops -fopenmp -D_GLIBCXX_PARALLEL -march=bdver2 -msse -msse2 -msse3 -mmmx -m3dnow
#
# Use -fprofile-generate to generate a profile.
# Here for AMD Piledriver Family 15h.
#
#CC= g++ -std=gnu++11 -Ofast -Wall -Wextra -pipe -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -frename-registers -funroll-loops -fopenmp -D_GLIBCXX_PARALLEL -march=bdver2 -msse -msse2 -msse3 -mmmx -m3dnow -fprofile-generate
#CC= g++ -std=gnu++14 -Ofast -Wall -Wextra -pipe -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -frename-registers -funroll-loops -fopenmp -D_GLIBCXX_PARALLEL -march=bdver2 -msse -msse2 -msse3 -mmmx -m3dnow -fprofile-generate
#
# Use -fprofile-use -fprofile-correction to use the profile.
# The second flag is needed for multithreading.
# Here for AMD Piledriver Family 15h.
#
#CC= g++ -std=gnu++11 -Ofast -Wall -Wextra -pipe -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -frename-registers -funroll-loops -fopenmp -D_GLIBCXX_PARALLEL -march=bdver2 -msse -msse2 -msse3 -mmmx -m3dnow -fprofile-use -fprofile-correction
#CC= g++ -std=gnu++14 -Ofast -Wall -Wextra -pipe -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -frename-registers -funroll-loops -fopenmp -D_GLIBCXX_PARALLEL -march=bdver2 -msse -msse2 -msse3 -mmmx -m3dnow -fprofile-use -fprofile-correction
link= -o
......
......@@ -24,43 +24,14 @@
FRANCE
*/
//#include <crypto++/sha.h>
//#include <crypto++/hex.h>
#include <string> // std::string
#include <iostream> // std::cout, ostream (std::endl),
// io (std::hex)
#include <cstddef> // size_t
#include <cstdlib> // EXIT_SUCCESS, exit
#include <boost/thread.hpp> // boost::thread_group, boost::bind
#include <tomcrypt.h> // sha1_desc, hash_state, sha1_init, sha1_process
#include "functions.h"
/**
* Hashes a given input string using the SHA1 algorithm
* @param input The input sequence pointer
* @param inputSize The size of the input sequence
* @return A new[]-allocated pointer to the resulting data. 20 bytes long.
*/
unsigned char * hashSHA1(const std::string & input)
{
//Initial
unsigned char * hashResult = new unsigned char[sha1_desc.hashsize];
//Initialize a state variable for the hash
hash_state md;
sha1_init(&md);
//Process the text - remember you can call process() multiple times
sha1_process(&md, (const unsigned char*) input.c_str(), input.size());
//Finish the hash calculation
sha1_done(&md, hashResult);
// Return the result
return hashResult;
}
/*std::string generateHash(const std::string & source)
/*std::string generateHash(const auto & source)
{
CryptoPP::SHA1 hash;
byte digest[CryptoPP::SHA1::DIGESTSIZE];
......@@ -74,39 +45,17 @@ unsigned char * hashSHA1(const std::string & input)
return output;
}*/
void findPassword(const unsigned char * hash, const std::string * list, const unsigned short int & COUNT, const unsigned char & L, const unsigned char N_THREAD)
{
// Create a group of threads
boost::thread_group t;
for(unsigned short int i = 0; i < COUNT; ++i)
{
// Launch a thread
// Initiate the recursive function with the first letter
t.create_thread(boost::bind(findPasswordThread, hash, list[i], list, COUNT, L, 1, N_THREAD));
// Wait until N_THREAD finish before launching news ones
if((i % N_THREAD) == (N_THREAD - 1))
{
// Do not continue before all launched threads finish
t.join_all();
// Display a progress to standard output
std::cout<<"progress: "<<100 * i / (double)COUNT <<"%"<<std::endl;
}
}
// Wait for remaining threads to finish
t.join_all();
}
void findPasswordThread(const unsigned char * hash, const std::string & pass, const std::string * list, const unsigned short int & COUNT, const unsigned char & L, const unsigned char & l, const unsigned char N_THREAD)
void findPasswordThread(const unsigned char * hash, const std::string & pass, const std::string * list, const unsigned char & COUNT, const unsigned char & L, const unsigned char & l, const unsigned char & N_THREAD)
{
// If the password to test is build,
// test if it is the correct one
if(l == L)
{
// Hash the pass word to test
unsigned char * sha1 = hashSHA1(pass);
const auto sha1 = hashSHA1(pass);
// Test if hashes match
// Assume that the correct password expected if shown otherwise
bool found = true;
auto found = true;
for(unsigned char i = 0; i < 20; ++i)
{
// This condition is strictly equivalent to
......@@ -152,7 +101,7 @@ void findPasswordThread(const unsigned char * hash, const std::string & pass, co
{
// If the password is not build yet,
// keep appending characters.
for(unsigned short int i = 0; i < COUNT; ++i)
for(unsigned char i = 0; i < COUNT; ++i)
{
// pass + list[i]
// adds a new letter to the password to test.
......
......@@ -27,13 +27,53 @@
#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_
/* Return the hash SHA-1 of a string (input)
using libtomcrypt */
unsigned char * hashSHA1(const std::string & input);
#include <string> // std::string
#include <iostream> // std::cout, ostream (std::endl)
#include <boost/thread.hpp> // boost::thread_group, boost::bind
//#include <crypto++/sha.h>
//#include <crypto++/hex.h>
#include <tomcrypt.h> // sha1_desc, hash_state, sha1_init, sha1_process
/* Hashes a given input string using the SHA-1 algorithm
using libtomcrypt
Parameter:
* The input sequence pointer
Returns:
* A 20 bytes long new[]-allocated pointer
to the resulting data. */
unsigned char * hashSHA1(const auto & input)
{
// Initial
auto * hashResult = new unsigned char [sha1_desc.hashsize];
// Initialize a state variable for the hash
hash_state md;
sha1_init(&md);
// Process the text
sha1_process(&md, (const decltype(hashSHA1(input))) input.c_str(), input.size());
// Finish the hash calculation
sha1_done(&md, hashResult);
// Return the result
return hashResult;
}
/* Return the hash SHA-1 of a string (source)
using Crypto++ */
//std::string generateHash(const std::string & source);
//std::string generateHash(const auto & source);
/* Seek for password, display the latter if found it
and exit the program.
Parameters:
* hash: The hash to crack.
* pass: Current state of the password to build and test.
* list: List of all allowed characters.
* COUNT: Length of list.
* L: Maximum length of the password to test.
* l: Current length of the password to test.
* N_THREAD: Number of threads to launch. */
void findPasswordThread(const unsigned char * hash, const std::string & pass, const std::string * list, const unsigned char & COUNT, const unsigned char & L, const unsigned char & l, const unsigned char & N_THREAD);
/* Seek for password, display the latter if found it
and exit the program.
......@@ -55,18 +95,26 @@ unsigned char * hashSHA1(const std::string & input);
* COUNT: Length of list.
* L: Maximum length of the password to test.
* N_THREAD: Number of threads to launch. */
void findPassword(const unsigned char * hash, const std::string * list, const unsigned short int & COUNT, const unsigned char & L, const unsigned char N_THREAD);
/* Seek for password, display the latter if found it
and exit the program.
Parameters:
* hash: The hash to crack.
* pass: Current state of the password to build and test.
* list: List of all allowed characters.
* COUNT: Length of list.
* L: Maximum length of the password to test.
* l: Current length of the password to test.
* N_THREAD: Number of threads to launch. */
void findPasswordThread(const unsigned char * hash, const std::string & pass, const std::string * list, const unsigned short int & COUNT, const unsigned char & L, const unsigned char & l, const unsigned char N_THREAD);
void findPassword(const auto hash, const auto list, const auto & COUNT, const auto & L, const auto & N_THREAD)
{
// Create a group of threads
boost::thread_group t;
for(unsigned char i = 0; i < COUNT; ++i)
{
// Launch a thread
// Initiate the recursive function with the first letter
t.create_thread(boost::bind(findPasswordThread, hash, list[i], list, COUNT, L, 1, N_THREAD));
// Wait until N_THREAD finish before launching news ones
if((i % N_THREAD) == (N_THREAD - 1))
{
// Do not continue before all launched threads finish
t.join_all();
// Display a progress to standard output
std::cout<<"progress: "<<100 * i / (float)COUNT <<"%"<<std::endl;
}
}
// Wait for remaining threads to finish
t.join_all();
}
#endif
......@@ -141,7 +141,7 @@ int main ()
// count: Increment used to access list element in the loop.
// Also used to pass the length of the list
// to findPassword.
unsigned short int count = 0;
unsigned char count = 0;
// First item is "".
// Used to build password to test smaller than MaxLength.
list[0] = "";
......@@ -151,7 +151,7 @@ int main ()
// In this case, all from 33 to 126 included.
// The reverted order is used to test in priority
// lowercase-based passwords.
for(unsigned short int i = 126; i > 32; --i)
for(unsigned char i = 126; i > 32; --i)
{
// Convert the dec value of i to char.
list[count] = (char)i;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment