Fix #5134 - rahash2 -K entropy was randomized by uninitalized values

This commit is contained in:
pancake 2016-06-14 20:29:31 +02:00
parent a772452d27
commit 2582e5fd0e
3 changed files with 14 additions and 6 deletions

View File

@ -68,6 +68,7 @@ R_API int r_hash_calculate(RHash *ctx, ut64 algobit, const ut8 *buf, int len) {
return R_HASH_SIZE_PARITY;
}
if (algobit & R_HASH_ENTROPY) {
memset (ctx->digest, 0, R_HASH_SIZE_ENTROPY);
*ctx->digest = (ut8)r_hash_entropy (buf, len);
return R_HASH_SIZE_ENTROPY;
}

View File

@ -11,9 +11,11 @@
static double get_px(ut8 x, const ut8 *data, ut64 size) {
ut64 i, count = 0;
for (i = 0; i < size; i++)
if (data[i] == x)
for (i = 0; i < size; i++) {
if (data[i] == x) {
count++;
}
}
return (double) count / size;
}
@ -30,9 +32,13 @@ R_API double r_hash_entropy(const ut8 *data, ut64 size) {
R_API double r_hash_entropy_fraction(const ut8 *data, ut64 size) {
double h = r_hash_entropy (data, size);
if (size < 256)
return h * log (2.0) / log (size);
return h/8; //(size/256);//8;
if (size < 256) {
double base = log (size);
return base
? h * log (2.0) / base
: 0;
}
return h / 8;
}
// 0-8

View File

@ -188,8 +188,9 @@ R_API char *r_hash_to_string(RHash *ctx, const char *name, const ut8 *data, int
digest_hex = NULL;
} else {
digest_hex = malloc ((digest_size * 2) + 1);
for (i = 0; i < digest_size; i++)
for (i = 0; i < digest_size; i++) {
sprintf (digest_hex + (i * 2), "%02x", ctx->digest[i]);
}
digest_hex[digest_size * 2] = 0;
}
}