Why is my MD5 Hash static for the whole process?

185 Views Asked by At

I'm using nettle for MD5 hashing(I also tried SHA1 with the same issue). Whenever I hash anything, the hash is always the same, even for radically different lengths and data.

    if (rs.response->body != NULL && rs.response->body->len > 0) { // body is a unsigned char*, len is a size_t, I have verified both here with a printf.
        struct md5_ctx md5ctx;
        md5_init(&md5ctx);
        md5_update(&md5ctx, rs.response->body->len, rs.response->body->data);
        unsigned char md5raw[16];
        md5_digest(&md5ctx, 16, md5raw);
        char md5[33];
        md5[32] = 0;
        for (int i = 0; i < 4; i++) {
            snprintf(md5 + (i * 8), 9, "%08X", md5raw + (i * 4));
        }
        printf("%s\n", md5);
    }

I've verified that they are different values, valgrind reports no invalid reads/writes, etc. The hash changes when I restart the process, but is never different during the process.

1

There are 1 best solutions below

0
On BEST ANSWER

snprintf takes the value argument. You are giving it a pointer, and it prints the pointer. Try

        snprintf(md5 + (i * 8), 9, "%08X", *(int *) (md5raw + (i * 4)));