md5 utility (data + resource fork)

This commit is contained in:
Kelvin Sherlock 2016-07-24 14:50:34 -04:00
parent aca8500d9c
commit dac5493289
6 changed files with 325 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.DS_Store
*.o

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libtomcrypt"]
path = libtomcrypt
url = https://github.com/libtom/libtomcrypt.git

1
libtomcrypt Submodule

@ -0,0 +1 @@
Subproject commit 6ad52252688bb34f90b5e79da4830a927e87b81f

View File

@ -90,6 +90,16 @@ Parameters: Parameters.c.o
Echo: Echo.c.o
$(MPW) Link $(LDFLAGS) -o $@ $^ $(LIBS)
md5 : md5.c.o libtomcrypt/src/hashes/md5.c.o
$(MPW) Link $(LDFLAGS) -o $@ $^ $(LIBS)
md5.c.o : md5.c
$(MPW) SC -P -I . -I libtomcrypt/src/headers $< -o $@
libtomcrypt/src/hashes/md5.c.o : libtomcrypt/src/hashes/md5.c
$(MPW) SC -P -I . -I libtomcrypt/src/headers -d LTC_MD5 $< -o $@
#SetFile.c : SetFile.rl
# ragel -G2 -p -m -o $@ $<

175
md5.c Normal file
View File

