NTSTATUS WINAPI BCryptCreateHash( BCRYPT_ALG_HANDLE algorithm, BCRYPT_HASH_HANDLE *handle, UCHAR *object, ULONG objectlen, UCHAR *secret, ULONG secretlen, ULONG flags ) { struct algorithm *alg = algorithm; UCHAR buffer[MAX_HASH_BLOCK_BITS / 8] = {0}; struct hash *hash; int block_bytes; NTSTATUS status; int i; TRACE( "%p, %p, %p, %u, %p, %u, %08x - stub\n", algorithm, handle, object, objectlen, secret, secretlen, flags ); if (flags) { FIXME( "unimplemented flags %08x\n", flags ); return STATUS_NOT_IMPLEMENTED; } if (!alg || alg->hdr.magic != MAGIC_ALG) return STATUS_INVALID_HANDLE; if (object) FIXME( "ignoring object buffer\n" ); if (!(hash = heap_alloc( sizeof(*hash) ))) return STATUS_NO_MEMORY; hash->hdr.magic = MAGIC_HASH; hash->alg_id = alg->id; hash->hmac = alg->hmac; /* initialize hash */ if ((status = hash_init( &hash->inner, hash->alg_id ))) goto end; if (!hash->hmac) goto end; /* initialize hmac */ if ((status = hash_init( &hash->outer, hash->alg_id ))) goto end; block_bytes = alg_props[hash->alg_id].block_bits / 8; if (secretlen > block_bytes) { struct hash_impl temp; if ((status = hash_init( &temp, hash->alg_id ))) goto end; if ((status = hash_update( &temp, hash->alg_id, secret, secretlen ))) goto end; if ((status = hash_finish( &temp, hash->alg_id, buffer, alg_props[hash->alg_id].hash_length ))) goto end; } else { memcpy( buffer, secret, secretlen ); } for (i = 0; i < block_bytes; i++) buffer[i] ^= 0x5c; if ((status = hash_update( &hash->outer, hash->alg_id, buffer, block_bytes ))) goto end; for (i = 0; i < block_bytes; i++) buffer[i] ^= (0x5c ^ 0x36); status = hash_update( &hash->inner, hash->alg_id, buffer, block_bytes ); end: if (status != STATUS_SUCCESS) { heap_free( hash ); return status; } *handle = hash; return STATUS_SUCCESS; }
NTSTATUS WINAPI BCryptFinishHash( BCRYPT_HASH_HANDLE handle, UCHAR *output, ULONG size, ULONG flags ) { UCHAR buffer[MAX_HASH_OUTPUT_BYTES]; struct hash *hash = handle; NTSTATUS status; int hash_length; TRACE( "%p, %p, %u, %08x\n", handle, output, size, flags ); if (!hash || hash->hdr.magic != MAGIC_HASH) return STATUS_INVALID_HANDLE; if (!output) return STATUS_INVALID_PARAMETER; if (!hash->hmac) return hash_finish( &hash->inner, hash->alg_id, output, size ); hash_length = alg_props[hash->alg_id].hash_length; if ((status = hash_finish( &hash->inner, hash->alg_id, buffer, hash_length ))) return status; if ((status = hash_update( &hash->outer, hash->alg_id, buffer, hash_length ))) return status; return hash_finish( &hash->outer, hash->alg_id, output, size ); }
NTSTATUS WINAPI BCryptFinishHash( BCRYPT_HASH_HANDLE handle, UCHAR *output, ULONG size, ULONG flags ) { struct hash *hash = handle; TRACE( "%p, %p, %u, %08x\n", handle, output, size, flags ); if (!hash || hash->hdr.magic != MAGIC_HASH) return STATUS_INVALID_HANDLE; if (!output) return STATUS_INVALID_PARAMETER; return hash_finish( hash, output, size ); }
static int hash_write_trailer(struct AVFormatContext *s) { struct HashContext *c = s->priv_data; char buf[256]; av_strlcpy(buf, av_hash_get_name(c->hash), sizeof(buf) - 200); av_strlcat(buf, "=", sizeof(buf) - 200); hash_finish(s, buf); av_hash_freep(&c->hash); return 0; }
static int framehash_write_packet(struct AVFormatContext *s, AVPacket *pkt) { struct HashContext *c = s->priv_data; char buf[256]; av_hash_init(c->hash); av_hash_update(c->hash, pkt->data, pkt->size); snprintf(buf, sizeof(buf) - 64, "%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, ", pkt->stream_index, pkt->dts, pkt->pts, pkt->duration, pkt->size); hash_finish(s, buf); return 0; }
core::stringc CHashMD5::quickHash(core::stringc str) { hash_start(); hash_append((u8*)str.c_str(), str.size()); u8 digest[16]; hash_finish(digest, 16); c8 retstr[33]; memset(retstr, 0, 33); c8* temp = retstr; for(int i = 0; i < 16; i++) { if((digest[i] & 0xff) > 0xf){ sprintf(temp, "%x", (digest[i] & 0xff)); }else{ sprintf(temp, "0%x", (digest[i] & 0xff)); } temp += 2; } core::stringc ret(retstr); return ret; };
/* --------------------------------------- 文件处理回调 --------------------------------------- */ static bool_t hasher (void_t *param, sSEARCHa *info) { sHASH hash; /* 过滤掉两个生成的文件 */ if (str_cmpA(info->name, "__hash__.old") == 0 || str_cmpA(info->name, "__hash__.txt") == 0) return (TRUE); /* 显示文件名和大小字节数 */ printf("%s (%" CR_FSZ "u Bytes) ", info->name, info->size); /* 根据内存大小选择读取方式 */ timer_set_base(s_profile); if (info->size == 0) { /* 空文件 */ hash_init(&hash); hash_update(&hash, NULL, 0); hash_finish(param, info->size, info->name, &hash); } else if (info->size <= s_total) { sVFILE file; void_t *data; /* 内存映射 */ data = file_mappingA(info->name, &file); if (data == NULL) goto _read_it; hash_init(&hash); hash_update(&hash, data, (leng_t)info->size); hash_finish(param, info->size, info->name, &hash); file_release(&file); } else { fraw_t file; leng_t rest; fsize_t blks; _read_it: /* 分块读取 */ file = file_raw_openA(info->name, CR_FO_RO | CR_FO_SEQ); if (file == NULL) goto _failure; /* 文件很大, 只能分块读取 */ if (s_rdata == NULL) s_rdata = mem_malloc(FREAD_BLOCK); rest = ( leng_t)(info->size % FREAD_BLOCK); blks = (fsize_t)(info->size / FREAD_BLOCK); for (hash_init(&hash); blks != 0; blks--) { if (file_raw_read(s_rdata, FREAD_BLOCK, file) != FREAD_BLOCK) { file_raw_close(file); goto _failure; } hash_update(&hash, s_rdata, FREAD_BLOCK); } if (rest != 0) { if (file_raw_read(s_rdata, rest, file) != rest) { file_raw_close(file); goto _failure; } hash_update(&hash, s_rdata, rest); } hash_finish(param, info->size, info->name, &hash); file_raw_close(file); } fp32_t time; time = timer_get_delta(s_profile); time *= 1.024f; printf(" %.2f KB/S\n", info->size / time); return (TRUE); _failure: printf(" [FAILED]\n"); return (TRUE); }