-1

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]);
    }
}
David C. Rankin
  • 69,681
  • 6
  • 44
  • 72
thale
  • 23
  • 4
  • 1
    `if (length > MAX_SIZE)` dont be afraid. infinity wont be > infinity. – wildplasser May 12 '17 at 00:07
  • `if (i < 0 && buffer[i] == '\n')` is *Undefined Behavior* unless `buffer` already points to at least the '`i`th' element **within** the original `buffer` filled by `fread`. (which it does not in this case). What is the rule against attempting to access memory outside of **array bounds**? – David C. Rankin May 13 '17 at 03:15
  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve)." There is no possible way to match your expected output from some text file `"abc"` without knowing what file abc contains. Your expected `sha1sum` isn't correct for the literal text `"abc"`. – David C. Rankin May 13 '17 at 03:24

1 Answers1

0

You were also forgetting that there is a space every 8 characters, this would cause an error in the packing of the chars.

#define MAX_SIZE 256
unsigned int length = 0;
void convertCharArrayToIntArray(unsigned char buffer[], unsigned int message[])
{ 
    int e = 0;
    int cnt=0;
    //int d = 0; 
    //buffLength shows me how many chars are in array
    size_t buffLength= length;
    //printf("%i tot", buffLength); 
    buffLength = (buffLength+ 3)/4;
    printf("%d\n",buffLength);
    int crs=0;
    for (e=0 ; e< buffLength-1; e++)
    {
        unsigned int n=0;
        n |= (buffer[crs  ] << 24);
        n |= (buffer[crs+1] << 16);
        n |= (buffer[crs+2] << 8 );
        n |= (buffer[crs+3]      );
        printf("4 chars :>%c%c%c%c to uint=%d\n",buffer[crs  ],buffer[crs+1],buffer[crs+2],buffer[crs+3],n );

        crs+=4;
        cnt++;
        if(cnt==2)/*We can not forget that there is a space between every 8 characters ...*/
        {
            cnt=0;
            crs++;
        }

        message[e]=n;/*Save our uint in buffer*/
    }

}

unsigned int readFile(unsigned char buffer[])
{ 
    //int sizer;
    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);
            return 0;/*error*/
        }
        if (length > MAX_SIZE )
        {
            fputs("Input file too big for program", stderr);
            return 0;/*error*/
        }
        /*
        It is not necessary to add a null at the end, the buffer is already initialized with zeros
        */
        /*Did not display the content correctly because it was in hexadecimal*/
        for (i = 0; i < length; i++) 
            printf("%c", buffer[i]);/*use %c show real data, it is rubbish but work */

        /*for (i = 0; i < length; i++) 
        printf("%08x", buffer[i]); */

    }

    /*OR print*/
    printf("\n\n%s\n",buffer);
    fclose(filePointer);
    return length;
}


void main()
{
    unsigned char szbuffer[1024]={0};/*initialized to zeroes*/
    if(readFile(szbuffer))
    {
      unsigned int message[100];
      convertCharArrayToIntArray(szbuffer,message);
    }
    else
       printf("error...\n");
    return;
}
  • How do you know there is a space after every 8 characters? (are you talking about is *expected/actual Output*?) Also, the proper declarations for `main` are `int main (void)` and `int main (int argc, char **argv)` (which you will see written with the equivalent `char *argv[]`). **note:** `main` is a function of `type int` and it returns a value. See [**See What should main() return in C and C++?**](http://stackoverflow.com/questions/204476/) – David C. Rankin May 13 '17 at 03:31
  • He mentioned at the beginning of the question : a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d That's what I realized – Edson K. Cury May 13 '17 at 03:35
  • Yes, the **output** he included in his question, but I did not take that to mean that there were spaces in his input (which we have no clue what it is because he didn't post the contents of file `"abc"`). I'm not knocking you on it, it just does not appear that it is a valid conclusion to draw from the question -- which, granted, does not provide the required MCVE. My concern is that your `cnt` may not produce the required results. – David C. Rankin May 13 '17 at 03:45
  • He mentioned that he got the text abc.txt from the string: "from the txt file "abc" and get a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d , but I'm getting the output E66882F9 DF84C901 18BADCFE 90325476 C3D2E1F0" cnt skips space in string – Edson K. Cury May 13 '17 at 03:48
  • I see what you are saying, but I took the *"I'm trying to ... and get"* to mean the output he **wants** to get and then explains *"but I'm getting the output ..."*. I can't say with 100% confidence either way based on the quality of the question -- that's why I can't fault you for your assumption, I'm just pointing out that, to me, that appears unlikely what he intended `:)` See my last comment to the original question, until he provides a valid MCVE, we will never know. – David C. Rankin May 13 '17 at 03:54