Qt: Add file hashes to capture file properties dialog

Like capinfos provide file hashes in the capture file properties dialog.

Change-Id: Ia9f1b05f61abd239d81b7061bbba1e53c01f28be
Signed-off-by: Jaap Keuter <jaap.keuter@xs4all.nl>
Reviewed-on: https://code.wireshark.org/review/30524
Tested-by: Petri Dish Buildbot
Reviewed-by: Stig Bjørlykke <stig@bjorlykke.org>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Jaap Keuter 2018-11-06 22:34:09 +01:00 committed by Anders Broman
parent 163e4637d5
commit c29c652eb9
3 changed files with 69 additions and 1 deletions

View File

@ -161,6 +161,21 @@ QString CaptureFilePropertiesDialog::summaryToHtml()
<< table_data_tmpl.arg(file_size_to_qstring(summary.file_length))
<< table_row_end;
out << table_row_begin
<< table_vheader_tmpl.arg(tr("Hash (SHA256)"))
<< table_data_tmpl.arg(summary.file_sha256)
<< table_row_end;
out << table_row_begin
<< table_vheader_tmpl.arg(tr("Hash (RIPEMD160)"))
<< table_data_tmpl.arg(summary.file_rmd160)
<< table_row_end;
out << table_row_begin
<< table_vheader_tmpl.arg(tr("Hash (SHA1)"))
<< table_data_tmpl.arg(summary.file_sha1)
<< table_row_end;
QString format_str = wtap_file_type_subtype_string(summary.file_type);
if (summary.iscompressed) {
format_str.append(tr(" (gzip compressed)"));

View File

@ -15,9 +15,18 @@
#include <wiretap/pcapng.h>
#include <epan/packet.h>
#include <wsutil/file_util.h>
#include <wsutil/wsgcrypt.h>
#include "cfile.h"
#include "ui/summary.h"
// Strongest to weakest
#define HASH_SIZE_SHA256 32
#define HASH_SIZE_RMD160 20
#define HASH_SIZE_SHA1 20
#define HASH_BUF_SIZE (1024 * 1024)
static void
tally_frame_data(frame_data *cur_frame, summary_tally *sum_tally)
{
@ -86,6 +95,15 @@ tally_frame_data(frame_data *cur_frame, summary_tally *sum_tally)
}
}
static void
hash_to_str(const unsigned char *hash, size_t length, char *str) {
int i;
for (i = 0; i < (int) length; i++) {
g_snprintf(str+(i*2), 3, "%02x", hash[i]);
}
}
void
summary_fill_in(capture_file *cf, summary_tally *st)
{
@ -101,6 +119,11 @@ summary_fill_in(capture_file *cf, summary_tally *st)
char* if_string;
wtapng_if_descr_filter_t* if_filter;
FILE *fh;
char *hash_buf;
gcry_md_hd_t hd;
size_t hash_bytes;
st->packet_count_ts = 0;
st->start_time = 0;
st->stop_time = 0;
@ -184,6 +207,31 @@ summary_fill_in(capture_file *cf, summary_tally *st)
g_array_append_val(st->ifaces, iface);
}
g_free(idb_info);
g_strlcpy(st->file_sha256, "<unknown>", HASH_STR_SIZE);
g_strlcpy(st->file_rmd160, "<unknown>", HASH_STR_SIZE);
g_strlcpy(st->file_sha1, "<unknown>", HASH_STR_SIZE);
gcry_md_open(&hd, GCRY_MD_SHA256, 0);
if (hd) {
gcry_md_enable(hd, GCRY_MD_RMD160);
gcry_md_enable(hd, GCRY_MD_SHA1);
}
hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
fh = ws_fopen(cf->filename, "rb");
if (fh && hash_buf && hd) {
while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
gcry_md_write(hd, hash_buf, hash_bytes);
}
gcry_md_final(hd);
hash_to_str(gcry_md_read(hd, GCRY_MD_SHA256), HASH_SIZE_SHA256, st->file_sha256);
hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, st->file_rmd160);
hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, st->file_sha1);
}
if (fh) fclose(fh);
g_free(hash_buf);
gcry_md_close(hd);
}
#ifdef HAVE_LIBPCAP

View File

@ -30,6 +30,8 @@ typedef struct iface_summary_info_tag {
int encap_type; /**< wiretap encapsulation type */
} iface_summary_info;
#define HASH_STR_SIZE (65) /* Max hash size * 2 + '\0' */
typedef struct _summary_tally {
guint64 bytes; /**< total bytes */
double start_time; /**< seconds, with msec resolution */
@ -50,8 +52,11 @@ typedef struct _summary_tally {
guint64 filtered_bytes; /**< total bytes in the filtered packets */
double filtered_start; /**< time in seconds, with msec resolution */
double filtered_stop; /**< time in seconds, with msec resolution */
const char *filename;
const char *filename; /**< path of capture file */
gint64 file_length; /**< file length in bytes */
gchar file_sha256[HASH_STR_SIZE]; /**< SHA256 hash of capture file */
gchar file_rmd160[HASH_STR_SIZE]; /**< RIPEMD160 hash of capture file */
gchar file_sha1[HASH_STR_SIZE]; /**< SHA1 hash of capture file */
int file_type; /**< wiretap file type */
int iscompressed; /**< TRUE if file is compressed */
int file_encap_type; /**< wiretap encapsulation type for file */