#include "pch.h"
#include <string>
#include <fstream>
#include <iostream>
#include "windows.h"
#include "math.h"
using namespace std;
class Hash
{
private:
bool *hash;
string text;
int length;
int rozr;
int *mas;
public:
Hash() :length(0),rozr(0)
{
}
void initiate()
{
cout << "Введіть розрядність хешу - (8,16,32,64,128,256)"<<endl;
cin>>rozr;
string tmpstr;
ifstream file("D:/text.txt");
int tmp=0;
while (!file.eof())
{
getline(file, tmpstr);
text += tmpstr;
}
file.close();
length = rozr/8;
if (text.length() % length == 0) length = text.length();
else
{
int var = text.length();
while (var%length!=0)
{
var+=1;
}
length = var;
}
mas = new int[length];
for (int i = 0; i < text.length(); i++)
{
tmp = int(text[i]);
if (tmp < 0) tmp = 256 + tmp;
mas[i] = tmp;
}
if (text.length() % (rozr / 8) != 0)
{
for (int i = text.length(); i < length; i++)
{
mas[i] = 0;
}
}
hash = new bool[rozr];
for (int i = 0; i < rozr; i++)
{
hash[i] = false;
}
}
bool* tobin(int numb)
{
bool * binary = new bool[8];
for (int i = 0; i < 8; i++)
{
binary[i] = false;
}
int y = 1, x = numb, i = 0;
while (y != 0)
{
y = x / 2;
if (x % 2 == 1) binary[i] = true;
x = y;
i++;
}
bool *newbinary = new bool[8];
i = 7;
for (int j = 0; j < 8; j++)
{
newbinary[i] = binary[j];
i--;
}
delete[] binary;
return newbinary;
}
bool * together(int *x)
{
bool *ret = new bool[rozr];
bool *tmp = new bool[8];
int j = 0;
for (int i = 0; i < rozr / 8; i++)
{
tmp = tobin(x[i]);
for (int k = 0; k < 8; k++)
{
ret[j] = tmp[k];
j++;
}
delete[]tmp;
}
return ret;
}
void left(bool *x)
{
bool tmp = x[0];
for (int i = 0; i < rozr; i++)
{
x[i] = x[i+1];
}
x[rozr-1] = tmp;
}
void hashfunc()
{
bool *tmphash;
cout << "Довжина тексту: " << text.length() << endl;
cout << "Необхідна довжина: " << length << endl;
for (int i = 0; i < length-1; i += (rozr/8))
{
cout << "Букви: ";
for (int j = i; j < i + rozr / 8; j++)
{
if (j > text.length()) cout << " ";
else cout << text[j]<<' ';
}
cout << endl;
cout << "Числа: ";
for (int j = i; j < i + rozr / 8; j++)
{
cout << mas[j] << ' ';
}
cout << endl;
int *tmpmas = new int[rozr/8];
int k = 0;
for (int j = i; j < i+rozr / 8; j++)
{
tmpmas[k] = mas[j];
k++;
}
tmphash = together(tmpmas);
cout << "Переведені числа: ";
for (int j = 0; j < rozr; j++)
{
cout << tmphash[j];
}
left(hash);
cout << endl;
cout << "Хеш: ";
for (int j = 0; j < rozr; j++)
{
cout << hash[j];
}
cout << endl;
for (int j = 0; j < rozr; j++)
{
hash[j] = hash[j] ^ tmphash[j];
}
cout << "XOR: ";
for (int j = 0; j < rozr; j++)
{
cout << hash[j];
}
cout << endl;
}
}
void tofile()
{
ofstream file("D:\hash.txt");
for (int i = 0; i < rozr; i++)
{
file << hash[i];
}
file << endl;
file.close();
}
int toint(bool *x)
{
int k = 0;
int j = 7;
for (int i = 0; i < 8; i++)
{
k += int(pow(2, j))*int(x[i]);
j--;
}
return k;
}
void tosimb()
{
bool *tmp=new bool[8];
string text2;
int k = 0;
for (int i = 0; i < rozr/8; i++)
{
for (int j = 0; j < 8; j++)
{
tmp[j] = hash[k];
k++;
}
text2+=char(toint(tmp));
}
cout << "Хеш текстом: " << text2 << endl;
ofstream file("D:\hash.txt", ios::app);
file << text2;
file.close();
}
void show()
{
initiate();
hashfunc();
cout << "Hash " << endl;
for (int i = 0; i < rozr; i++)
{
cout << hash[i];
}
cout << endl;
tofile();
tosimb();
system("pause");
}
~Hash()
{
delete[]mas;
}
};
class menu
{
private:
bool flag;
Hash x1;
public:
menu() :flag(true)
{
}
void start()
{
int choice = 0;
while (flag)
{
system("cls");
cout << "Меню: " << endl;
cout << "Створити хеш повідомлення - 1" << endl;
cout << "Вихід - 2" << endl;
cin >> choice;
switch (choice)
{
case 1:
x1.show();
break;
case 2:
flag = false;
break;
}
}
}
};
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
menu x;
x.start();
}