mirror of
https://github.com/radareorg/radare2.git
synced 2025-02-03 20:22:38 +00:00
Use pj in rahash2 ##refactor
This commit is contained in:
parent
bfb794858e
commit
4c58f45500
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -70,7 +70,7 @@ NAME=rahash2 -h
|
||||
FILE=-
|
||||
CMDS=!rahash2~Usage
|
||||
EXPECT=<<EOF
|
||||
Usage: rahash2 [-rBhLkv] [-b S] [-a A] [-c H] [-E A] [-s S] [-f O] [-t O] [file] ...
|
||||
Usage: rahash2 [-BhjkLqrv] [-b S] [-a A] [-c H] [-E A] [-s S] [-f O] [-t O] [file] ...
|
||||
EOF
|
||||
RUN
|
||||
|
||||
@ -998,3 +998,19 @@ EXPECT_ERR=<<EOF
|
||||
Cannot open empty path
|
||||
EOF
|
||||
RUN
|
||||
|
||||
NAME=rahash2 -j file
|
||||
FILE=-
|
||||
CMDS=!rahash2 -j -a md4,md5 bins/elf/analysis/hello-linux-x86_64
|
||||
EXPECT=<<EOF
|
||||
[{"name":"md5","hash":"c957bd5bd6204470256bc15248ccafd4"},{"name":"md4","hash":"d4d61472a12d59be3a85e07ff9065658"}]
|
||||
EOF
|
||||
RUN
|
||||
|
||||
NAME=rahash2 -j string
|
||||
FILE=-
|
||||
CMDS=!rahash2 -j -a sha1,sha256 -s 233
|
||||
EXPECT=<<EOF
|
||||
[{"name":"sha1","hash":"52fdb9f68c503e11d168fe52035901864c0a4861"},{"name":"sha256","hash":"c0509a487a18b003ba05e505419ebb63e57a29158073e381f57160b5c5b86426"}]
|
||||
EOF
|
||||
RUN
|
||||
|
Loading…
x
Reference in New Issue
Block a user