Skip to content

Commit 4751998

Browse files
committed
Fix #13937: add rahash2 -R radare2 sdb output
Keep -r output unchanged for backward compatibility (existing tests/scripts expect the current comment-like output). Add -R to emit r2 sdb commands (k file.<algo>=...) so hashes can be stored for any algorithm without relying on evars like file.sha1. Update manpage and add r2r tests.
1 parent e537eec commit 4751998

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

libr/main/rahash2.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ static void do_hash_print(RHash *ctx, RahashOptions *ro, ut64 hash, int dlen, PJ
118118
case 0:
119119
if (!ro->quiet) {
120120
printf ("0x%08" PFMT64x "-0x%08" PFMT64x " %s: ",
121-
ro->from, ro->to > 0? ro->to - 1: 0, hname);
121+
ro->from,
122+
ro->to > 0? ro->to - 1: 0,
123+
hname);
122124
}
123125
if (hash & R_HASH_SSDEEP) {
124126
printf ("%s\n", ctx->digest);
@@ -132,6 +134,16 @@ static void do_hash_print(RHash *ctx, RahashOptions *ro, ut64 hash, int dlen, PJ
132134
printf ("CC file %s:", hname);
133135
do_hash_hexprint (c, dlen, ule, pj, rad);
134136
break;
137+
case 3:
138+
printf ("k file.%s=", hname);
139+
if (hash & R_HASH_SSDEEP) {
140+
printf ("%s\n", ctx->digest);
141+
} else if (dlen == R_HASH_SIZE_ENTROPY) {
142+
printf ("%.8f\n", ctx->entropy);
143+
} else {
144+
do_hash_hexprint (c, dlen, ule, pj, 0);
145+
}
146+
break;
135147
case 'n':
136148
if (ro->quiet > 2) {
137149
// print nothing
@@ -318,7 +330,7 @@ static int do_hash(RahashOptions *ro, const char *file, const char *algo, RIO *i
318330
}
319331

320332
static int do_help(int line) {
321-
printf ("Usage: rahash2 [-BehjkLqrvX] [-b S] [-a A] [-c H] [-E A] [-s S] [-f O] [-t O] [file] ...\n");
333+
printf ("Usage: rahash2 [-BehjkLqRrvX] [-b S] [-a A] [-c H] [-E A] [-s S] [-f O] [-t O] [file] ...\n");
322334
if (line) {
323335
return 0;
324336
}
@@ -341,6 +353,7 @@ static int do_help(int line) {
341353
" -q run in quiet mode (-qq to show only the hash)\n"
342354
" -L list muta plugins (combines with -q, used by -a, -E and -D)\n"
343355
" -r output radare commands\n"
356+
" -R output radare2 sdb commands (k file.<algo>=...)\n"
344357
" -s string hash this string instead of files\n"
345358
" -t to stop hashing at given address\n"
346359
" -x hexstr hash this hexpair string instead of files\n"
@@ -562,7 +575,7 @@ R_API int r_main_rahash2(int argc, const char **argv) {
562575
goto beach; \
563576
}
564577
RGetopt opt;
565-
r_getopt_init (&opt, argc, argv, "p:jJD:rveE:a:i:I:S:s:x:b:nBhf:t:kLqc:X");
578+
r_getopt_init (&opt, argc, argv, "p:jJD:RrveE:a:i:I:S:s:x:b:nBhf:t:kLqc:X");
566579
while ((c = r_getopt_next (&opt)) != -1) {
567580
switch (c) {
568581
case 'q':
@@ -603,6 +616,7 @@ R_API int r_main_rahash2(int argc, const char **argv) {
603616
ro->endian = !ro->endian;
604617
break;
605618
case 'r': rad = 1; break;
619+
case 'R': rad = 3; break;
606620
case 'k': rad = 2; break;
607621
case 'p': ptype = opt.arg; break;
608622
case 'a': add_algo (algos, opt.arg); break;

man/rahash2.1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
.Nd Block-Based Hashing, Encoding, and Encryption Utility
66
.Sh SYNOPSIS
77
.Nm rahash2
8-
.Op Fl BehjkLqrvX
8+
.Op Fl BehjkLqRrvX
99
.Op Fl b Ar S
1010
.Op Fl a Ar A
1111
.Op Fl c Ar H
@@ -60,7 +60,16 @@ Run in quiet mode (-qq to show only the hash)
6060
.It Fl L
6161
List muta plugins (combines with -q, used by -a, -E and -D)
6262
.It Fl r
63-
Output radare commands
63+
.No Output radare commands (comments), for example:
64+
.Bd -literal -offset indent
65+
$ rahash2 -a sha1 -r file | r2 -qni file
66+
.Ed
67+
.It Fl R
68+
.No Output radare2 sdb commands to store hashes under
69+
.No file.<algo> keys (works with any algorithm name), for example:
70+
.Bd -literal -offset indent
71+
$ rahash2 -a sha1 -R file | r2 -qni file
72+
.Ed
6473
.It Fl s Ar string
6574
Hash this string instead of files
6675
.It Fl t Ar to
@@ -132,4 +141,4 @@ When using the -c flag, an exit status of 0 indicates a match between the expect
132141
.Xr radare2(1)
133142
.Sh AUTHORS
134143
.Pp
135-
pancake <pancake@nopcode.org>
144+
pancake <pancake@nopcode.org>

test/db/tools/rahash2

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,21 @@ bins/elf/analysis/hello-linux-x86_64: CC file sha256:7bdbf25324af1946ec0b16dbf92
7474
EOF
7575
RUN
7676

77+
NAME=rahash2 -a md5,sha1,sha256 -R
78+
FILE=-
79+
CMDS=!rahash2 -a md5,sha1,sha256 -R bins/elf/analysis/hello-linux-x86_64
80+
EXPECT=<<EOF
81+
bins/elf/analysis/hello-linux-x86_64: k file.md5=c957bd5bd6204470256bc15248ccafd4
82+
bins/elf/analysis/hello-linux-x86_64: k file.sha1=687c82d13cb27f0600d8e57edc784282c1732f56
83+
bins/elf/analysis/hello-linux-x86_64: k file.sha256=7bdbf25324af1946ec0b16dbf928875a588a786f7c279cd115729c5a3a297a55
84+
EOF
85+
RUN
86+
7787
NAME=rahash2 -h
7888
FILE=-
7989
CMDS=!rahash2~Usage
8090
EXPECT=<<EOF
81-
Usage: rahash2 [-BehjkLqrvX] [-b S] [-a A] [-c H] [-E A] [-s S] [-f O] [-t O] [file] ...
91+
Usage: rahash2 [-BehjkLqRrvX] [-b S] [-a A] [-c H] [-E A] [-s S] [-f O] [-t O] [file] ...
8292
EOF
8393
RUN
8494

0 commit comments

Comments
 (0)