Skip to content

Instantly share code, notes, and snippets.

@yamidevs
Last active March 12, 2019 19:04
Show Gist options
  • Save yamidevs/ef588f0fb753e49f4e0aad008033b7c6 to your computer and use it in GitHub Desktop.
Save yamidevs/ef588f0fb753e49f4e0aad008033b7c6 to your computer and use it in GitHub Desktop.
implémentation tth 5 64
#include "tth.h"
#include <iostream>
#include<vector>
#include<string>
using namespace Helper;
using namespace std;
TTH::TTH(vector<int> messages)
{
// initialisation de l'empreinte a 0
for(int i = 0;i<5;i++)
print[i] = 0;
this->messages = messages;
// verfication si c'est un multiple de 25
if((this->messages.size() % 25) != 0)
{
int jam = (((this->messages.size() / 25) + 1) * 25) - this->messages.size();
//clone list
messages_updates = this->messages;
for(int i = 0;i<jam;i++)
{
//le premier bourage a 32
if(i == 0)
{
this->messages_updates.push_back(32);
}else
{
this->messages_updates.push_back(0);
}
}
}
for(int i = 0;i<this->messages_updates.size() / 25;i++)
{
vector<int> block;
for(int j = 0;j<25;j++)
{
block.push_back(this->messages_updates[ j + ( i * 25)]);
}
blocks.push_back(block);
}
}
void TTH::Hash()
{
// parcours la liste des blocks
// phase 1
for(int i = 0;i<this->blocks.size();i++)
{
this->updatePrint(i);
auto copy = blocks;
//décalage du block courant
for(int k = 0;k<5;k++)
{
//nombre de fois que on décale circulaire la ligne
for(int r = 0;r<k;r++)
{
for(int z = 0 ;z< 4;z++)
{
blocks[i][(5 * k) + 4 - z] = blocks[i][(5 * k) + 4 - z - 1];
}
}
// remplacement des cases vides
for(int r = 0;r<k;r++)
{
blocks[i][(5 * k) + k - r - 1] = copy[i][(5 * k) + 4 - r];
}
}
this->updatePrint(i);
}
}
void TTH::Print()
{
for(int i = 0;i<5;i++)
{
cout << " " << print[i];
}
cout << "" << endl;
}
// recalcule de l'empreinte par rapport a un block
void TTH::updatePrint(int blockNumber)
{
for(int j = 0;j<5;j++)
{
print[0] = (print[0] + blocks[blockNumber][j * 5]) % 64;
print[1] = (print[1] + blocks[blockNumber][j * 5 + 1]) % 64;
print[2] = (print[2] + blocks[blockNumber][j * 5 + 2]) % 64;
print[3] = (print[3] + blocks[blockNumber][j * 5 + 3]) % 64;
print[4] = (print[4] + blocks[blockNumber][j * 5 + 4]) % 64;
}
}
#ifndef TTH_CLASS
#define TTH_CLASS
#include<vector>
#include<string>
using namespace std;
namespace Helper
{
class TTH
{
public :
TTH(std::vector<int> messages);
void Hash();
void Print();
private :
void updatePrint(int blockNumber);
int print[5];
std::vector<int> messages,messages_updates;
//messages_updates aprés le bourrages de messages
std::vector<std::vector<int>> blocks;
};
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment