Sunteți pe pagina 1din 6

Secured Hash Algorithm

INTRODUCTION:
SHA-1 that expands to Secure Hash Algorithm 1 is a cryptographic hash
function designed by the United States National Security Agency and is a
U.S. Federal Information Processing Standard published by the United States NIST.
SHA-1 produces a 160-bit (20-byte) hash value known as a message digest. A SHA-
1 hash value is typically rendered as a hexadecimal number which is 40 digits long.
Hashing takes in any length of data and converts it into uniform length digest. The
primary difference between encryption and hashing is that Hashing is not reversible.

VERSIONS OF SHA:

There are several versions of SHA as follows

SHA-0 : It is a 160-bit hash function published in 1993 under the name "SHA".
It was withdrawn shortly after publication due to an undisclosed "significant
flaw" and replaced by the slightly revised version SHA-1.

SHA-1: A 160-bit hash function which resembles the earlier MD5 algorithm.
This was designed by the National Security Agency (NSA) to be part of
the Digital Signature Algorithm. Cryptographic weaknesses were discovered
in SHA-1, and the standard was no longer approved for most cryptographic
uses after 2010.

SHA-2: A family of two similar hash functions, with different block sizes,
known as SHA-256and SHA-512. They differ in the word size; SHA-256 uses
32-bit words where SHA-512 uses 64-bit words. There are also truncated
versions of each standard, known as SHA-224, SHA-384, SHA-
512/224 and SHA-512/256. These were also designed by the NSA.

SHA-3: A hash function formerly called Keccak, chosen in 2012 after a public
competition among non-NSA designers. It supports the same hash lengths as
SHA-2, and its internal structure differs significantly from the rest of the SHA
family.
ALGORITHM FOR SHA 1:

The workflow of SHA1 involves the below stated operations

Initialize variables

Pre-processing

Process the message in successive 512-bit chunks

Extend the sixteen 32-bit words into eighty 32-bit words

Initialize hash value for this chunk

Main loop

Add this chunk's hash to result so far

Produce the final hash value (big-endian) as a 160-bit number

FIG 1: Block depiction of SHA1


IMPLEMENTATION USING C LANGUAGE:

The SHA1 hashing technique is performed using C programming to analyze the


digest it produces on processing a text file.
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>
#define rotateleft(x,n) ((x<<n) | (x>>(32-n)))
#define rotateright(x,n) ((x>>n) | (x<<(32-n)))
int i,j,m=0;
void SHA1(unsigned char * str1)
{unsigned long int h0,h1,h2,h3,h4,a,b,c,d,e,f,k,temp;
h0 = 0x67452301;
h1 = 0xEFCDAB89;
h2 = 0x98BADCFE;
h3 = 0x10325476;
h4 = 0xC3D2E1F0;
unsigned char * str;
str = (unsigned char *)malloc(strlen((const char *)str1)+100);
strcpy((char *)str,(const char *)str1);
int current_length = strlen((const char *)str);
int original_length = current_length;
str[current_length] = 0x80;
str[current_length + 1] = '\0';
char ic = str[current_length];
current_length++;
{word[j] = rotateleft((word[j-3] ^ word[j-
8] ^ word[j-14] ^ word[j-16]),1);}
int ib = current_length % 64;
a = h0;
if(ib<56)
b = h1;
ib = 56-ib;
c = h2;
for(i=0;i < ib;i++)
d = h3;
{str[current_length]=0x00;
e = h4;
current_length++;}
for(m=0;m<80;m++)
str[current_length + 1]='\0';
{if(m<=19)}
for(i=0;i<6;i++)
f = (b & c) | ((~b) & d);
{str[current_length]=0x0;
k = 0x5A827999;}
current_length++;}
else if(m<=39)
str[current_length] = (original_length *
8) / 0x100 ; {f = b ^ c ^ d;
current_length++; k = 0x6ED9EBA1;}
str[current_length] = (original_length * else if(m<=59)
8) % 0x100;
{f = (b & c) | (b & d) | (c & d);
current_length++;
k = 0x8F1BBCDC;}
str[current_length+i]='\0';
else
int number_of_chunks =
current_length/64; {f = b ^ c ^ d;

unsigned long int word[80]; k = 0xCA62C1D6;}

for(i=0;i<number_of_chunks;i++) temp = (rotateleft(a,5) + f + e + k +


word[m]) & 0xFFFFFFFF;
{for(j=0;j<16;j++)
e = d;
{word[j] = str[i*64 + j*4 + 0] *
0x1000000 + str[i*64 + j*4 + 1] * d = c;
0x10000 + str[i*64 + j*4 + 2] * 0x100 + c = rotateleft(b,30);
str[i*64 + j*4 + 3];}
b = a;
for(j=16;j<80;j++)
a = temp;}
h0 = h0 + a; FILE *fptr;
h1 = h1 + b; if ((fptr = fopen("sha_txt.txt", "r")) ==
NULL)
h2 = h2 + c;
{ printf("Error! opening file");
h3 = h3 + d;
exit(1);}
h4 = h4 + e;}
fscanf(fptr,"%[^\n]", c);
printf("\n\n");
printf("Data from the file:\n %s\n", c);
printf("Hash: %x%x%x%x%x",h0, h1,
h2, h3, h4); fclose(fptr);
printf("\n\n");} SHA1((unsigned char *)c);
int main() return 0;}
{ char c[1000];

INPUT:
A input text file containing the quick brown fox jumps over the lazy dog is fed as
the
OUTPUT:

FIG 2: Output of C program for SHA1


VERIFICATION:

In order to ensure that the program written is functioning efficiently, comparison was
made with online hash generator whose result is displayed below.

FIG 3: Output of Online hash calculator.

S-ar putea să vă placă și