Sha-1的C实现中的哈希值错误

时间:2017-05-11 00:30:15

标签: c hash bit-shift sha

我的文件输入包含字符" abc"并且应该生成哈希:" a9993e364706816aba3e25717850c26c9cd0d89d"相反,我的输出是2474716729 148412417 414899454 2419217526 3285377520.我的位移应该是正确的但是当我打印出我的W [t]我很确定数字是错误的。

#include <stdio.h>
#define MAX_SIZE  1048576
static unsigned char buffer[MAX_SIZE];
static unsigned int message[MAX_SIZE];

unsigned int readFile(unsigned char buffer[])
{

size_t length = 0;
int b = 0;
for (b = 0; b < MAX_SIZE; b++)
{
    buffer[b]= '\0';
}
int i = 0;
char *fileName = "abc.txt";
FILE *filePointer = fopen(fileName, "r");
if (filePointer != NULL)
{
    length = fread(buffer, sizeof(char), MAX_SIZE, filePointer);
    if (ferror(filePointer)!= 0)
    {
        fputs("Error", stderr);
    }
    if (length > MAX_SIZE)
    {
         fputs("Input file too big for program", stderr);
    }

    //add one bit;
    buffer[length] = 0x80;
    //length++;

}

/*for (i = 0; i < MAX_SIZE; i++)
{
    printf("%c", buffer[i]);
}
*/

fclose(filePointer);
return length;
}

unsigned int calculateBlocks(unsigned int sizeOfFileInBytes)
{
int blockCount = (((8 * sizeOfFileInBytes) + 1) / 512) + 1;

if((((8 * sizeOfFileInBytes) + 1) % 512) > (512 - 64))
{
    blockCount = blockCount + 1;
}
//printf("here's block count: %i", blockCount);
return blockCount;
}

void convertCharArrayToIntArray(unsigned char buffer[], unsigned int 
message[], unsigned int sizeOfFileInBytes)
 {
int e = 0;
int d = 0;
//buffLength shows me how many chars are in array
size_t buffLength= strlen((char*)buffer);
//printf("this is bufflength: %zu   -done", buffLength);


buffLength = buffLength/4;
for (e=0 ; e< buffLength; e++)
{
    message[e] |= (buffer[e] <<24);
    message[e] |= (buffer[e+1] <<16);
    message[e] |= (buffer[e+2] <<8);
    message[e] |= (buffer[e+3]);
}

/*for (d = 0; d <MAX_SIZE; d++)
{
    printf("%i", message[d]);
} */
//printf("- done with message[d]");
}

void addBitCountToLastBlock(unsigned int message[], unsigned int 
sizeOfFileInBytes, unsigned int blockCount)
{
int indexOfEndOfLastBlock = blockCount* 16 -1;
//printf("the size of file in bytes is: %i   -done", 
sizeOfFileInBytes);
sizeOfFileInBytes = sizeOfFileInBytes * 8;
message[indexOfEndOfLastBlock] = sizeOfFileInBytes;
 //printf("index of last block is %i", message[indexOfEndOfLastBlock]);

  int d = 0;

  /* for (d = 0; d <MAX_SIZE; d++)
 {
 printf("%i", message[d]);

 }
   printf("- done with message[d]"); */
 }

unsigned int K(unsigned int t)
{
unsigned int kHex[] = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
if (t>= 0 && t <= 19)
{
    return kHex[0];
}
else if (t>= 20 && t <= 39)
{
    return kHex[1];
}
else if (t>= 40 && t <= 59)
{
    return kHex[2];
}
else if (t>= 60 && t <= 79)
{
    return kHex[3];
}

return 0;
}

unsigned int f(unsigned int t, unsigned int B, unsigned int C, unsigned 
int D)
{
if (t>= 0 && t <= 19)
{
    return (B & C) |((~B)&D);
}
else if (t>= 20 && t <= 39)
{
    return B^C^D;
}
else if (t>= 40 && t <= 59)
{
    return (B & C) | (B & D) | (C & D);
}
else if (t>= 60 && t <= 79)
{
    return B^C^D;
}
return 0 ;
}

void computeMessageDigest(unsigned int message[], unsigned int 
blockCount)
void computeMessageDigest(unsigned int message[], unsigned int 
blockCount)
{
unsigned int A, B, C, D, E;
unsigned int h[5];
unsigned int W[80];
unsigned int t = 0;
unsigned int temp; //temporary word value
unsigned int bCounter = 0;
int i = 0;
int d = 0 ;
// int z = 0;
//int blCount = 0;

/* for (d = 0; d <MAX_SIZE; d++)
{
    printf("%i", message[d]);

}
printf("- done with message[d]"); */

int wInc = 0;


h[0] = 0x67452301;
h[1] = 0xefcdab89;
h[2] = 0x98badcfe;
h[3] = 0x10325476;
h[4] = 0xc3d2e1f0;

while ( bCounter < blockCount)
{
    for (i= 16 * bCounter; i < 16 *bCounter +16; i++)
    {
        W[wInc] = message[i];
        wInc++; //block one
    }
    wInc = 0;
   /*for (z = 0; z < 16; z++)
  {
    printf("%i", W[z]);
  } */

  for (t = 16; t < 80; t++)
   {
    W[t] = (W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]) << 1;

   // printf("%u  ", W[t]);
   }



A = h[0];
B = h[1];
C = h[2];
D = h[3];
E = h[4];

  for (t = 0; t < 80; t++)
  {

    temp = (A << 5) + f(t, B, C, D) + E + W[t] + K(t);
    E = D;
    D = C;
    C = B << 30;
    B = A;
    A = temp;
  }

    printf("Hex values for A, B, C, D, E"); 
    printf("%u, %u, %u, %u, %u", A, B, C, D, E) ;


h[0] += A;
h[1] += B;
h[2] += C;
h[3] += D;
h[4] += E;

int b = 0;
for (b = 0; b <5; b++)
printf("%u ", h[b]);
   bCounter++;
}

int main(int argc, const char * argv[]) {
// insert code here...


unsigned int blockCount = 0;
unsigned int sizeOfFile = 0;

sizeOfFile = readFile(buffer);
/*for (i = 0; i < MAX_SIZE; i++)
 {
 printf("%c", buffer[i]);
 } */

//printf("%i is the size", readFile(buffer));
blockCount = calculateBlocks(sizeOfFile);
convertCharArrayToIntArray(buffer, message, sizeOfFile);
//printf("\n");

addBitCountToLastBlock(message, sizeOfFile, blockCount);
computeMessageDigest(message, blockCount);

return 0;

0 个答案:

没有答案