mirror of
https://github.com/radareorg/radare2.git
synced 2024-10-07 02:23:58 +00:00
Fix tests and minor cleanup/refactoring in main.rahash2
This commit is contained in:
parent
5671945bb9
commit
20725207ed
@ -20,7 +20,7 @@ typedef struct {
|
|||||||
RHashSeed s;
|
RHashSeed s;
|
||||||
} RahashOptions;
|
} RahashOptions;
|
||||||
|
|
||||||
static void compare_hashes(const RHash *ctx, const ut8 *compare, int length, int *ret, int rad) {
|
static void compare_hashes(const RHash *ctx, RahashOptions *ro, const ut8 *compare, int length, int *ret, int rad) {
|
||||||
if (compare) {
|
if (compare) {
|
||||||
// algobit has only 1 bit set
|
// algobit has only 1 bit set
|
||||||
if (!memcmp (ctx->digest, compare, length)) {
|
if (!memcmp (ctx->digest, compare, length)) {
|
||||||
@ -28,7 +28,7 @@ static void compare_hashes(const RHash *ctx, const ut8 *compare, int length, int
|
|||||||
R_LOG_INFO ("Computed hash matches the expected one");
|
R_LOG_INFO ("Computed hash matches the expected one");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (rad != 'q') {
|
if (rad != 'q' || ro->quiet < 2) {
|
||||||
R_LOG_WARN ("Computed hash doesn't match the expected one");
|
R_LOG_WARN ("Computed hash doesn't match the expected one");
|
||||||
}
|
}
|
||||||
*ret = 1;
|
*ret = 1;
|
||||||
@ -67,7 +67,12 @@ static void do_hash_seed(RahashOptions *ro, const char *seed) {
|
|||||||
if (r_str_startswith (sptr, "s:")) {
|
if (r_str_startswith (sptr, "s:")) {
|
||||||
strcpy ((char *) ro->s.buf, sptr + 2);
|
strcpy ((char *) ro->s.buf, sptr + 2);
|
||||||
ro->s.len = strlen (sptr + 2);
|
ro->s.len = strlen (sptr + 2);
|
||||||
} else if (r_str_startswith (sptr, "0x")) {
|
} else if (r_str_startswith (sptr, "+")) {
|
||||||
|
// TODO: honor endian
|
||||||
|
ut32 n = r_num_math (NULL, sptr);
|
||||||
|
r_write_ble32 (ro->s.buf, n, ro->endian);
|
||||||
|
ro->s.len = 4;
|
||||||
|
} else {
|
||||||
ro->s.len = r_hex_str2bin (sptr, ro->s.buf);
|
ro->s.len = r_hex_str2bin (sptr, ro->s.buf);
|
||||||
if (ro->s.len < 1) {
|
if (ro->s.len < 1) {
|
||||||
strcpy ((char *) ro->s.buf, sptr);
|
strcpy ((char *) ro->s.buf, sptr);
|
||||||
@ -75,11 +80,6 @@ static void do_hash_seed(RahashOptions *ro, const char *seed) {
|
|||||||
R_LOG_WARN ("Expected seed/key in hexpair format, use 0x or s: prefix instead")
|
R_LOG_WARN ("Expected seed/key in hexpair format, use 0x or s: prefix instead")
|
||||||
// assuming a string, prefix it with 's:' to skip this message");
|
// assuming a string, prefix it with 's:' to skip this message");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// TODO: honor endian
|
|
||||||
ut32 n = r_num_math (NULL, sptr);
|
|
||||||
r_write_ble32 (ro->s.buf, n, ro->endian);
|
|
||||||
ro->s.len = 4;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +108,8 @@ static void do_hash_hexprint(const ut8 *c, int len, int ule, PJ *pj, int rad) {
|
|||||||
free (buf);
|
free (buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void do_hash_print(RHash *ctx, RahashOptions *ro, ut64 hash, int dlen, PJ *pj, int rad, int ule) {
|
static void do_hash_print(RHash *ctx, RahashOptions *ro, ut64 hash, int dlen, PJ *pj, int rad) {
|
||||||
|
int ule = ro->endian;
|
||||||
char *o;
|
char *o;
|
||||||
const ut8 *c = ctx->digest;
|
const ut8 *c = ctx->digest;
|
||||||
const char *hname = r_hash_name (hash);
|
const char *hname = r_hash_name (hash);
|
||||||
@ -131,7 +132,7 @@ static void do_hash_print(RHash *ctx, RahashOptions *ro, ut64 hash, int dlen, PJ
|
|||||||
do_hash_hexprint (c, dlen, ule, pj, rad);
|
do_hash_hexprint (c, dlen, ule, pj, rad);
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
if (ro->quiet) {
|
if (ro->quiet > 2) {
|
||||||
// print nothing
|
// print nothing
|
||||||
} else {
|
} else {
|
||||||
if (hash & R_HASH_SSDEEP) {
|
if (hash & R_HASH_SSDEEP) {
|
||||||
@ -151,9 +152,10 @@ static void do_hash_print(RHash *ctx, RahashOptions *ro, ut64 hash, int dlen, PJ
|
|||||||
pj_k (pj, hname);
|
pj_k (pj, hname);
|
||||||
do_hash_hexprint (c, dlen, ule, pj, rad);
|
do_hash_hexprint (c, dlen, ule, pj, rad);
|
||||||
break;
|
break;
|
||||||
case 'q':
|
case 'Q':
|
||||||
// nothing to print
|
// nothing to print
|
||||||
break;
|
break;
|
||||||
|
case 'q':
|
||||||
default:
|
default:
|
||||||
o = r_print_randomart (c, dlen, ro->from);
|
o = r_print_randomart (c, dlen, ro->from);
|
||||||
printf ("%s\n%s\n", hname, o);
|
printf ("%s\n%s\n", hname, o);
|
||||||
@ -162,7 +164,7 @@ static void do_hash_print(RHash *ctx, RahashOptions *ro, ut64 hash, int dlen, PJ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_hash_internal(RHash *ctx, RahashOptions *ro, ut64 hash, const ut8 *buf, int len, PJ *pj, int rad, int print, int le) {
|
static int do_hash_internal(RHash *ctx, RahashOptions *ro, ut64 hash, const ut8 *buf, int len, PJ *pj, int rad, int print) {
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -173,7 +175,7 @@ static int do_hash_internal(RHash *ctx, RahashOptions *ro, ut64 hash, const ut8
|
|||||||
if (ro->iterations > 0) {
|
if (ro->iterations > 0) {
|
||||||
r_hash_do_spice (ctx, hash, ro->iterations, ro->_s);
|
r_hash_do_spice (ctx, hash, ro->iterations, ro->_s);
|
||||||
}
|
}
|
||||||
do_hash_print (ctx, ro, hash, dlen, pj, rad, le);
|
do_hash_print (ctx, ro, hash, dlen, pj, rad);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,15 +237,15 @@ static int do_hash(RahashOptions *ro, const char *file, const char *algo, RIO *i
|
|||||||
int dlen = r_hash_size (hashbit);
|
int dlen = r_hash_size (hashbit);
|
||||||
r_hash_do_begin (ctx, i);
|
r_hash_do_begin (ctx, i);
|
||||||
if (ro->s.buf && ro->s.prefix) {
|
if (ro->s.buf && ro->s.prefix) {
|
||||||
do_hash_internal (ctx, ro, hashbit, ro->s.buf, ro->s.len, pj, rad, 0, ule);
|
do_hash_internal (ctx, ro, hashbit, ro->s.buf, ro->s.len, pj, rad, 0);
|
||||||
}
|
}
|
||||||
for (j = ro->from; j < ro->to; j += bsize) {
|
for (j = ro->from; j < ro->to; j += bsize) {
|
||||||
int len = ((j + bsize) > ro->to)? (ro->to - j): bsize;
|
int len = ((j + bsize) > ro->to)? (ro->to - j): bsize;
|
||||||
r_io_pread_at (io, j, buf, len);
|
r_io_pread_at (io, j, buf, len);
|
||||||
do_hash_internal (ctx, ro, hashbit, buf, len, pj, rad, 0, ule);
|
do_hash_internal (ctx, ro, hashbit, buf, len, pj, rad, 0);
|
||||||
}
|
}
|
||||||
if (ro->s.buf && !ro->s.prefix) {
|
if (ro->s.buf && !ro->s.prefix) {
|
||||||
do_hash_internal (ctx, ro, hashbit, ro->s.buf, ro->s.len, pj, rad, 0, ule);
|
do_hash_internal (ctx, ro, hashbit, ro->s.buf, ro->s.len, pj, rad, 0);
|
||||||
}
|
}
|
||||||
r_hash_do_end (ctx, i);
|
r_hash_do_end (ctx, i);
|
||||||
if (ro->iterations > 0) {
|
if (ro->iterations > 0) {
|
||||||
@ -255,9 +257,11 @@ static int do_hash(RahashOptions *ro, const char *file, const char *algo, RIO *i
|
|||||||
if (!ro->quiet && rad != 'j') {
|
if (!ro->quiet && rad != 'j') {
|
||||||
printf ("%s: ", file);
|
printf ("%s: ", file);
|
||||||
}
|
}
|
||||||
do_hash_print (ctx, ro, i, dlen, pj, ro->quiet? 'n': rad, ule);
|
do_hash_print (ctx, ro, i, dlen, pj, ro->quiet? 'n': rad);
|
||||||
if (ro->quiet == 1) {
|
if (ro->quiet == 1) {
|
||||||
printf (" %s\n", file);
|
printf (" %s\n", file);
|
||||||
|
} else if (ro->quiet > 0 && ro->quiet < 3 && !rad) {
|
||||||
|
printf ("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -285,9 +289,9 @@ static int do_hash(RahashOptions *ro, const char *file, const char *algo, RIO *i
|
|||||||
if (ro->to > fsize) {
|
if (ro->to > fsize) {
|
||||||
ro->to = fsize;
|
ro->to = fsize;
|
||||||
}
|
}
|
||||||
do_hash_internal (ctx, ro, hashbit, buf, nsize, pj, rad, 1, ule);
|
do_hash_internal (ctx, ro, hashbit, buf, nsize, pj, rad, 1);
|
||||||
}
|
}
|
||||||
do_hash_internal (ctx, ro, hashbit, NULL, 0, pj, rad, 1, ule);
|
do_hash_internal (ctx, ro, hashbit, NULL, 0, pj, rad, 1);
|
||||||
ro->from = ofrom;
|
ro->from = ofrom;
|
||||||
ro->to = oto;
|
ro->to = oto;
|
||||||
}
|
}
|
||||||
@ -303,7 +307,7 @@ static int do_hash(RahashOptions *ro, const char *file, const char *algo, RIO *i
|
|||||||
if (ro->quiet) {
|
if (ro->quiet) {
|
||||||
mode = 'q';
|
mode = 'q';
|
||||||
}
|
}
|
||||||
compare_hashes (ctx, compare, r_hash_size (algobit), &ret, mode);
|
compare_hashes (ctx, ro, compare, r_hash_size (algobit), &ret, mode);
|
||||||
r_hash_free (ctx);
|
r_hash_free (ctx);
|
||||||
free (buf);
|
free (buf);
|
||||||
return ret;
|
return ret;
|
||||||
@ -726,8 +730,8 @@ R_API int r_main_rahash2(int argc, const char **argv) {
|
|||||||
ctx = r_hash_new (true, hashbit);
|
ctx = r_hash_new (true, hashbit);
|
||||||
ro->from = 0;
|
ro->from = 0;
|
||||||
ro->to = strsz;
|
ro->to = strsz;
|
||||||
do_hash_internal (ctx, ro, hashbit, (const ut8 *) str, strsz, pj, rad, 1, ule);
|
do_hash_internal (ctx, ro, hashbit, (const ut8 *) str, strsz, pj, rad, 1);
|
||||||
compare_hashes (ctx, compareBin, r_hash_size (algobit), &_ret, mode);
|
compare_hashes (ctx, ro, compareBin, r_hash_size (algobit), &_ret, mode);
|
||||||
r_hash_free (ctx);
|
r_hash_free (ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user