Skip to content

Commit

Permalink
Add SHA-512 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjko committed Jan 13, 2023
1 parent c19c1b8 commit db2b4fc
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 18 deletions.
10 changes: 7 additions & 3 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ mandir = $(prefix)/share/man
CC = @CC@
XCPPFLAGS = @CPPFLAGS@
CFLAGS = @CFLAGS@ $(XCPPFLAGS) $(DEFS)
#CFLAGS += -Wall -Wformat -Werror=format-security
#CFLAGS += -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -fsanitize=address,undefined
ifeq ($(CC),gcc)
CFLAGS += -Wall -Wformat -Werror=format-security
#CFLAGS += -fno-omit-frame-pointer -D_FORTIFY_SOURCE=2
#CFLAGS += -fstack-protector --param=ssp-buffer-size=4 -fsanitize=address,undefined
endif
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@
STRIP = strip
Expand All @@ -52,7 +55,8 @@ INSTALL_PROGRAM = @INSTALL_PROGRAM@
DIRNAME = $(shell basename `pwd`)
DISTNAME = $(PKGNAME)-$(Version)

OBJS = $(PKGNAME).o misc.o @GNUGETOPT@ md5.o sha256/hash.o sha256/blocks.o
OBJS = $(PKGNAME).o misc.o @GNUGETOPT@ md5.o sha256/hash.o sha256/blocks.o \
sha512/hash.o sha512/blocks.o

$(PKGNAME): $(OBJS)
$(CC) $(CFLAGS) -o $(PKGNAME) $(OBJS) $(LDFLAGS) $(LIBS)
Expand Down
5 changes: 3 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ HISTORY
support for JSON output (--json),
add support for reading input file from stdin (--stdin),
add support for displaying column names (--header),
add SHA-512 sigest support (--sha512),
other minor fixes.
v1.6.2 - add SHA256 digest support (--sha256 or -2),
v1.6.2 - add SHA-256 digest support (--sha256 or -2),
improved Win32 support thanks to tumagonx,
other minor fixes.
v1.6.1 - support for new libjpeg v7 thanks to Guido Vollbeding,
Expand Down Expand Up @@ -77,7 +78,7 @@ ACKNOWLEDGEMENT

- MD5 message-digest allgorithm used is public domain implementation
written by Colin Plumb.
- SHA-256 message-digest algorithm used is public domain implementation
- SHA-2 message-digest algorithm used is public domain implementation
by D. J. Bernstein (from NaCl library).


Expand Down
54 changes: 41 additions & 13 deletions jpeginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <jerror.h>

#include "sha256/crypto_hash_sha256.h"
#include "sha512/crypto_hash_sha512.h"
#include "md5.h"
#include "jpeginfo.h"

Expand Down Expand Up @@ -97,6 +98,7 @@ int longinfo_mode = 0;
int input_from_file = 0;
int md5_mode = 0;
int sha256_mode = 0;
int sha512_mode = 0;
int stdin_mode = 0;
int csv_mode = 0;
int json_mode = 0;
Expand All @@ -116,6 +118,7 @@ static struct option long_options[] = {
{"info",0,0,'i'},
{"md5",0,0,'5'},
{"sha256",0,0,'2'},
{"sha512",0,&sha512_mode,1},
{"version",0,0,'V'},
{"comments",0,0,'C'},
{"csv",0,0,'s'},
Expand Down Expand Up @@ -188,6 +191,7 @@ void print_usage(void)
fprintf(stderr,
"Usage: jpeginfo [options] <filenames>\n\n"
" -2, --sha256 Calculate SHA-256 checksum for each file.\n"
" --sha512 Calculate SHA-512 checksum for each file.\n"
" -5, --md5 Calculate MD5 checksum for each file.\n"
" -c, --check Check files also for errors.\n"
" -C, --comments Display comments (from COM markers)\n"
Expand Down Expand Up @@ -487,30 +491,51 @@ void print_jpeg_info(struct jpeg_info *info)

if ((header_mode || json_mode) && !header_printed) {
if (csv_mode) {
printf("filename,size,hash,width,height,color_depth,type,progressive_normal,extra_info,comments,status,status_detail\n");
printf("filename,size,");
if (md5_mode)
printf("md5");
else if (sha256_mode)
printf("sha256");
else
printf("sha512");
printf("_hash,width,height,color_depth,type,progressive_normal,extra_info,comments,status,status_detail\n");
}
else if (json_mode) {
printf("[\n");
}
else if (list_mode) {
printf(" W x H Color Type P ");
if (longinfo_mode) printf("ExtraInfo ");
if (longinfo_mode)
printf("ExtraInfo ");
printf(" Size ");
if (md5_mode) printf("MD5 ");
if (sha256_mode) printf("SHA-256 ");
if (com_mode) printf("Comments ");
if (md5_mode)
printf("MD5 ");
if (sha256_mode)
printf("SHA-256 ");
if (sha512_mode)
printf("SHA-512 "
" ");
if (com_mode)
printf("Comments ");
printf("Filename ");
if (check_mode)
printf("Status Details");
printf("\n");
}
else {
printf("Filename W x H Color Type P ");
if (longinfo_mode) printf("ExtraInfo ");
if (longinfo_mode)
printf("ExtraInfo ");
printf(" Size ");
if (md5_mode) printf("MD5 ");
if (sha256_mode) printf("SHA-256 ");
if (com_mode) printf("Comments ");
if (md5_mode)
printf("MD5 ");
if (sha256_mode)
printf("SHA-256 ");
if (sha512_mode)
printf("SHA-512 "
" ");
if (com_mode)
printf("Comments ");
if (check_mode)
printf("Status Details");
printf("\n");
Expand Down Expand Up @@ -708,18 +733,21 @@ int main(int argc, char **argv)
}

/* Calculate hash (message-digest) of the input file */
if (md5_mode || sha256_mode) {
unsigned char digest[32];
char digest_text[65];
if (md5_mode || sha256_mode || sha512_mode) {
unsigned char digest[64];
char digest_text[128 + 1];

if (md5_mode) {
MD5Init(MD5);
MD5Update(MD5, inbuf, file_size);
MD5Final(digest, MD5);
digest2str(digest, digest_text, 16);
} else {
} else if (sha256_mode) {
crypto_hash_sha256(digest, inbuf, file_size);
digest2str(digest, digest_text, 32);
} else {
crypto_hash_sha512(digest, inbuf, file_size);
digest2str(digest, digest_text, 64);
}
info->digest = strdup(digest_text);
}
Expand Down
Loading

0 comments on commit db2b4fc

Please sign in to comment.