2003-03-02 19:59:24 -05: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 18:55:53 -05:00
|
|
|
#include <tomcrypt.h>
|
2003-03-02 19:59:24 -05:00
|
|
|
|
2017-05-01 08:10:24 -04:00
|
|
|
#if _POSIX_C_SOURCE >= 200112L
|
|
|
|
#include <libgen.h>
|
|
|
|
#else
|
|
|
|
#define basename(x) x
|
|
|
|
#endif
|
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
int errno;
|
|
|
|
|
2016-01-23 12:59:44 -05:00
|
|
|
void register_algs(void);
|
2003-03-02 19:59:24 -05:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-04-23 16:37:32 -04:00
|
|
|
int idx, z;
|
|
|
|
unsigned long w, x;
|
2003-03-02 19:59:24 -05:00
|
|
|
unsigned char hash_buffer[MAXBLOCKSIZE];
|
|
|
|
|
|
|
|
/* You need to register algorithms before using them */
|
|
|
|
register_algs();
|
|
|
|
if (argc < 2) {
|
2017-05-01 08:10:24 -04:00
|
|
|
printf("usage: %s algorithm file [file ...]\n", basename(argv[0]));
|
2003-03-02 19:59:24 -05:00
|
|
|
printf("Algorithms:\n");
|
2017-05-01 08:10:24 -04:00
|
|
|
w = 0;
|
2003-03-02 19:59:24 -05:00
|
|
|
for (x = 0; hash_descriptor[x].name != NULL; x++) {
|
2017-05-01 08:10:24 -04:00
|
|
|
w += printf("%-14s", hash_descriptor[x].name);
|
|
|
|
if (w >= 70) {
|
|
|
|
printf("\n");
|
|
|
|
w = 0;
|
|
|
|
}
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
2017-05-01 08:10:24 -04:00
|
|
|
printf("\n");
|
2003-03-02 19:59:24 -05:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = find_hash(argv[1]);
|
|
|
|
if (idx == -1) {
|
|
|
|
fprintf(stderr, "\nInvalid hash specified on command line.\n");
|
2017-05-01 08:10:24 -04:00
|
|
|
return EXIT_FAILURE;
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 2) {
|
2017-04-23 16:37:32 -04:00
|
|
|
w = sizeof(hash_buffer);
|
|
|
|
if ((errno = hash_filehandle(idx, stdin, hash_buffer, &w)) != CRYPT_OK) {
|
2017-05-01 08:10:24 -04:00
|
|
|
fprintf(stderr, "File hash error: %s\n", error_to_string(errno));
|
|
|
|
return EXIT_FAILURE;
|
2017-04-23 16:37:32 -04:00
|
|
|
} else {
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
printf("%02x",hash_buffer[x]);
|
|
|
|
}
|
|
|
|
printf(" *-\n");
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (z = 2; z < argc; z++) {
|
|
|
|
w = sizeof(hash_buffer);
|
|
|
|
if ((errno = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) {
|
2017-05-01 08:10:24 -04:00
|
|
|
fprintf(stderr, "File hash error: %s\n", error_to_string(errno));
|
|
|
|
return EXIT_FAILURE;
|
2003-03-02 19:59:24 -05:00
|
|
|
} else {
|
2017-04-23 16:37:32 -04:00
|
|
|
for (x = 0; x < w; x++) {
|
2003-03-02 19:59:24 -05:00
|
|
|
printf("%02x",hash_buffer[x]);
|
|
|
|
}
|
2017-04-23 11:32:21 -04:00
|
|
|
printf(" *%s\n", argv[z]);
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2003-09-25 21:16:18 -04:00
|
|
|
void register_algs(void)
|
2003-03-02 19:59:24 -05:00
|
|
|
{
|
2004-10-29 23:00:26 -04:00
|
|
|
int err;
|
2017-03-01 05:48:39 -05:00
|
|
|
LTC_UNUSED_PARAM(err);
|
2004-10-29 23:00:26 -04:00
|
|
|
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_TIGER
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&tiger_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_MD2
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&md2_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_MD4
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&md4_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_MD5
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&md5_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_SHA1
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&sha1_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_SHA224
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&sha224_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_SHA256
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&sha256_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_SHA384
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&sha384_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_SHA512
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&sha512_desc);
|
|
|
|
#endif
|
2017-04-21 11:03:16 -04:00
|
|
|
#ifdef LTC_SHA512_224
|
|
|
|
register_hash (&sha512_224_desc);
|
|
|
|
#endif
|
|
|
|
#ifdef LTC_SHA512_256
|
|
|
|
register_hash (&sha512_256_desc);
|
|
|
|
#endif
|
|
|
|
#ifdef LTC_SHA3
|
|
|
|
register_hash (&sha3_224_desc);
|
|
|
|
register_hash (&sha3_256_desc);
|
|
|
|
register_hash (&sha3_384_desc);
|
|
|
|
register_hash (&sha3_512_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_RIPEMD128
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&rmd128_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_RIPEMD160
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&rmd160_desc);
|
|
|
|
#endif
|
2017-04-21 11:03:16 -04:00
|
|
|
#ifdef LTC_RIPEMD256
|
|
|
|
register_hash (&rmd256_desc);
|
|
|
|
#endif
|
|
|
|
#ifdef LTC_RIPEMD320
|
|
|
|
register_hash (&rmd320_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_WHIRLPOOL
|
2004-02-20 15:03:32 -05:00
|
|
|
register_hash (&whirlpool_desc);
|
|
|
|
#endif
|
2017-04-19 16:50:34 -04:00
|
|
|
#ifdef LTC_BLAKE2S
|
|
|
|
register_hash (&blake2s_128_desc);
|
|
|
|
register_hash (&blake2s_160_desc);
|
|
|
|
register_hash (&blake2s_224_desc);
|
|
|
|
register_hash (&blake2s_256_desc);
|
|
|
|
#endif
|
|
|
|
#ifdef LTC_BLAKE2B
|
|
|
|
register_hash (&blake2b_160_desc);
|
|
|
|
register_hash (&blake2b_256_desc);
|
|
|
|
register_hash (&blake2b_384_desc);
|
|
|
|
register_hash (&blake2b_512_desc);
|
|
|
|
#endif
|
2007-07-20 13:48:02 -04:00
|
|
|
#ifdef LTC_CHC_HASH
|
2004-10-29 23:00:26 -04: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 15:03:32 -05:00
|
|
|
|
2003-03-02 19:59:24 -05:00
|
|
|
}
|
2005-06-08 20:08:13 -04:00
|
|
|
|
|
|
|
/* $Source$ */
|
|
|
|
/* $Revision$ */
|
|
|
|
/* $Date$ */
|