2 * Written by Daniel Richards <kyhwana@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
10 #include <mycrypt_custom.h>
16 int main(int argc, char **argv)
20 unsigned char hash_buffer[MAXBLOCKSIZE];
23 /* You need to register algorithms before using them */
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\n", hash_descriptor[x].name);
34 idx = find_hash(argv[1]);
36 fprintf(stderr, "\nInvalid hash specified on command line.\n");
41 hash_descriptor[idx].init(&md);
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]);
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));
57 for (x = 0; x < (int)hash_descriptor[idx].hashsize; x++) {
58 printf("%02x",hash_buffer[x]);
60 printf(" %s\n", argv[z]);
67 void register_algs(void)
69 register_hash(&sha512_desc);
70 register_hash(&sha384_desc);
71 register_hash(&sha256_desc);
72 register_hash(&sha1_desc);
73 register_hash(&md5_desc);
74 register_hash(&md4_desc);
75 register_hash(&tiger_desc);
76 register_hash(&md2_desc);