libtomcrypt/demos/hashsum.c

121 lines
2.9 KiB
C
Raw Normal View History

2003-03-03 00:59:24 +00:00
/*
* Written by Daniel Richards <kyhwana@world-net.co.nz> 6/7/2002
* hash.c: This app uses libtomcrypt to hash either stdin or a file
* This file is Public Domain. No rights are reserved.
* Compile with 'gcc hashsum.c -o hashsum -ltomcrypt'
* This example isn't really big enough to warrent splitting into
* more functions ;)
*/
2004-12-30 23:55:53 +00:00
#include <tomcrypt.h>
2003-03-03 00:59:24 +00:00
int errno;
2016-01-23 17:59:44 +00:00
void register_algs(void);
2003-03-03 00:59:24 +00:00
int main(int argc, char **argv)
{
int idx, x, z;
unsigned long w;
unsigned char hash_buffer[MAXBLOCKSIZE];
hash_state md;
/* You need to register algorithms before using them */
register_algs();
if (argc < 2) {
printf("usage: ./hash algorithm file [file ...]\n");
printf("Algorithms:\n");
for (x = 0; hash_descriptor[x].name != NULL; x++) {
2004-10-30 03:00:26 +00:00
printf(" %s (%d)\n", hash_descriptor[x].name, hash_descriptor[x].ID);
2003-03-03 00:59:24 +00:00
}
exit(EXIT_SUCCESS);
}
idx = find_hash(argv[1]);
if (idx == -1) {
fprintf(stderr, "\nInvalid hash specified on command line.\n");
return -1;
}
if (argc == 2) {
hash_descriptor[idx].init(&md);
do {
x = fread(hash_buffer, 1, sizeof(hash_buffer), stdin);
hash_descriptor[idx].process(&md, hash_buffer, x);
} while (x == sizeof(hash_buffer));
hash_descriptor[idx].done(&md, hash_buffer);
for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) {
printf("%02x",hash_buffer[x]);
}
printf(" (stdin)\n");
} else {
for (z = 2; z < argc; z++) {
w = sizeof(hash_buffer);
if ((errno = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) {
printf("File hash error: %s\n", error_to_string(errno));
} else {
for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) {
printf("%02x",hash_buffer[x]);
}
printf(" %s\n", argv[z]);
}
}
}
return EXIT_SUCCESS;
}
2003-09-26 01:16:18 +00:00
void register_algs(void)
2003-03-03 00:59:24 +00:00
{
2004-10-30 03:00:26 +00:00
int err;
LTC_UNUSED_PARAM(err);
2004-10-30 03:00:26 +00:00
2007-07-20 17:48:02 +00:00
#ifdef LTC_TIGER
2004-02-20 20:03:32 +00:00
register_hash (&tiger_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_MD2
2004-02-20 20:03:32 +00:00
register_hash (&md2_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_MD4
2004-02-20 20:03:32 +00:00
register_hash (&md4_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_MD5
2004-02-20 20:03:32 +00:00
register_hash (&md5_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_SHA1
2004-02-20 20:03:32 +00:00
register_hash (&sha1_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_SHA224
2004-02-20 20:03:32 +00:00
register_hash (&sha224_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_SHA256
2004-02-20 20:03:32 +00:00
register_hash (&sha256_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_SHA384
2004-02-20 20:03:32 +00:00
register_hash (&sha384_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_SHA512
2004-02-20 20:03:32 +00:00
register_hash (&sha512_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_RIPEMD128
2004-02-20 20:03:32 +00:00
register_hash (&rmd128_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_RIPEMD160
2004-02-20 20:03:32 +00:00
register_hash (&rmd160_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_WHIRLPOOL
2004-02-20 20:03:32 +00:00
register_hash (&whirlpool_desc);
#endif
2007-07-20 17:48:02 +00:00
#ifdef LTC_CHC_HASH
2004-10-30 03:00:26 +00:00
register_hash(&chc_desc);
if ((err = chc_register(register_cipher(&aes_enc_desc))) != CRYPT_OK) {
printf("chc_register error: %s\n", error_to_string(err));
exit(EXIT_FAILURE);
}
#endif
2004-02-20 20:03:32 +00:00
2003-03-03 00:59:24 +00:00
}
2005-06-09 00:08:13 +00:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */