Home | History | Annotate | Download | only in demos
      1 /*
      2  * Written by Daniel Richards <kyhwana (at) world-net.co.nz> 6/7/2002
      3  * hash.c: This app uses libtomcrypt to hash either stdin or a file
      4  * This file is Public Domain. No rights are reserved.
      5  * Compile with 'gcc hashsum.c -o hashsum -ltomcrypt'
      6  * This example isn't really big enough to warrent splitting into
      7  * more functions ;)
      8 */
      9 
     10 #include <tomcrypt.h>
     11 
     12 int errno;
     13 
     14 void register_algs();
     15 
     16 int main(int argc, char **argv)
     17 {
     18    int idx, x, z;
     19    unsigned long w;
     20    unsigned char hash_buffer[MAXBLOCKSIZE];
     21    hash_state md;
     22 
     23    /* You need to register algorithms before using them */
     24    register_algs();
     25    if (argc < 2) {
     26       printf("usage: ./hash algorithm file [file ...]\n");
     27       printf("Algorithms:\n");
     28       for (x = 0; hash_descriptor[x].name != NULL; x++) {
     29          printf(" %s (%d)\n", hash_descriptor[x].name, hash_descriptor[x].ID);
     30       }
     31       exit(EXIT_SUCCESS);
     32    }
     33 
     34    idx = find_hash(argv[1]);
     35    if (idx == -1) {
     36       fprintf(stderr, "\nInvalid hash specified on command line.\n");
     37       return -1;
     38    }
     39 
     40    if (argc == 2) {
     41       hash_descriptor[idx].init(&md);
     42       do {
     43          x = fread(hash_buffer, 1, sizeof(hash_buffer), stdin);
     44          hash_descriptor[idx].process(&md, hash_buffer, x);
     45       } while (x == sizeof(hash_buffer));
     46       hash_descriptor[idx].done(&md, hash_buffer);
     47       for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) {
     48           printf("%02x",hash_buffer[x]);
     49       }
     50       printf("  (stdin)\n");
     51    } else {
     52       for (z = 2; z < argc; z++) {
     53          w = sizeof(hash_buffer);
     54          if ((errno = hash_file(idx,argv[z],hash_buffer,&w)) != CRYPT_OK) {
     55             printf("File hash error: %s\n", error_to_string(errno));
     56          } else {
     57              for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) {
     58                  printf("%02x",hash_buffer[x]);
     59              }
     60              printf("  %s\n", argv[z]);
     61          }
     62       }
     63    }
     64    return EXIT_SUCCESS;
     65 }
     66 
     67 void register_algs(void)
     68 {
     69   int err;
     70 
     71 #ifdef TIGER
     72   register_hash (&tiger_desc);
     73 #endif
     74 #ifdef MD2
     75   register_hash (&md2_desc);
     76 #endif
     77 #ifdef MD4
     78   register_hash (&md4_desc);
     79 #endif
     80 #ifdef MD5
     81   register_hash (&md5_desc);
     82 #endif
     83 #ifdef SHA1
     84   register_hash (&sha1_desc);
     85 #endif
     86 #ifdef SHA224
     87   register_hash (&sha224_desc);
     88 #endif
     89 #ifdef SHA256
     90   register_hash (&sha256_desc);
     91 #endif
     92 #ifdef SHA384
     93   register_hash (&sha384_desc);
     94 #endif
     95 #ifdef SHA512
     96   register_hash (&sha512_desc);
     97 #endif
     98 #ifdef RIPEMD128
     99   register_hash (&rmd128_desc);
    100 #endif
    101 #ifdef RIPEMD160
    102   register_hash (&rmd160_desc);
    103 #endif
    104 #ifdef WHIRLPOOL
    105   register_hash (&whirlpool_desc);
    106 #endif
    107 #ifdef CHC_HASH
    108   register_hash(&chc_desc);
    109   if ((err = chc_register(register_cipher(&aes_enc_desc))) != CRYPT_OK) {
    110      printf("chc_register error: %s\n", error_to_string(err));
    111      exit(EXIT_FAILURE);
    112   }
    113 #endif
    114 
    115 }
    116 
    117 /* $Source: /cvs/libtom/libtomcrypt/demos/hashsum.c,v $ */
    118 /* $Revision: 1.2 $ */
    119 /* $Date: 2005/05/05 14:35:56 $ */
    120