I'm trying to implement the hash implementation of Sha-1 from the txt file "abc" and get a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
, but I'm getting the output E66882F9 DF84C901 18BADCFE 90325476 C3D2E1F0
. I know that the problem is in either my reading or convertChar
method, but I'm having trouble figuring out what my issue is.
For readfile, I need to append the 1 bit at the end of the buffer[]array and return the number of characters in the file. variable length is const unsigned int.
For convertArrayToIntArray, I need to convert from unsigned char into an equivalent array of unsigned ints by packing 4 chars into the 1 inter-variable.
unsigned int readFile (unsigned char buffer[])
{
length = 0;
int b = 0;
int i = 0;
char *fileName = "abc.txt";
FILE *filePointer = fopen (fileName, "rb");
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);
}
i = length - 1;
if (i < 0 && buffer[i] == '\n') {
buffer[i] = '\0';
}
length++;
buffer[length - 1] = 0x80;
}
fclose (filePointer);
return length;
}
void convertCharArrayToIntArray (unsigned char buffer[], unsigned int
message[], unsigned int sizeOfFileInBytes)
{
int e = 0;
size_t buffLength = length;
buffLength = (buffLength + 3) / 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]);
}
}