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; }
OOP SHA1AllocOOP (void) { struct sha1_ctx ctx; sha1_init_ctx (&ctx); return vmProxy->byteArrayToOOP ((char *) &ctx, sizeof (ctx)); }
void HashInitSHA(HASH *Hash, int Len) { switch (Len) { case 512: Hash->Ctx=(void *) calloc(1,sizeof(SHA2_SHA512_CTX)); SHA2_SHA512_Init((SHA2_SHA512_CTX *) Hash->Ctx); Hash->Update=HashUpdateSHA512; Hash->Finish=HashFinishSHA512; Hash->Clone=HashCloneSHA512; break; case 256: Hash->Ctx=(void *) calloc(1,sizeof(SHA2_SHA256_CTX)); SHA2_SHA256_Init((SHA2_SHA256_CTX *) Hash->Ctx); Hash->Update=HashUpdateSHA256; Hash->Finish=HashFinishSHA256; Hash->Clone=HashCloneSHA256; break; default: Hash->Ctx=(void *) calloc(1,sizeof(struct sha1_ctx)); sha1_init_ctx((struct sha1_ctx *) Hash->Ctx); Hash->Update=HashUpdateSHA1; Hash->Finish=HashFinishSHA1; Hash->Clone=HashCloneSHA1; break; } }
void HashInitSHA1(THash *Hash) { Hash->Ctx=(void *) calloc(1,sizeof(struct sha1_ctx)); sha1_init_ctx((struct sha1_ctx *) Hash->Ctx); Hash->Update=HashUpdateSHA1; Hash->Finish=HashFinishSHA1; }
Gc_rc gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle) { _gc_hash_ctx *ctx; Gc_rc rc = GC_OK; ctx = calloc (sizeof (*ctx), 1); ctx->alg = hash; ctx->mode = mode; switch (hash) { #ifdef GC_USE_MD2 case GC_MD2: md2_init_ctx (&ctx->md2Context); break; #endif #ifdef GC_USE_MD4 case GC_MD4: md4_init_ctx (&ctx->md4Context); break; #endif #ifdef GC_USE_MD5 case GC_MD5: md5_init_ctx (&ctx->md5Context); break; #endif #ifdef GC_USE_SHA1 case GC_SHA1: sha1_init_ctx (&ctx->sha1Context); break; #endif default: rc = GC_INVALID_HASH; break; } switch (mode) { case 0: break; default: rc = GC_INVALID_HASH; break; } if (rc == GC_OK) *outhandle = ctx; else free (ctx); return rc; }
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; }