sha256.pdf

4
// Implementation of SHA-256 #include<iostream> #include<cstring> #include<conio.h> using namespace std; #define ch(x,y,z) (z^(x&(y^z))) #define maj(x,y,z) (y^((x^y)&(y^z))) #define rtol(x,n) ((x<<n) | (x>>(32-n))) //cyclic left shift #define rtor(x,n) ((x>>n) | (x<<(32-n))) //cyclic right shit #define shr(x,n) (x>>n) //right shift #define s0(x) (rtor(x,2)^rtor(x,13)^rtor(x,22)) #define s1(x) (rtor(x,6)^rtor(x,11)^rtor(x,25)) #define s2(x) (rtor(x,7)^rtor(x,18)^(shr(x,3))) #define s3(x) (rtor(x,17)^rtor(x,19)^(shr(x,10))) int main() { typedef unsigned int word32; typedef unsigned int hashwordtype; hashwordtype h[8]={0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; //Initial hash value word32 k[64]={ // 64 constant 32-bit word 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; word32 w[64],w1[10][64],a,b,c,d,e,f,g,h1,T1,T2; //Variable declaration char str[150]; int m[150],a1,a2,a3,a4,a6,a7,i,j; //message initialization for(i=0;i<150;i++) m[i]=0; //Intiailizing working variable for(i=0;i<10;i++) { for(j=0;j<64;j++) w1[i][j]=0; } //getting the message for which hash value iis generated

description

Sha256 program in cpp , sha algorithm

Transcript of sha256.pdf

Page 1: sha256.pdf

// Implementation of SHA-256#include<iostream>#include<cstring>#include<conio.h>using namespace std;#define ch(x,y,z) (z^(x&(y^z)))#define maj(x,y,z) (y^((x^y)&(y^z)))#define rtol(x,n) ((x<<n) | (x>>(32-n))) //cyclic left shift#define rtor(x,n) ((x>>n) | (x<<(32-n))) //cyclic right shit#define shr(x,n) (x>>n) //right shift#define s0(x) (rtor(x,2)^rtor(x,13)^rtor(x,22))#define s1(x) (rtor(x,6)^rtor(x,11)^rtor(x,25))#define s2(x) (rtor(x,7)^rtor(x,18)^(shr(x,3)))#define s3(x) (rtor(x,17)^rtor(x,19)^(shr(x,10)))int main(){typedef unsigned int word32;typedef unsigned int hashwordtype;hashwordtype h[8]={0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; //Initial hash valueword32 k[64]={ //64 constant 32-bit word

0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };

word32 w[64],w1[10][64],a,b,c,d,e,f,g,h1,T1,T2;//Variable declarationchar str[150];int m[150],a1,a2,a3,a4,a6,a7,i,j;//message initializationfor(i=0;i<150;i++)m[i]=0;//Intiailizing working variablefor(i=0;i<10;i++){for(j=0;j<64;j++)w1[i][j]=0;}//getting the message for which hash value iis generated

Page 2: sha256.pdf

cout<<"ENTER THE MESSAGE :";cin>>str;a1=strlen(str);cout<<endl<<"ENTERTED MESSAGE IS:";for(i =0;i<a1;i++){m[i]=str[i];cout<<str[i];}m[a1]=0x80;m[a1+1]=0;m[a1+2]=0;m[a1+3]=0; //appending the message withsingle one and zeroscout<<endl;// MESSAGE PREPROCESSING CONVERTING IT TO BLOCK OF 512 BITSfor( i=0;i<64;i++)w[i]=0;if(a1<56) //if message lenght is lessthan 448 bits{a6=0;w1[a6][15]=a1*8;a2=0;for(i=0;i<=a1;i++){w1[a6][a2]=m[i]<<24|m[i+1]<<16|m[i+2]<<8|m[i+3]; //make a 32bit word bycombing 4 byte togetheri=i+3;a2++;}cout<<"VALUE OF MESSAGE IN HEX IS:"<<endl; // Display message blockfor(i=0;i<16;i++){cout<<hex<<w1[a6][i]<<" ";}cout<<endl;}else //if message length is more than448 bit{a3=a1/64;a4=a1%64;a6=a3+1;a2=0;if(a3==0)a3=1;for(j=0;j<a3;j++){for(i=0;i<=a1;i++){w1[j][a2]=m[i]<<24|m[i+1]<<16|m[i+2]<<8|m[i+3]; //make a 32bit word bycombing 4 byte togetheri=i+3;a2++;}

Page 3: sha256.pdf

}w1[a3][15]=a1*8;a2=0;for (i=1;i<=a4;i++){w1[a3][a2]=m[(i+64)]<<24|m[(i+1+64)]<<16|m[(i+2+64)]<<8|m[(i+3+64)];//make a 32bit word by combing 4 byte togetheri=i+3;a2++;}cout<<"VALUE OF MESSAGE IN HEX IS:"<<endl;

for(j=0;j<=a3;j++){cout<<"Message block No "<<j+1<<": "; //display the messageblockfor(i=0;i<16;i++)cout<<hex<<w1[j][i]<<" ";cout<<endl;}}a7=0;do{///Message schedulingfor (i=0;i<16;i++)w[i]=w1[a7][i];

for(i=16;i<64;i++){w[i]=s3(w[i-2])+w[i-7]+s2(w[i-15])+w[i-16];}//Iniatiazing the eight variablea=h[0];b=h[1];c=h[2];d=h[3];e=h[4];f=h[5];g=h[6];h1=h[7];//Algorithim implementationfor(i=0;i<64;i++){

T1=h1+s1(e)+ch(e,f,g)+k[i]+w[i];T2=s0(a)+maj(a,b,c);h1=g;g=f;f=e;e=d+T1;d=c;c=b;b=a;a=T1+T2;}

// update the hash valueh[0]=a+h[0];h[1]=b+h[1];h[2]=c+h[2];

Page 4: sha256.pdf

h[3]=d+h[3];h[4]=e+h[4];h[5]=f+h[5];h[6]=g+h[6];h[7]=h1+h[7];

a7++;}while(a7<=a6);cout<<endl<<"MESSAGE DIGEST GENERATED BY SHA-256 IS:"<<endl;for (int i=0;i<8;i++)cout<<hex<<h[i];cout<<endl<<endl;getch();return 0;}