0

I'm creating a hashing program that takes either a file or stdin and prints out the hash in either another file or to the terminal via stdout. My issue is I'm getting different hash values based on the same stdin because the hex values of the argument change.

the part of my code where this happens is here:

    for (i = 3; i < argc; i++) {
        if (strcmp(argv[i], "-i") == 0 && argv[i+1] != NULL) {
            in = argv[i+1];
            in = strtok(in, " ");
    printf("arg \"%s\" hex: %02x\n", argv[i+1], argv[i+1]);
            inCheck = 1;
            i++;
        }
        else if (strcmp(argv[i], "-o") == 0 && argv[i+1] != NULL) {
            out = argv[i+1];
            i++;
        }
        else 
            printf("Unknown argument %s. Ignoring.\n", argv[i]);
    }

Basically here I'm checking the hex-value of the input following "-i" in the command line. And when I enter:

./executable hash -sha -i hello -o world I get the output arg "hello" hex: ce4a2823 and when I enter:

./executable hash -sha -i hello I get the output arg "hello" hex: 247f582c and when I enter:

./executable hash -sha -o world -i hello I get the output arg "hello" hex: 57e2f82c

So I'm wondering why the hex-value of the string keeps changing?

ChangeJar
  • 163
  • 1
  • 1
  • 10
  • 2
    Since we can't see the code that calculates the hash - or any of the supporting code - there's no way to tell. You should prepare a [minimal compilable example](https://stackoverflow.com/help/mcve) which reproduces the problem and post _that_. – Useless Jul 15 '14 at 18:12
  • 1
    The hex number that you're printing is the address of the string in memory. On most modern systems, that value will change every time you run the program, even if you give the program the exact same arguments. Side note: the strings in the `argv` array never contain spaces (because spaces are the delimiter between command line arguments), so the `strtok` call is guaranteed to do nothing. – user3386109 Jul 15 '14 at 18:41
  • I showed you the minimal code example. The hash part comes way later and it's during this part that I'm having issues with. Which is why I posted what I did. As far as strtok, I had pretty much figured that out on my own, but added that while checking why things could be different and I didn't get around to removing it before I posted, but thank you, it is definitely good to know. The memory address makes sense, and is what I suspected but wasn't entirely sure. So thank you for that. – ChangeJar Jul 15 '14 at 19:14
  • @user3386109 `argv` strings can certainly contain spaces. How else could you (for example) handle file names with spaces in them? – hyde Jul 15 '14 at 20:35
  • @hyde yup you're right, you can use quotes to force spaces in the arguments. But in that case, you wouldn't use `strtok` to truncate the argument at the first space. – user3386109 Jul 15 '14 at 21:24

1 Answers1

1

You are not printing some "hex value of string", you are printing address of the string. If you want to print the hex value of the first char in the string, you can do this:

printf("arg \"%s\" hex: %02x\n", argv[i+1], argv[i+1][0]);

Hex value is possibly different, because your OS randomizes the location of the stack each time a program is run, in order to make buffer overrun/underrun bug exploits more difficult. And the argv[] strings may be stored in the stack (without checking, I think they have to be modifiable, and their length is not static of course, so putting them to the stack at program startup is a natural solution to that).


Since you talk about "hex value of the string", a note: C string is basically just address of a piece of memory (such as a char array) containing 1 or more char: the characters of the string, and the string terminating '\0' byte (which must be there even if string is empty). It's a very primitive thing compared to string types of most other languages.

hyde
  • 60,639
  • 21
  • 115
  • 176