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; }
int HashFinishSHA1(THash *Hash, int Encoding, char **HashStr) { int count, len; char *Tempstr=NULL, *DigestBuff=NULL; DigestBuff=(char *) calloc(1,SHA1LEN+1); sha1_finish_ctx((struct sha1_ctx *) Hash->Ctx, DigestBuff); free(Hash->Ctx); if (Encoding > 0) { *HashStr=EncodeBytes(*HashStr, DigestBuff, SHA1LEN, Encoding); len=StrLen(*HashStr); } else { len=SHA1LEN; *HashStr=SetStrLen(*HashStr,len); memcpy(*HashStr,DigestBuff,len); } DestroyString(DigestBuff); DestroyString(Tempstr); return(len); }
int HashFinishSHA1(HASH *Hash, char **HashStr) { int len; char *DigestBuff=NULL; DigestBuff=(char *) calloc(1,SHA1LEN+1); sha1_finish_ctx((struct sha1_ctx *) Hash->Ctx, DigestBuff); len=SHA1LEN; *HashStr=SetStrLen(*HashStr,len); memcpy(*HashStr,DigestBuff,len); DestroyString(DigestBuff); return(len); }
const char * gc_hash_read (gc_hash_handle handle) { _gc_hash_ctx *ctx = handle; const char *ret = NULL; switch (ctx->alg) { #ifdef GNULIB_GC_MD2 case GC_MD2: md2_finish_ctx (&ctx->md2Context, ctx->hash); ret = ctx->hash; break; #endif #ifdef GNULIB_GC_MD4 case GC_MD4: md4_finish_ctx (&ctx->md4Context, ctx->hash); ret = ctx->hash; break; #endif #ifdef GNULIB_GC_MD5 case GC_MD5: md5_finish_ctx (&ctx->md5Context, ctx->hash); ret = ctx->hash; break; #endif #ifdef GNULIB_GC_SHA1 case GC_SHA1: sha1_finish_ctx (&ctx->sha1Context, ctx->hash); ret = ctx->hash; break; #endif default: return NULL; } return ret; }
bfd_boolean generate_build_id (bfd *abfd, const char *style, checksum_fn checksum_contents, unsigned char *id_bits, int size ATTRIBUTE_UNUSED) { if (streq (style, "md5")) { struct md5_ctx ctx; md5_init_ctx (&ctx); if (!(*checksum_contents) (abfd, (sum_fn) &md5_process_bytes, &ctx)) return FALSE; md5_finish_ctx (&ctx, id_bits); } else if (streq (style, "sha1")) { struct sha1_ctx ctx; sha1_init_ctx (&ctx); if (!(*checksum_contents) (abfd, (sum_fn) &sha1_process_bytes, &ctx)) return FALSE; sha1_finish_ctx (&ctx, id_bits); } else if (streq (style, "uuid")) { #ifndef __MINGW32__ int n; int fd = open ("/dev/urandom", O_RDONLY); if (fd < 0) return FALSE; n = read (fd, id_bits, size); close (fd); if (n < size) return FALSE; #else /* __MINGW32__ */ typedef RPC_STATUS (RPC_ENTRY * UuidCreateFn) (UUID *); UUID uuid; UuidCreateFn uuid_create = 0; HMODULE rpc_library = LoadLibrary ("rpcrt4.dll"); if (!rpc_library) return FALSE; uuid_create = (UuidCreateFn) (void (WINAPI *)(void)) GetProcAddress (rpc_library, "UuidCreate"); if (!uuid_create) { FreeLibrary (rpc_library); return FALSE; } if (uuid_create (&uuid) != RPC_S_OK) { FreeLibrary (rpc_library); return FALSE; } FreeLibrary (rpc_library); memcpy (id_bits, &uuid, (size_t) size < sizeof (UUID) ? (size_t) size : sizeof (UUID)); #endif /* __MINGW32__ */ } else if (strneq (style, "0x", 2)) { /* ID is in string form (hex). Convert to bits. */ const char *id = style + 2; size_t n = 0; do { if (ISXDIGIT (id[0]) && ISXDIGIT (id[1])) { id_bits[n] = read_hex (*id++) << 4; id_bits[n++] |= read_hex (*id++); } else if (*id == '-' || *id == ':') ++id; else abort (); /* Should have been validated earlier. */ } while (*id != '\0'); } else abort (); /* Should have been validated earlier. */ return TRUE; }