@ -0,0 +1,175 @@
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#define LTC_MD5
#include "tomcrypt.h"
const char *unhash(const unsigned char *hash) {
static char buffer[33];
static char table[] = "0123456789abcdef";
unsigned i, j;
for (i = 0, j = 0; i < 16; ++i) {
unsigned char c = hash[i];
buffer[j++] = table[c >> 4];
buffer[j++] = table[c & 0x0f];
}
buffer[j] = 0;
return buffer;
}
int process(int fd, const char *name, int fork) {
static unsigned char buffer[4096];
hash_state md;
unsigned char hash[16];
md5_init(&md);
for(;;) {
long l = read(fd, buffer, sizeof(buffer));
if (l == 0) break;
if (l < 0) {
if (errno == EINTR) continue;
perror("read");
return -1;
}
md5_process(&md, buffer, l);
}
md5_done(&md, hash);
fprintf(stdout, "MD5 (%s%s) = %s\n",
name, fork ? "+" : "", unhash(hash));
return 0;
}
// from md5_test
int test() {
static const struct {
char *msg;
unsigned char hash[16];
} tests[] = {
{ "",
{ 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
{ "a",
{0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } },
{ "abc",
{ 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
{ "message digest",
{ 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
{ "abcdefghijklmnopqrstuvwxyz",
{ 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } },
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
{ 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } },
{ "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
{ 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },
{ NULL, { 0 } }
};
unsigned i;
unsigned ok;
hash_state md;
unsigned char hash[16];
for (i = 0; tests[i].msg != NULL; i++) {
md5_init(&md);
md5_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg));
md5_done(&md, hash);
ok = !memcmp(hash, tests[i].hash, 16);
printf("MD5 (\"%s\") = %s - %s\n",
tests[i].msg, unhash(hash),
ok ? "verified correct" : "INCORRECT RESULT!");
if (!ok) return -1;
}
return 0;
}
void help() {
fputs("Usage: MD5 [-x] files...\n", stdout);
}
int main(int argc, char **argv) {
int rv;
int i;
for (i = 1; i < argc; ++i) {
char *cp = argv[i];
if (*cp != '-') {
break;
}
if (strcmp(cp, "--")) {
++i;
break;
}
if (!strcmp(cp, "-h")) {
help();
exit(0);
}
if (!strcmp(cp, "-x")) {
return test();
}
}
argv += i;
argc -= i;
if (argc == 0) {
help();
return 1;
}
rv = 0;
for ( i = 0; i < argc; ++i) {
int fd;
int tmp;
char *cp = argv[i];
fd = open(cp, O_RDONLY | O_BINARY);
if (fd < 0) {
perror(cp);
rv = 1;
continue;
}
tmp = process(fd, cp, 0);
if (tmp) rv = 1;
close(fd);
// flag for resource fork...
fd = open(cp, O_RDONLY | O_BINARY | O_RSRC);
if (fd < 0) {
perror(cp);
rv = 1;
continue;
}
tmp = process(fd, cp, 1);
if (tmp) rv = 1;
close(fd);
}
return rv;
}

134
tomcrypt.h Normal file
View File

@ -0,0 +1,134 @@
#ifndef TOMCRYPT_H_
#define TOMCRYPT_H_
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <time.h>
#include <ctype.h>
#include <limits.h>
/* use configuration data */
//#include <tomcrypt_custom.h>
#ifdef __cplusplus
extern "C" {
#endif
/* version */
#define CRYPT 0x0117
#define SCRYPT "1.17"
/* max size of either a cipher/hash block or symmetric key [largest of the two] */
#define MAXBLOCKSIZE 128
/* descriptor table size */
#define TAB_SIZE 32
/* error codes [will be expanded in future releases] */
enum {
CRYPT_OK=0, /* Result OK */
CRYPT_ERROR, /* Generic Error */
CRYPT_NOP, /* Not a failure but no operation was performed */
CRYPT_INVALID_KEYSIZE, /* Invalid key size given */
CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */
CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */
CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */
CRYPT_INVALID_PACKET, /* Invalid input packet given */
CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */
CRYPT_INVALID_CIPHER, /* Invalid cipher specified */
CRYPT_INVALID_HASH, /* Invalid hash specified */
CRYPT_INVALID_PRNG, /* Invalid PRNG specified */
CRYPT_MEM, /* Out of memory */
CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
CRYPT_INVALID_ARG, /* Generic invalid argument */
CRYPT_FILE_NOTFOUND, /* File Not Found */
CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */
CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */
CRYPT_PK_DUP, /* Duplicate key already in key ring */
CRYPT_PK_NOT_FOUND, /* Key not found in keyring */
CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */
CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */
CRYPT_PK_INVALID_PADDING, /* Invalid padding on input */
CRYPT_HASH_OVERFLOW /* Hash applied to too many bits */
};
typedef unsigned long ulong64; // only used for length field, so ok.
typedef unsigned long ulong32;
#define LTC_MUTEX_PROTO(x)
#define STORE64L(x, y) \
do { (y)[7] = 0; (y)[6] = 0; \
(y)[5] = 0; (y)[4] = 0; \
(y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
#define LOAD32L(x, y) \
do { x = ((ulong32)((y)[3] & 255)<<24) | \
((ulong32)((y)[2] & 255)<<16) | \
((ulong32)((y)[1] & 255)<<8) | \
((ulong32)((y)[0] & 255)); } while(0)
#define STORE32L(x, y) \
do { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
#define XMEMCPY memcpy
#ifndef MAX
#define MAX(x, y) ( ((x)>(y))?(x):(y) )
#endif
#ifndef MIN
#define MIN(x, y) ( ((x)<(y))?(x):(y) )
#endif
#define ROL(x, y) ( (((ulong32)(x)<<(ulong32)((y)&31)) | (((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)(32-((y)&31)))) & 0xFFFFFFFFUL)
#define ROR(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)(32-((y)&31)))) & 0xFFFFFFFFUL)
#define ROLc(x, y) ( (((ulong32)(x)<<(ulong32)((y)&31)) | (((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)(32-((y)&31)))) & 0xFFFFFFFFUL)
#define RORc(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)(32-((y)&31)))) & 0xFFFFFFFFUL)
/* fatal type of error */
#define LTC_ARGCHK(x) assert((x))
#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
//#include <tomcrypt_cfg.h>
//#include <tomcrypt_macros.h>
//#include <tomcrypt_cipher.h>
#include <tomcrypt_hash.h>
//#include <tomcrypt_mac.h>
//#include <tomcrypt_prng.h>
//#include <tomcrypt_pk.h>
//#include <tomcrypt_math.h>
//#include <tomcrypt_misc.h>
//#include <tomcrypt_argchk.h>
//#include <tomcrypt_pkcs.h>
#ifdef __cplusplus
}
#endif
#endif /* TOMCRYPT_H_ */
/* $Source$ */
/* $Revision$ */
/* $Date$ */