diff --git a/libr/hash/md4.c b/libr/hash/md4.c index 5c2067c05f..1bee4b5f25 100644 --- a/libr/hash/md4.c +++ b/libr/hash/md4.c @@ -128,7 +128,7 @@ static void mdfour64(ut32 *M, ut32 *A, ut32 *B, ut32 *C, ut32 *D) { static void copy64(ut32 *M, const ut8 *in) { int i; for (i = 0; i < 16; i++) { - M[i] = (in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) | + M[i] = ((ut32)in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) | (in[i * 4 + 1] << 8) | (in[i * 4 + 0] << 0); } } diff --git a/libr/main/rahash2.c b/libr/main/rahash2.c index 0479762f01..eb9f0ac3f4 100644 --- a/libr/main/rahash2.c +++ b/libr/main/rahash2.c @@ -71,23 +71,30 @@ static void do_hash_seed(const char *seed) { } } -static void do_hash_hexprint(const ut8 *c, int len, int ule, int rad) { +static void do_hash_hexprint(const ut8 *c, int len, int ule, PJ *pj, int rad) { int i; + char *buf = malloc (len * 2 + 1); + if (!buf) { + return; + } if (ule) { - for (i = len - 1; i >= 0; i--) { - printf ("%02x", c[i]); + for (i = 0; i < len; i++) { + snprintf (buf + i * 2, (len - i) * 2 + 1, "%02x", c[len - i - 1]); } } else { for (i = 0; i < len; i++) { - printf ("%02x", c[i]); + snprintf (buf + i * 2, (len - i) * 2 + 1, "%02x", c[i]); } } - if (rad != 'j') { - printf ("\n"); + if (rad == 'j') { + pj_ks (pj, "hash", buf); + } else { + printf ("%s%s", buf, rad == 'n' ? "" : "\n"); } + free (buf); } -static void do_hash_print(RHash *ctx, ut64 hash, int dlen, int rad, int ule) { +static void do_hash_print(RHash *ctx, ut64 hash, int dlen, PJ *pj, int rad, int ule) { char *o; const ut8 *c = ctx->digest; const char *hname = r_hash_name (hash); @@ -100,21 +107,21 @@ static void do_hash_print(RHash *ctx, ut64 hash, int dlen, int rad, int ule) { if (dlen == R_HASH_SIZE_ENTROPY) { printf("%.8f\n", ctx->entropy); } else { - do_hash_hexprint (c, dlen, ule, rad); + do_hash_hexprint (c, dlen, ule, pj, rad); } break; case 1: printf ("CC file %s:", hname); - do_hash_hexprint (c, dlen, ule, rad); + do_hash_hexprint (c, dlen, ule, pj, rad); break; case 'n': - do_hash_hexprint (c, dlen, ule, 'j'); + do_hash_hexprint (c, dlen, ule, pj, rad); break; case 'j': - //TODO PJ - printf ("{\"name\":\"%s\",\"hash\":\"", hname); - do_hash_hexprint (c, dlen, ule, rad); - printf ("\"}"); + pj_o (pj); + pj_ks (pj, "name", hname); + do_hash_hexprint (c, dlen, ule, pj, rad); + pj_end (pj); break; default: o = r_print_randomart (c, dlen, from); @@ -124,7 +131,7 @@ static void do_hash_print(RHash *ctx, ut64 hash, int dlen, int rad, int ule) { } } -static int do_hash_internal(RHash *ctx, ut64 hash, const ut8 *buf, int len, int rad, int print, int le) { +static int do_hash_internal(RHash *ctx, ut64 hash, const ut8 *buf, int len, PJ *pj, int rad, int print, int le) { if (len < 0) { return 0; } @@ -135,7 +142,7 @@ static int do_hash_internal(RHash *ctx, ut64 hash, const ut8 *buf, int len, int if (iterations > 0) { r_hash_do_spice (ctx, hash, iterations, _s); } - do_hash_print (ctx, hash, dlen, rad, le); + do_hash_print (ctx, hash, dlen, pj, rad, le); return 1; } @@ -145,7 +152,6 @@ static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int r ut8 *buf; int ret = 0; ut64 i; - bool first = true; if (algobit == R_HASH_NONE) { eprintf ("rahash2: Invalid hashing algorithm specified\n"); return 1; @@ -176,11 +182,17 @@ static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int r if (!buf) { return 1; } + PJ *pj = NULL; + if (rad == 'j') { + pj = pj_new (); + if (!pj) { + free (buf); + return 1; + } + pj_a (pj); + } ctx = r_hash_new (true, algobit); - if (rad == 'j') { - printf ("["); - } if (incremental) { for (i = 1; i < R_HASH_ALL; i <<= 1) { if (algobit & i) { @@ -188,15 +200,15 @@ static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int r int dlen = r_hash_size (hashbit); r_hash_do_begin (ctx, i); if (s.buf && s.prefix) { - do_hash_internal (ctx, hashbit, s.buf, s.len, rad, 0, ule); + do_hash_internal (ctx, hashbit, s.buf, s.len, pj, rad, 0, ule); } for (j = from; j < to; j += bsize) { int len = ((j + bsize) > to)? (to - j): bsize; r_io_pread_at (io, j, buf, len); - do_hash_internal (ctx, hashbit, buf, len, rad, 0, ule); + do_hash_internal (ctx, hashbit, buf, len, pj, rad, 0, ule); } if (s.buf && !s.prefix) { - do_hash_internal (ctx, hashbit, s.buf, s.len, rad, 0, ule); + do_hash_internal (ctx, hashbit, s.buf, s.len, pj, rad, 0, ule); } r_hash_do_end (ctx, i); if (iterations > 0) { @@ -205,17 +217,10 @@ static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int r if (!*r_hash_name (i)) { continue; } - if (rad == 'j') { - if (first) { - first = false; - } else { - printf (","); - } - } if (!quiet && rad != 'j') { printf ("%s: ", file); } - do_hash_print (ctx, i, dlen, quiet? 'n': rad, ule); + do_hash_print (ctx, i, dlen, pj, quiet? 'n': rad, ule); if (quiet == 1) { printf (" %s\n", file); } else { @@ -249,16 +254,18 @@ static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int r if (to > fsize) { to = fsize; } - do_hash_internal (ctx, hashbit, buf, nsize, rad, 1, ule); + do_hash_internal (ctx, hashbit, buf, nsize, pj, rad, 1, ule); } - do_hash_internal (ctx, hashbit, NULL, 0, rad, 1, ule); + do_hash_internal (ctx, hashbit, NULL, 0, pj, rad, 1, ule); from = ofrom; to = oto; } } } if (rad == 'j') { - printf ("]\n"); + pj_end (pj); + printf ("%s\n", pj_string (pj)); + pj_free (pj); } compare_hashes (ctx, compare, r_hash_size (algobit), &ret); @@ -268,7 +275,7 @@ static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int r } static int do_help(int line) { - printf ("Usage: rahash2 [-rBhLkv] [-b S] [-a A] [-c H] [-E A] [-s S] [-f O] [-t O] [file] ...\n"); + printf ("Usage: rahash2 [-BhjkLqrv] [-b S] [-a A] [-c H] [-E A] [-s S] [-f O] [-t O] [file] ...\n"); if (line) { return 0; } @@ -283,6 +290,7 @@ static int do_help(int line) { " -f from start hashing at given address\n" " -i num repeat hash N iterations\n" " -I iv use give initialization vector (IV) (hexa or s:string)\n" + " -j output in json\n" " -S seed use given seed (hexa or s:string) use ^ to prefix (key for -E)\n" " (- will slurp the key from stdin, the @ prefix points to a file\n" " -k show hash using the openssh's randomkey algorithm\n" @@ -641,17 +649,30 @@ R_API int r_main_rahash2(int argc, const char **argv) { free (iv); return 1; } + PJ *pj = NULL; + if (rad == 'j') { + pj = pj_new (); + if (!pj) { + return 1; + } + pj_a (pj); + } for (i = 1; i < R_HASH_ALL; i <<= 1) { if (algobit & i) { ut64 hashbit = i & algobit; ctx = r_hash_new (true, hashbit); from = 0; to = strsz; - do_hash_internal (ctx, hashbit, (const ut8 *) str, strsz, rad, 1, ule); + do_hash_internal (ctx, hashbit, (const ut8 *) str, strsz, pj, rad, 1, ule); compare_hashes (ctx, compareBin, r_hash_size (algobit), &ret); r_hash_free (ctx); } } + if (rad == 'j') { + pj_end (pj); + printf ("%s\n", pj_string (pj)); + pj_free (pj); + } if (_s) { if (str != hashstr) { free (str); @@ -712,6 +733,7 @@ R_API int r_main_rahash2(int argc, const char **argv) { r_io_pwrite_at (io, 0, buf, sz); } free (uri); + free (buf); } else { if (r_file_is_directory (argv[i])) { eprintf ("rahash2: Cannot hash directories\n"); diff --git a/test/db/tools/rahash2 b/test/db/tools/rahash2 index a2c257d87d..9ad4fbb8bf 100644 --- a/test/db/tools/rahash2 +++ b/test/db/tools/rahash2 @@ -70,7 +70,7 @@ NAME=rahash2 -h FILE=- CMDS=!rahash2~Usage EXPECT=<