int main (void) { int k; struct sha1_ctx ctx; uint8_t digest[20]; char output[80]; uint8_t retval = 0; for (k = 0; k < 2; k++) { sha1_init_ctx (&ctx); sha1_process_bytes ((const uint8_t*)test_data[k], &ctx, strlen(test_data[k])); sha1_finish_ctx (&ctx, digest); bin_to_hex(digest, output); if (strcmp(output, test_results[k])) { fprintf(stdout, "FAIL\n"); fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]); fprintf(stderr,"\t%s returned\n", output); fprintf(stderr,"\t%s is correct\n", test_results[k]); retval = 1; } } /* million 'a' vector we feed separately */ sha1_init_ctx (&ctx); for (k = 0; k < 1000000; k++) sha1_process_bytes ((const uint8_t*)"a", &ctx, 1); sha1_finish_ctx (&ctx, digest); bin_to_hex(digest, output); if (strcmp(output, test_results[2])) { fprintf(stdout, "FAIL\n"); fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]); fprintf(stderr,"\t%s returned\n", output); fprintf(stderr,"\t%s is correct\n", test_results[2]); retval = 1; } /* The same test as above, but with 1000 blocks of 1000 bytes. */ char buf[1000]; memset (buf, 'a', sizeof (buf)); sha1_init_ctx (&ctx); for (k = 0; k < 1000; ++k) sha1_process_bytes ((const uint8_t*)buf, &ctx, sizeof (buf)); sha1_finish_ctx (&ctx, digest); bin_to_hex(digest, output); if (strcmp(output, test_results[2])) { fprintf(stdout, "FAIL\n"); fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]); fprintf(stderr,"\t%s returned\n", output); fprintf(stderr,"\t%s is correct\n", test_results[2]); retval = 1; } /* success */ return retval; }
int hmac_sha1 (const void *key, size_t keylen, const void *in, size_t inlen, void *resbuf) { struct sha1_ctx inner; struct sha1_ctx outer; char optkeybuf[20]; char block[64]; char innerhash[20]; /* Reduce the key's size, so that it becomes <= 64 bytes large. */ if (keylen > 64) { struct sha1_ctx keyhash; sha1_init_ctx (&keyhash); sha1_process_bytes (key, keylen, &keyhash); sha1_finish_ctx (&keyhash, optkeybuf); key = optkeybuf; keylen = 20; } /* Compute INNERHASH from KEY and IN. */ sha1_init_ctx (&inner); memset (block, IPAD, sizeof (block)); memxor (block, key, keylen); sha1_process_block (block, 64, &inner); sha1_process_bytes (in, inlen, &inner); sha1_finish_ctx (&inner, innerhash); /* Compute result from KEY and INNERHASH. */ sha1_init_ctx (&outer); memset (block, OPAD, sizeof (block)); memxor (block, key, keylen); sha1_process_block (block, 64, &outer); sha1_process_bytes (innerhash, 20, &outer); sha1_finish_ctx (&outer, resbuf); return 0; }
void gc_hash_write (gc_hash_handle handle, size_t len, const char *data) { _gc_hash_ctx *ctx = handle; switch (ctx->alg) { #ifdef GNULIB_GC_MD2 case GC_MD2: md2_process_bytes (data, len, &ctx->md2Context); break; #endif #ifdef GNULIB_GC_MD4 case GC_MD4: md4_process_bytes (data, len, &ctx->md4Context); break; #endif #ifdef GNULIB_GC_MD5 case GC_MD5: md5_process_bytes (data, len, &ctx->md5Context); break; #endif #ifdef GNULIB_GC_SHA1 case GC_SHA1: sha1_process_bytes (data, len, &ctx->sha1Context); break; #endif default: break; } }
void HashUpdateSHA1(HASH *Hash, const char *Data, int Len) { sha1_process_bytes(Data,Len,(struct sha1_ctx *) Hash->Ctx); }