1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#define EXPORT extern "C" __declspec(dllexport)
// Parametres
using namespace std;
// Déclaration des variables
unsigned short int i;
unsigned short int y;
short int k;
short unsigned int z;
unsigned int t;
int h[5];
int a;
int b;
int c;
int d;
int e;
unsigned short int mask;
unsigned int s;
unsigned int K;
unsigned int temp;
unsigned int ft;
string retour;
//string nom = "Mou";
//string taille = "che";
std::string mot;
union u_init
{
unsigned short int entier[16];
char carac[64];
};
unsigned int S(unsigned short int n, unsigned int x)
{
x = (x << n)|(x >> 32-n);
return x;
}
EXPORT char* hash(char* mot)
{
// déclaration des variables
union u_init init;
// mise en place du mots
z = 0;
i = 8*9;
k = 448 - (i + 1);
if (k < 0)
{
k += k + 512;
}
char * nomchar = new char[40];
strncpy( nomchar, mot, 40 );
y = i/8;
y = y-1;
while (z <= y)
{
init.carac[z] = nomchar[z];
++z;
}
init.carac[z] = 0x80;
z += 1;
y = k/16;
y = y + z;
while ( z <= y)
{
init.carac[z] = 0x00;
z += 1;
}
y = 32 - (i/8) + 8;
init.carac[y] = i;
h[0] = 0x67452301;
h[1] = 0xefcdab89;
h[2] = 0x98badcfe;
h[3] = 0x10325476;
h[4] = 0xc3d2e1f0;
// traitement
a = h[0];
b = h[1];
c = h[2];
d = h[3];
e = h[4];
mask = 0x0000000F;
t = 0;
while (t<=79)
{
if (t <= 19)
{
K = 0x5A827999;
}
if ((t >19) && (t <= 59))
{
K = 0x8F1BBCDC;
}
if ((t >60) && (t <= 79))
{
K = 0xCA62C1D6;
}
s = t&mask;
if (t >=16)
{
init.carac[s] = S(1,(init.entier[(s+13)&mask]) xor (init.entier[(s+8)&mask]) xor (init.entier[(s+2)&mask]) xor (init.entier[s]));
}
if (t<=19)
{
ft = (b&c)|((!b)&d);
}
if ((t >= 20) && (t <= 39))
{
ft = b xor c xor d;
}
if ((t >= 40) && (t <= 59))
{
ft = (b&c)|(b&d)|(c&d);
}
if (t >=60)
{
ft = b xor c xor d;
}
temp = S(5,a) + ft + e + init.carac[s] + K;
e = d;
d = c;
c = S(30,b);
b = a;
a = temp;
if (t == 79)
{
h[0] = h[0]+a;
h[1] = h[1]+b;
h[2] = h[2]+c;
h[3] = h[3]+d;
h[4] = h[4]+e;
std::ostringstream retour;
retour << std::hex << h[0] << h[1] << h[2] << h[3] << h[4];
std::string test(retour.str());
string::size_type len;
len = test.length();
void* teest;
char * out;
teest = malloc(sizeof(char)*40);
out = (char*)teest;
strncpy(out, test.c_str(), len);
return teest;
}
t = t+1;
}
} |
Partager