|
|
- #include <stdio.h>
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include "genSha1.h"
-
- #define rol(x,n) ((x << n) | (x >> (32-n)))
-
-
- void sha1(unsigned char* message,unsigned short messageLength,char *result){
- //////przygotowanie bloku
-
- int size = 64; //rozmiar jednego bloku musi być 512bit
- unsigned char block[size] = {0};
-
- for(int i = 0;i<messageLength;i++)block[i] = message[i];
- block[messageLength] = 0x80;//tuż po zakończeniu wiadomości dodaje bity 10000000
- messageLength =messageLength<<3; //rozmiar wiadomości w bitach
- block[63] = messageLength; // rozmiar wiadomość zapisany jako big-endian
- block[62] = messageLength >> 8;
-
-
- //////////właściwe obliczanie sha1 z bloku
-
- uint32_t w[80],a, b, c, d, e, tmp;
-
- //ustawianie szesnastu 32-bitowych słów
- w[0] = (block[0]<<24) + (block[1]<<16) + (block[2]<<8) + block[3];
- w[1] = (block[4]<<24) + (block[5]<<16) + (block[6]<<8) + block[7];
- w[2] = (block[8]<<24) + (block[9]<<16) + (block[10]<<8) + block[11];
- w[3] = (block[12]<<24) + (block[13]<<16) + (block[14]<<8) + block[15];
- w[4] = (block[16]<<24) + (block[17]<<16) + (block[18]<<8) + block[19];
- w[5] = (block[20]<<24) + (block[21]<<16) + (block[22]<<8) + block[23];
- w[6] = (block[24]<<24) + (block[25]<<16) + (block[26]<<8) + block[27];
- w[7] = (block[28]<<24) + (block[29]<<16) + (block[30]<<8) + block[31];
- w[8] = (block[32]<<24) + (block[33]<<16) + (block[34]<<8) + block[35];
- w[9] = (block[36]<<24) + (block[37]<<16) + (block[38]<<8) + block[39];
- w[10] = (block[40]<<24) + (block[41]<<16) + (block[42]<<8) + block[43];
- w[11] = (block[44]<<24) + (block[45]<<16) + (block[46]<<8) + block[47];
- w[12] = (block[48]<<24) + (block[49]<<16) + (block[50]<<8) + block[51];
- w[13] = (block[52]<<24) + (block[53]<<16) + (block[54]<<8) + block[55];
- w[14] = (block[56]<<24) + (block[57]<<16) + (block[58]<<8) + block[59];
- w[15] = (block[60]<<24) + (block[61]<<16) + (block[62]<<8) + block[63];
-
-
- //rozszerzanie szesnastu słów do osiemdziesięciu
- w[16] = rol((w[13] ^ w[8] ^ w[2] ^ w[0]),1);
- w[17] = rol((w[14] ^ w[9] ^ w[3] ^ w[1]),1);
- w[18] = rol((w[15] ^ w[10] ^ w[4] ^ w[2]),1);
- w[19] = rol((w[16] ^ w[11] ^ w[5] ^ w[3]),1);
- w[20] = rol((w[17] ^ w[12] ^ w[6] ^ w[4]),1);
- w[21] = rol((w[18] ^ w[13] ^ w[7] ^ w[5]),1);
- w[22] = rol((w[19] ^ w[14] ^ w[8] ^ w[6]),1);
- w[23] = rol((w[20] ^ w[15] ^ w[9] ^ w[7]),1);
- w[24] = rol((w[21] ^ w[16] ^ w[10] ^ w[8]),1);
- w[25] = rol((w[22] ^ w[17] ^ w[11] ^ w[9]),1);
- w[26] = rol((w[23] ^ w[18] ^ w[12] ^ w[10]),1);
- w[27] = rol((w[24] ^ w[19] ^ w[13] ^ w[11]),1);
- w[28] = rol((w[25] ^ w[20] ^ w[14] ^ w[12]),1);
- w[29] = rol((w[26] ^ w[21] ^ w[15] ^ w[13]),1);
- w[30] = rol((w[27] ^ w[22] ^ w[16] ^ w[14]),1);
- w[31] = rol((w[28] ^ w[23] ^ w[17] ^ w[15]),1);
- w[32] = rol((w[29] ^ w[24] ^ w[18] ^ w[16]),1);
- w[33] = rol((w[30] ^ w[25] ^ w[19] ^ w[17]),1);
- w[34] = rol((w[31] ^ w[26] ^ w[20] ^ w[18]),1);
- w[35] = rol((w[32] ^ w[27] ^ w[21] ^ w[19]),1);
- w[36] = rol((w[33] ^ w[28] ^ w[22] ^ w[20]),1);
- w[37] = rol((w[34] ^ w[29] ^ w[23] ^ w[21]),1);
- w[38] = rol((w[35] ^ w[30] ^ w[24] ^ w[22]),1);
- w[39] = rol((w[36] ^ w[31] ^ w[25] ^ w[23]),1);
- w[40] = rol((w[37] ^ w[32] ^ w[26] ^ w[24]),1);
- w[41] = rol((w[38] ^ w[33] ^ w[27] ^ w[25]),1);
- w[42] = rol((w[39] ^ w[34] ^ w[28] ^ w[26]),1);
- w[43] = rol((w[40] ^ w[35] ^ w[29] ^ w[27]),1);
- w[44] = rol((w[41] ^ w[36] ^ w[30] ^ w[28]),1);
- w[45] = rol((w[42] ^ w[37] ^ w[31] ^ w[29]),1);
- w[46] = rol((w[43] ^ w[38] ^ w[32] ^ w[30]),1);
- w[47] = rol((w[44] ^ w[39] ^ w[33] ^ w[31]),1);
- w[48] = rol((w[45] ^ w[40] ^ w[34] ^ w[32]),1);
- w[49] = rol((w[46] ^ w[41] ^ w[35] ^ w[33]),1);
- w[50] = rol((w[47] ^ w[42] ^ w[36] ^ w[34]),1);
- w[51] = rol((w[48] ^ w[43] ^ w[37] ^ w[35]),1);
- w[52] = rol((w[49] ^ w[44] ^ w[38] ^ w[36]),1);
- w[53] = rol((w[50] ^ w[45] ^ w[39] ^ w[37]),1);
- w[54] = rol((w[51] ^ w[46] ^ w[40] ^ w[38]),1);
- w[55] = rol((w[52] ^ w[47] ^ w[41] ^ w[39]),1);
- w[56] = rol((w[53] ^ w[48] ^ w[42] ^ w[40]),1);
- w[57] = rol((w[54] ^ w[49] ^ w[43] ^ w[41]),1);
- w[58] = rol((w[55] ^ w[50] ^ w[44] ^ w[42]),1);
- w[59] = rol((w[56] ^ w[51] ^ w[45] ^ w[43]),1);
- w[60] = rol((w[57] ^ w[52] ^ w[46] ^ w[44]),1);
- w[61] = rol((w[58] ^ w[53] ^ w[47] ^ w[45]),1);
- w[62] = rol((w[59] ^ w[54] ^ w[48] ^ w[46]),1);
- w[63] = rol((w[60] ^ w[55] ^ w[49] ^ w[47]),1);
- w[64] = rol((w[61] ^ w[56] ^ w[50] ^ w[48]),1);
- w[65] = rol((w[62] ^ w[57] ^ w[51] ^ w[49]),1);
- w[66] = rol((w[63] ^ w[58] ^ w[52] ^ w[50]),1);
- w[67] = rol((w[64] ^ w[59] ^ w[53] ^ w[51]),1);
- w[68] = rol((w[65] ^ w[60] ^ w[54] ^ w[52]),1);
- w[69] = rol((w[66] ^ w[61] ^ w[55] ^ w[53]),1);
- w[70] = rol((w[67] ^ w[62] ^ w[56] ^ w[54]),1);
- w[71] = rol((w[68] ^ w[63] ^ w[57] ^ w[55]),1);
- w[72] = rol((w[69] ^ w[64] ^ w[58] ^ w[56]),1);
- w[73] = rol((w[70] ^ w[65] ^ w[59] ^ w[57]),1);
- w[74] = rol((w[71] ^ w[66] ^ w[60] ^ w[58]),1);
- w[75] = rol((w[72] ^ w[67] ^ w[61] ^ w[59]),1);
- w[76] = rol((w[73] ^ w[68] ^ w[62] ^ w[60]),1);
- w[77] = rol((w[74] ^ w[69] ^ w[63] ^ w[61]),1);
- w[78] = rol((w[75] ^ w[70] ^ w[64] ^ w[62]),1);
- w[79] = rol((w[76] ^ w[71] ^ w[65] ^ w[63]),1);
-
-
- //inicjowanie wartościami początkowymi
- a = 0x67452301;
- b = 0xEFCDAB89;
- c = 0x98BADCFE;
- d = 0x10325476;
- e = 0xC3D2E1F0;
-
- //działania
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[0]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[1]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[2]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[3]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[4]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[5]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[6]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[7]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[8]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[9]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[10]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[11]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[12]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[13]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[14]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[15]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[16]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[17]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[18]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + w[19]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[20]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[21]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[22]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[23]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[24]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[25]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[26]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[27]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[28]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[29]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[30]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[31]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[32]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[33]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[34]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[35]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[36]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[37]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[38]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0x6ED9EBA1 + w[39]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[40]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[41]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[42]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[43]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[44]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[45]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[46]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[47]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[48]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[49]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[50]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[51]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[52]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[53]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[54]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[55]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[56]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[57]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[58]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + w[59]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[60]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[61]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[62]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[63]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[64]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[65]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[66]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[67]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[68]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[69]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[70]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[71]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[72]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[73]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[74]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[75]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[76]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[77]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[78]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
- tmp = (rol(a,5) + (b ^ c ^ d) + e + 0xCA62C1D6 + w[79]); e = d; d = c; c = rol(b,30); b = a; a = tmp;
-
- //zapis obliczonego sha1
- sprintf(result, "%08x%08x%08x%08x%08x", a +0x67452301, b + 0xEFCDAB89, c+ 0x98BADCFE, d + 0x10325476, e + 0xC3D2E1F0);
-
-
- }
|