Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef BASE_H_INCLUDED
- #define BASE_H_INCLUDED
- // Definicije tipova:
- typedef unsigned int uint32; // Pretpostavicemo da ce ovo biti 32-bita
- typedef unsigned char uint1; // - || - 8-bita
- enum {
- S11 = 7, S12 = 12, S13 = 17, S14 = 22,
- S21 = 5, S22 = 9, S23 = 14, S24 = 20,
- S31 = 4, S32 = 11, S33 = 16, S34 = 23,
- S41 = 6, S42 = 10, S43 = 15, S44 = 21,
- blockSize = 64
- };
- #endif // BASE_H_INCLUDED
- #ifndef MD5_H_INCLUDED
- #define MD5_H_INCLUDED
- #include "base.h"
- #include <string>
- #include <iostream>
- using namespace std;
- class MD5Hash {
- private:
- bool done; // Kontrolna promenljiva koja signalizira da je doslo do kraja procesiranja MD5 hasha
- bool detailed; // Kontrolna promenljiva koja odredjuje tip prikaza na ekranu
- uint1 buffer[blockSize]; // Bitovi koji nisu stali u 64bitni blok
- uint32 count[2]; // 64-bitni brojac bitova
- uint32 state[4]; // Podaci u obradi
- uint1 digest[16]; // Rezultat
- // Logicke operacije:
- static uint32 rotateLeft(uint32 x, int n);
- static uint32 MD5_F(uint32 x, uint32 y, uint32 z);
- static uint32 MD5_G(uint32 x, uint32 y, uint32 z);
- static uint32 MD5_H(uint32 x, uint32 y, uint32 z);
- static uint32 MD5_I(uint32 x, uint32 y, uint32 z);
- static void MD5_FF(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac);
- static void MD5_GG(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac);
- static void MD5_HH(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac);
- static void MD5_II(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac);
- static void MD5_decode(uint32 output[], const uint1 input[], uint32 len);
- static void MD5_encode(uint1 output[], const uint32 input[], uint32 len);
- void m_init();
- void MD5Transform(const uint1 block[blockSize]);
- public:
- MD5Hash();
- MD5Hash(const string& txt);
- MD5Hash(const string& txt, bool val);
- string MD5Input(const string str);
- string MD5Input(const string str, bool val);
- void MD5Process(const unsigned char *buf, uint32 length);
- void MD5Process(const char *buf, uint32 length);
- void setDetailed(bool);
- bool getDetailed() const;
- MD5Hash& MD5Compute();
- string hexOutput() const;
- friend ostream& operator << (ostream&, MD5Hash md5);
- };
- #endif // MD5_H_INCLUDED
- #include "md5.h"
- #include "base.h"
- #include <cstdio>
- /* Uradjeno na osnovu algoritma: http://www.ietf.org/rfc/rfc1321.txt */
- //------------------------------------------------------------------------------------------------------------------------------------------
- bool MD5Hash::getDetailed() const {
- return detailed;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- void MD5Hash::setDetailed(bool val) {
- detailed = val;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- MD5Hash::MD5Hash() {
- m_init();
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Konsturktor za direktno proslijedjivanje stringa na obradu:
- MD5Hash::MD5Hash(const string &txt) {
- m_init();
- setDetailed(false);
- MD5Process(txt.c_str(), txt.length());
- MD5Compute();
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Konstruktor za direktno proslijedjivanje stringa na obradu sa dodatnom bool promenljivom koristenom za STANDARD|DETAILED output
- MD5Hash::MD5Hash(const string &txt, bool val) {
- m_init();
- setDetailed(val);
- MD5Process(txt.c_str(), txt.length());
- MD5Compute();
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- void MD5Hash::m_init() {
- done = false;
- count[0] = 0;
- count[1] = 0;
- // Magicni inicijalizatori...
- state[0] = 0x67452301;
- state[1] = 0xefcdab89;
- state[2] = 0x98badcfe;
- state[3] = 0x10325476;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // F, G, H, I logicke operacije za MD5 hashovanje
- uint32 MD5Hash::MD5_F(uint32 x, uint32 y, uint32 z) {
- return (x & y) | (~x & z);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- uint32 MD5Hash::MD5_G(uint32 x, uint32 y, uint32 z) {
- return (x & z) | (y & ~z);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- uint32 MD5Hash::MD5_H(uint32 x, uint32 y, uint32 z) {
- return x ^ y ^ z;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- uint32 MD5Hash::MD5_I(uint32 x, uint32 y, uint32 z) {
- return y ^ (x | ~z);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Rotiramo x ulijevo za n bita
- uint32 MD5Hash::rotateLeft(uint32 x, int n) {
- return (x << n) | (x >> (32 - n));
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // FF, GG, HH, II transformacijske funkcije za 4 kruga MD5 hasha
- void MD5Hash::MD5_FF(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac) {
- a = rotateLeft(a+ MD5_F(b, c, d) + x + ac, s) + b;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- void MD5Hash::MD5_GG(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac) {
- a = rotateLeft(a + MD5_G(b, c, d) + x + ac, s) + b;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- void MD5Hash::MD5_HH(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac) {
- a = rotateLeft(a + MD5_H(b, c, d) + x + ac, s) + b;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- void MD5Hash::MD5_II(uint32 &a, uint32 b, uint32 c, uint32 d, uint32 x, uint32 s, uint32 ac) {
- a = rotateLeft(a + MD5_I(b, c, d) + x + ac, s) + b;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Dekodiramo unos (unsigned char) u uint4 izlaz.
- void MD5Hash::MD5_decode(uint32 output[], const uint1 input[], uint32 len) {
- for (uint32 i = 0, j = 0; j < len; i++, j += 4)
- output[i] = ((uint32)input[j]) | (((uint32)input[j+1]) << 8) | (((uint32)input[j+2]) << 16) | (((uint32)input[j+3]) << 24);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Kodiramo ulaz (uint32) u unsigned char izlaz.
- void MD5Hash::MD5_encode(uint1 output[], const uint32 input[], uint32 len) {
- for (uint32 i = 0, j = 0; j < len; i++, j += 4) {
- output[j] = input[i] & 0xff;
- output[j+1] = (input[i] >> 8) & 0xff;
- output[j+2] = (input[i] >> 16) & 0xff;
- output[j+3] = (input[i] >> 24) & 0xff;
- }
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Primjena MD5 algoritma na blok podataka
- void MD5Hash::MD5Transform(const uint1 block[blockSize]) {
- uint32 a = state[0];
- uint32 b = state[1];
- uint32 c = state[2];
- uint32 d = state[3];
- uint32 x[16];
- MD5_decode(x, block, blockSize);
- if (getDetailed()) {
- cout << "Inicijalne vrijednosti:" << endl;
- cout << "A = " << a << endl << "B = " << b << endl << "C = " << c << endl << "D = " << d << endl << "X = " << x << endl << endl;
- }
- // 1. krug
- MD5_FF(a, b, c, d, x[0], S11, 0xd76aa478); // 1
- MD5_FF(d, a, b, c, x[1], S12, 0xe8c7b756);
- MD5_FF(c, d, a, b, x[2], S13, 0x242070db);
- MD5_FF(b, c, d, a, x[3], S14, 0xc1bdceee);
- MD5_FF(a, b, c, d, x[4], S11, 0xf57c0faf);
- MD5_FF(d, a, b, c, x[5], S12, 0x4787c62a);
- MD5_FF(c, d, a, b, x[6], S13, 0xa8304613);
- MD5_FF(b, c, d, a, x[7], S14, 0xfd469501);
- MD5_FF(a, b, c, d, x[8], S11, 0x698098d8);
- MD5_FF(d, a, b, c, x[9], S12, 0x8b44f7af);
- MD5_FF(c, d, a, b, x[10], S13, 0xffff5bb1);
- MD5_FF(b, c, d, a, x[11], S14, 0x895cd7be);
- MD5_FF(a, b, c, d, x[12], S11, 0x6b901122);
- MD5_FF(d, a, b, c, x[13], S12, 0xfd987193);
- MD5_FF(c, d, a, b, x[14], S13, 0xa679438e);
- MD5_FF(b, c, d, a, x[15], S14, 0x49b40821); // 16
- if (getDetailed()) {
- cout << "Prolaz 1. kruga: " << endl;
- cout << "A = " << a << endl << "B = " << b << endl << "C = " << c << endl << "D = " << d << endl << "X = " << x << endl << endl;
- }
- // 2. krug
- MD5_GG(a, b, c, d, x[1], S21, 0xf61e2562); // 17
- MD5_GG(d, a, b, c, x[6], S22, 0xc040b340);
- MD5_GG(c, d, a, b, x[11], S23, 0x265e5a51);
- MD5_GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);
- MD5_GG(a, b, c, d, x[5], S21, 0xd62f105d);
- MD5_GG(d, a, b, c, x[10], S22, 0x2441453);
- MD5_GG(c, d, a, b, x[15], S23, 0xd8a1e681);
- MD5_GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);
- MD5_GG(a, b, c, d, x[9], S21, 0x21e1cde6);
- MD5_GG(d, a, b, c, x[14], S22, 0xc33707d6);
- MD5_GG(c, d, a, b, x[3], S23, 0xf4d50d87);
- MD5_GG(b, c, d, a, x[8], S24, 0x455a14ed);
- MD5_GG(a, b, c, d, x[13], S21, 0xa9e3e905);
- MD5_GG(d, a, b, c, x[2], S22, 0xfcefa3f8);
- MD5_GG(c, d, a, b, x[7], S23, 0x676f02d9);
- MD5_GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); // 32
- if (getDetailed()) {
- cout << "Prolaz 2. kruga: " << endl;
- cout << "A = " << a << endl << "B = " << b << endl << "C = " << c << endl << "D = " << d << endl << "X = " << x << endl << endl;
- }
- // 3. krug
- MD5_HH(a, b, c, d, x[5], S31, 0xfffa3942); // 33
- MD5_HH(d, a, b, c, x[8], S32, 0x8771f681);
- MD5_HH(c, d, a, b, x[11], S33, 0x6d9d6122);
- MD5_HH(b, c, d, a, x[14], S34, 0xfde5380c);
- MD5_HH(a, b, c, d, x[1], S31, 0xa4beea44);
- MD5_HH(d, a, b, c, x[4], S32, 0x4bdecfa9);
- MD5_HH(c, d, a, b, x[7], S33, 0xf6bb4b60);
- MD5_HH(b, c, d, a, x[10], S34, 0xbebfbc70);
- MD5_HH(a, b, c, d, x[13], S31, 0x289b7ec6);
- MD5_HH(d, a, b, c, x[0], S32, 0xeaa127fa);
- MD5_HH(c, d, a, b, x[3], S33, 0xd4ef3085);
- MD5_HH(b, c, d, a, x[6], S34, 0x4881d05);
- MD5_HH(a, b, c, d, x[9], S31, 0xd9d4d039);
- MD5_HH(d, a, b, c, x[12], S32, 0xe6db99e5);
- MD5_HH(c, d, a, b, x[15], S33, 0x1fa27cf8);
- MD5_HH(b, c, d, a, x[2], S34, 0xc4ac5665); // 48
- if (getDetailed()) {
- cout << "Prolaz 3. kruga: " << endl;
- cout << "A = " << a << endl << "B = " << b << endl << "C = " << c << endl << "D = " << d << endl << "X = " << x << endl << endl;
- }
- // 4. krug
- MD5_II(a, b, c, d, x[0], S41, 0xf4292244); // 49
- MD5_II(d, a, b, c, x[7], S42, 0x432aff97);
- MD5_II(c, d, a, b, x[14], S43, 0xab9423a7);
- MD5_II(b, c, d, a, x[5], S44, 0xfc93a039);
- MD5_II(a, b, c, d, x[12], S41, 0x655b59c3);
- MD5_II(d, a, b, c, x[3], S42, 0x8f0ccc92);
- MD5_II(c, d, a, b, x[10], S43, 0xffeff47d);
- MD5_II(b, c, d, a, x[1], S44, 0x85845dd1);
- MD5_II(a, b, c, d, x[8], S41, 0x6fa87e4f);
- MD5_II(d, a, b, c, x[15], S42, 0xfe2ce6e0);
- MD5_II(c, d, a, b, x[6], S43, 0xa3014314);
- MD5_II(b, c, d, a, x[13], S44, 0x4e0811a1);
- MD5_II(a, b, c, d, x[4], S41, 0xf7537e82);
- MD5_II(d, a, b, c, x[11], S42, 0xbd3af235);
- MD5_II(c, d, a, b, x[2], S43, 0x2ad7d2bb);
- MD5_II(b, c, d, a, x[9], S44, 0xeb86d391); // 64
- if (getDetailed()) {
- cout << "Prolaz 4. kruga i vrijednosti prije spajanja sa inicijalnim vrijednostima:" << endl;
- cout << "A = " << a << endl << "B = " << b << endl << "C = " << c << endl << "D = " << d << endl << "X = " << x << endl << endl;
- }
- state[0] += a;
- state[1] += b;
- state[2] += c;
- state[3] += d;
- if (getDetailed()) {
- cout << "Vrijednosti nakon spajanja sa inicijalnim vrijednostima: " << endl;
- cout << "A = " << state[0] << endl << "B = " << state[1] << endl << "C = " << state[2] << endl << "D = " << state[3] << endl;
- cout << "Puni state oputput: " << state[0] << state[1] << state[2] << state[3] << endl << endl;
- }
- // Resetujemo x
- memset(x, 0, sizeof x);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Compute funkcija koja nastavlja MD5 algoritam na drugi blok podataka
- void MD5Hash::MD5Process(const unsigned char input[], uint32 length) {
- // Broj bitova mod 64
- uint32 index = count[0] / 8 % blockSize;
- uint32 i;
- if ((count[0] += (length << 3)) < (length << 3))
- count[1]++;
- count[1] += (length >> 29);
- // Broj bitova koje trebamo staviti u buffer
- uint32 firstpart = 64 - index;
- // Uradi transformaciju sto je vise puta moguce
- if (length >= firstpart) {
- // Punimo buffer
- memcpy(&buffer[index], input, firstpart);
- MD5Transform(buffer);
- // Transformacije dijelova od velicine bloka (64bit)
- for (i = firstpart; i + blockSize <= length; i += blockSize)
- MD5Transform(&input[i]);
- index = 0;
- }
- else
- i = 0;
- // Output buffer-a
- memcpy(&buffer[index], &input[i], length-i);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Za svaki slucaj, opcija za signed char
- void MD5Hash::MD5Process(const char input[], uint32 length) {
- MD5Process((const unsigned char*)input, length);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Krajnji korak hashovanja:
- MD5Hash& MD5Hash::MD5Compute() {
- static unsigned char padding[64] = {
- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- };
- if (!done) {
- unsigned char bits[8];
- MD5_encode(bits, count, 8);
- uint32 index = count[0] / 8 % 64;
- uint32 padLen = (index < 56) ? (56 - index) : (120 - index);
- MD5Process(padding, padLen);
- MD5Process(bits, 8);
- MD5_encode(digest, state, 16);
- // Resetujemo buffer i brojac
- memset(buffer, 0, sizeof(buffer));
- memset(count, 0, sizeof(count));
- done = true;
- }
- return *this;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Vracamo hex vrijednost kao string:
- string MD5Hash::hexOutput() const {
- if (!done)
- return "";
- char buf[33];
- for(int i=0; i<16; i++)
- sprintf(buf+i*2, "%02x", digest[i]);
- buf[32] = 0;
- return string(buf);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Prilagodimo operator nasim potrebama
- ostream& operator <<(ostream& out, MD5Hash md5) {
- return out << md5.hexOutput();
- }
- // Funkcija za unos stringa sa bool promenljivom
- string MD5Hash::MD5Input(const string str, bool val) {
- MD5Hash md5 = MD5Hash(str, val);
- return md5.hexOutput();
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- // Funkcija za unos stringa bez bool promenljive
- string MD5Hash::MD5Input(const string str) {
- MD5Hash md5 = MD5Hash(str);
- return md5.hexOutput();
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- #ifndef MENU_H_INCLUDED
- #define MENU_H_INCLUDED
- class Menu {
- public:
- Menu();
- void initMenu();
- };
- #endif // MENU_H_INCLUDED
- #include "menu.h"
- #include "config.h"
- #include "utils.h"
- #include "md5.h"
- #include <iostream>
- #include <stdlib.h>
- #include <ctype.h>
- using namespace std;
- Menu::Menu() {}
- //------------------------------------------------------------------------------------------------------------------------------------------
- void Menu::initMenu() {
- int c = -1; /* Korisnicki unos */
- int len = 0; /* Duzina za generisanje random stringa */
- char r = '0'; /* Odgovor (response) korisnika */
- string txt = ""; /* Uneseni text */
- string rezim = ""; /* Rezim prikaza podataka na konzoli */
- Config cfg; /* Instanca objekta tipa Config */
- Utils u; /* Instanca objekta tipa Utils */
- MD5Hash md5; /* Instanca objekta tipa MD5Hash */
- md5.setDetailed(false); /* Pocetni rezim prikaza na konzoli je STANDARD */
- do {
- rezim = (md5.getDetailed()) ? "DETALJNO" : "STANDARDNO";
- cout << "Trenutni rezim prikaza: " << rezim << endl;
- cout << "Meni :: MD5 - [NANSI] :: " << cfg.getAuthor() << " :: Verzija " << cfg.getStrVersion() << endl;
- cout << "1. MD5 Hash - Rucni unos stringa." << endl;
- cout << "2. Promjena rezima prikaza (standard <> detailed)." << endl;
- cout << "3. MD5 Hash (Test primjerci)." << endl;
- cout << "4. MD5 Hash od slucajno generisanog stringa." << endl;
- cout << "5. Stress Test" << endl;
- cout << "6. Izlaz" << endl << ">> ";
- cin >> c;
- switch(c) {
- case 1:
- u.cls();
- cout << "Unesite zeljeni tekst: " << endl << ">> ";
- cin >> txt;
- cout << "MD5 Hash od '" << txt << "' stringa je: " << md5.MD5Input(txt, md5.getDetailed()) << endl << endl << endl;
- break;
- case 2:
- u.cls();
- md5.setDetailed(!md5.getDetailed());
- cout << "Rezim prikaza informacija je uspjesno promenjen!" << endl << endl;
- break;
- case 3:
- u.cls();
- cout << "MD5 Hash od 'test' stringa: " << md5.MD5Input("test", md5.getDetailed()) << endl;
- cout << "MD5 Hash od 'ftn' stringa: " << md5.MD5Input("ftn", md5.getDetailed()) << endl;
- cout << "MD5 Hash od 'goran' stringa: " << md5.MD5Input("goran", md5.getDetailed()) << endl;
- cout << "Svi navedeni primjeri su testirani sa PHP md5() funkcijom i daju iste rezultate." << endl << endl;
- break;
- case 4:
- u.cls();
- do {
- cout << "Unesite zeljenu duzinu stringa:" << endl << ">> ";
- cin >> len;
- } while ((len <= 0) && (!isdigit(len)));
- txt = u.generateRandomString(len);
- cout << "MD5 Hash9 za slucajno generisani string '" << txt << "' je: " << endl << md5.MD5Input(txt, md5.getDetailed()) << endl << endl;
- break;
- case 5:
- for (int i = 0; i<= 2000; i++) {
- cout << md5.MD5Input(u.generateRandomString(i)) << endl;
- }
- break;
- case 6:
- do {
- cout << "\tDa li sigurno zelite da izadjete?? [y/n]" << endl << ">> ";
- cin >> r;
- } while (r != 'y' && r != 'n');
- if (r == 'y')
- exit(EXIT_SUCCESS);
- else {
- u.cls();
- initMenu();
- break;
- }
- default:
- u.cls();
- cout << "Nepoznat unos!" << endl << endl;
- }
- } while( ((c < 1) || (c > 6)) && cin.good());
- }
- //------------------------------------------------------------------------------------------------------------------------------------------
- #ifndef CONFIG_H_INCLUDED
- #define CONFIG_H_INCLUDED
- #include <string>
- using namespace std;
- class Config {
- private:
- string author;
- string s_version;
- int i_version;
- public:
- Config();
- string getAuthor() const;
- string getStrVersion() const;
- int getIntVersion() const;
- };
- #endif // CONFIG_H_INCLUDED
- #include "config.h"
- #include "string.h"
- Config::Config() {
- author = "Goran Todorovic RA 170/2011";
- s_version = "0.0.1";
- i_version = 001;
- }
- string Config::getAuthor() const {
- return author;
- }
- string Config::getStrVersion() const {
- return s_version;
- }
- int Config::getIntVersion() const {
- return i_version;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement