static void test(u1 *data, u4 len, u1 *correct_digest) { u1 digest[20]; void *ctx; int i; ctx = SHA1_Init(); if(ctx == NULL) { printf("no memory\n"); exit(1); } SHA1_Calc(ctx, data, len); SHA1_Finish(ctx, digest); if(memcmp(correct_digest, digest, 20)) { printf("correct is :"); for(i = 0;i < 20;i++) printf("%02x ", correct_digest[i]); printf("\n"); printf("result is :"); for(i = 0;i < 20;i++) printf("%02x ", digest[i]); printf("\n"); } else { printf("test passed: "); for(i = 0;i < 20;i++) printf("%02x ", digest[i]); printf("\n"); } }
void process_jpeg(void *ptr, size_t sz, const char *filename) { SHA1_CTX hashsum; SHA1_Init(&hashsum); int state = STATE_0; int want_data = 1; uint8_t *c = ptr; for (; sz > 0; --sz, ++c) { if (state == STATE_0) { if (*c == 0xFF) { state = STATE_FF; } else { if (want_data) SHA1_Update(&hashsum, c, 1); } } else { assert(state == STATE_FF); if (*c != 0x00) { // This is a new section want_data = want_type[*c]; } if (want_data) SHA1_Update(&hashsum, c-1, 2); state = STATE_0; } } if (state == STATE_FF && want_data) { SHA1_Update(&hashsum, c-1, 1); } /* * Construct the hexdigest */ char sha[SHA1_DIGEST_STRING_LENGTH]; uint8_t digest[20]; SHA1_Finish(&hashsum, digest); for (int i = 0; i < 20; ++i) { sha[2*i] = hex[digest[i] >> 4]; sha[2*i+1] = hex[digest[i] & 15]; } sha[SHA1_DIGEST_STRING_LENGTH-1] = '\000'; printf("%s\t%s\n", sha, filename); }