comparison demos/hashsum.c @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 5d99163f7e32
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
1 /*
2 * Written by Daniel Richards <[email protected]> 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 <mycrypt_custom.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\n", hash_descriptor[x].name);
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 #ifdef TIGER
70 register_hash (&tiger_desc);
71 #endif
72 #ifdef MD2
73 register_hash (&md2_desc);
74 #endif
75 #ifdef MD4
76 register_hash (&md4_desc);
77 #endif
78 #ifdef MD5
79 register_hash (&md5_desc);
80 #endif
81 #ifdef SHA1
82 register_hash (&sha1_desc);
83 #endif
84 #ifdef SHA224
85 register_hash (&sha224_desc);
86 #endif
87 #ifdef SHA256
88 register_hash (&sha256_desc);
89 #endif
90 #ifdef SHA384
91 register_hash (&sha384_desc);
92 #endif
93 #ifdef SHA512
94 register_hash (&sha512_desc);
95 #endif
96 #ifdef RIPEMD128
97 register_hash (&rmd128_desc);
98 #endif
99 #ifdef RIPEMD160
100 register_hash (&rmd160_desc);
101 #endif
102 #ifdef WHIRLPOOL
103 register_hash (&whirlpool_desc);
104 #endif
105
106 }