You can have information about SHA1 algorithm form Wikipedia.
follow http://en.wikipedia.org/wiki/SHA-1
Source code language : C++
System : Linux
---------------------------------------------------------------------------------------------
/*Implement SHA1 algorithm*/
/*Note : All values are in big Endian format*/
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
char ip[512];
unsigned long *ipl;
int len;
unsigned long aa=0x01234567;
unsigned long bb=0x89abcdef;
unsigned long cc=0x76543210;
unsigned long dd=0xfedcba98;
unsigned long ee=0xc4d5e6f7;
unsigned long leftrotate(unsigned long a,int n)
{
a=(a<<n)|(a>>(32-n));
return a;
}
int main()
{
system("clear");
cout<<"\nEnter String :: ";
cin>>ip;
len=strlen(ip);
int l=len+1;//gets the total no of words
while(l%16!=14)//till 448 mod 512
l++;
l=l+2;
cout<<"\nl="<<l;
/* Convert to long*/
ipl=new unsigned long[l];
for(int i=0;i<len;i++)
{
ipl[i]=(unsigned long)ip[i];
}
/*Step 1: Padding*/
ipl[len]=0x80000000;
len++;
//padding 0 till 448
for(int i=len;i<l-2;i++)
ipl[i]=0;
/*Step 2: Adding*/
ipl[l-2]=ipl[0];
ipl[l-1]=ipl[1];
cout<<"\n\nInput String :: ";
for(int i=0;i<l;i++)
cout<<" "<<ipl[i];
int n=l/16;
int st=0,ed=16;
/*Step 3: Actual Processing*/
cout<<"\n\nOutput :: ";
while(n!=0)
{
unsigned long ipb[80];
int j=0;
for(int i=st;i<ed;i++)
ipb[j++]=ipl[i];
/*Step 4: Expansion*/
for(j=j;j<80;j++)
{
ipb[j]=leftrotate((ipb[j-3]^ipb[j-8]^ipb[j-14]^ipb[j-16]),1);
}
unsigned long a,b,c,d,e,f,k;
a=aa;
b=bb;
c=cc;
d=dd;
e=ee;
/*Step 5: Processing*/
for(int i=0;i<80;i++)
{
if(i>=0&&i<=19)
{
f=(b & c) | ((~ b) & d);
k= 0x5a827999;
}
if(i>=20&&i<=39)
{
f=b^c^d;
k=0x6ed9eba1;
}
if(i>=40&&i<=59)
{
f = (b & c) | (b & d) | (c & d);
k = 0x8f1bbcdc;
}
if(i>=60&&i<=79)
{
f=b^c^d;
k = 0xca62c1d6;
}
unsigned long temp;
temp=leftrotate(a,5)+f+k+e+ipb[i];
e=d;
d=c;
c=leftrotate(b,30);
b=a;
a=temp;
}
aa=aa+a;
bb=bb+b;
cc=cc+c;
dd=dd+d;
ee=ee+e;
cout<<" "<<hex<<aa<<" "<<hex<<bb<<" "<<hex<<cc<<" "<<hex<<dd<<" "<<hex<<ee;
n--;
st=ed;
ed=ed+16;
}
cout<<"\n";
return 0;
}
--------------------------------------------------------------------------------------------------
SHA1 Implementation:
$ g++ sha1.cpp
$ ./a.out
output:
Enter String :: chakreshprasad
l=32
Input String :: 99 104 97 107 114 101 115 104 112 114 97 115 97 100 2147483648 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 104
Output :: 2f2364da a3f511e0 4c771ed5 5d3a85ef e9092224 ce37ff65 4a8edb22 d39cf363 d134c70f 9330c66f
follow http://en.wikipedia.org/wiki/SHA-1
Source code language : C++
System : Linux
---------------------------------------------------------------------------------------------
/*Implement SHA1 algorithm*/
/*Note : All values are in big Endian format*/
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
char ip[512];
unsigned long *ipl;
int len;
unsigned long aa=0x01234567;
unsigned long bb=0x89abcdef;
unsigned long cc=0x76543210;
unsigned long dd=0xfedcba98;
unsigned long ee=0xc4d5e6f7;
unsigned long leftrotate(unsigned long a,int n)
{
a=(a<<n)|(a>>(32-n));
return a;
}
int main()
{
system("clear");
cout<<"\nEnter String :: ";
cin>>ip;
len=strlen(ip);
int l=len+1;//gets the total no of words
while(l%16!=14)//till 448 mod 512
l++;
l=l+2;
cout<<"\nl="<<l;
/* Convert to long*/
ipl=new unsigned long[l];
for(int i=0;i<len;i++)
{
ipl[i]=(unsigned long)ip[i];
}
/*Step 1: Padding*/
ipl[len]=0x80000000;
len++;
//padding 0 till 448
for(int i=len;i<l-2;i++)
ipl[i]=0;
/*Step 2: Adding*/
ipl[l-2]=ipl[0];
ipl[l-1]=ipl[1];
cout<<"\n\nInput String :: ";
for(int i=0;i<l;i++)
cout<<" "<<ipl[i];
int n=l/16;
int st=0,ed=16;
/*Step 3: Actual Processing*/
cout<<"\n\nOutput :: ";
while(n!=0)
{
unsigned long ipb[80];
int j=0;
for(int i=st;i<ed;i++)
ipb[j++]=ipl[i];
/*Step 4: Expansion*/
for(j=j;j<80;j++)
{
ipb[j]=leftrotate((ipb[j-3]^ipb[j-8]^ipb[j-14]^ipb[j-16]),1);
}
unsigned long a,b,c,d,e,f,k;
a=aa;
b=bb;
c=cc;
d=dd;
e=ee;
/*Step 5: Processing*/
for(int i=0;i<80;i++)
{
if(i>=0&&i<=19)
{
f=(b & c) | ((~ b) & d);
k= 0x5a827999;
}
if(i>=20&&i<=39)
{
f=b^c^d;
k=0x6ed9eba1;
}
if(i>=40&&i<=59)
{
f = (b & c) | (b & d) | (c & d);
k = 0x8f1bbcdc;
}
if(i>=60&&i<=79)
{
f=b^c^d;
k = 0xca62c1d6;
}
unsigned long temp;
temp=leftrotate(a,5)+f+k+e+ipb[i];
e=d;
d=c;
c=leftrotate(b,30);
b=a;
a=temp;
}
aa=aa+a;
bb=bb+b;
cc=cc+c;
dd=dd+d;
ee=ee+e;
cout<<" "<<hex<<aa<<" "<<hex<<bb<<" "<<hex<<cc<<" "<<hex<<dd<<" "<<hex<<ee;
n--;
st=ed;
ed=ed+16;
}
cout<<"\n";
return 0;
}
--------------------------------------------------------------------------------------------------
SHA1 Implementation:
$ g++ sha1.cpp
$ ./a.out
output:
Enter String :: chakreshprasad
l=32
Input String :: 99 104 97 107 114 101 115 104 112 114 97 115 97 100 2147483648 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 104
Output :: 2f2364da a3f511e0 4c771ed5 5d3a85ef e9092224 ce37ff65 4a8edb22 d39cf363 d134c70f 9330c66f
No comments:
Post a Comment