1 /* Computes SHA and MD5 of given file
2  * Copyright (C) 2000  David Helder
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  */
19 
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 
28 #include <gnet.h>
29 
30 
31 
32 int
main(int argc,char * argv[])33 main (int argc, char* argv[])
34 {
35   gchar* filename;
36   FILE* file;
37   gchar* buffer;
38   gint length;
39   struct stat filestat;
40 
41   gchar* str;
42 
43   GMD5* md5;
44   GMD5* md5b;
45 
46   GSHA* sha;
47   GSHA* shab;
48 
49   gnet_init ();
50 
51 
52   /* ******************** */
53 
54   if (argc != 2)
55     {
56       fprintf (stderr, "Usage: hash <filename>\n");
57       exit (EXIT_FAILURE);
58     }
59 
60   filename = argv[1];
61 
62   /* Open file */
63   file = fopen (filename, "rb");
64   g_assert (file);
65 
66   length = fstat(fileno(file), &filestat);
67   g_assert (length == 0);
68   length = filestat.st_size;
69   g_assert (length >= 0);
70 
71   if (length)
72     {
73       buffer = g_malloc (length);
74       g_assert (fread (buffer, length, 1, file) == 1);
75     }
76   else
77     buffer = NULL;
78   fclose (file);
79 
80   /* Reminder that on windows, text files have extra "\r" in them
81      where as on *NIX they do not. This will throw off SHA & MD5
82      if you don't filter the extra characters out before calling
83      those functions.
84   */
85 
86   /* **************************************** */
87 
88   /* Create MD5 */
89   md5 = gnet_md5_new (buffer, length);
90   g_assert (md5);
91 
92   /* Get string */
93   str = gnet_md5_get_string (md5);
94   g_assert (str);
95 
96   g_print ("%s MD5: %s\n", filename, str);
97 
98   /* Create MD5 from string */
99   md5b = gnet_md5_new_string (str);
100   g_assert (md5b);
101   g_free (str);
102 
103   /* Check if equal */
104   g_assert (gnet_md5_equal (md5, md5b));
105 
106   gnet_md5_delete (md5);
107   gnet_md5_delete (md5b);
108 
109   /* **************************************** */
110 
111   /* Create SHA */
112   sha = gnet_sha_new (buffer, length);
113   g_assert (sha);
114 
115   /* Get string */
116   str = gnet_sha_get_string (sha);
117   g_assert (str);
118 
119   g_print ("%s SHA: %s\n", filename, str);
120 
121   /* Create SHA from string */
122   shab = gnet_sha_new_string (str);
123   g_assert (shab);
124   g_free (str);
125 
126   /* Check if equal */
127   g_assert (gnet_sha_equal (sha, shab));
128 
129   gnet_sha_delete (sha);
130   gnet_sha_delete (shab);
131 
132   /* **************************************** */
133 
134   if (buffer)
135     g_free (buffer);
136 
137   /* **************************************** */
138 
139   exit (EXIT_SUCCESS);
140 
141   return 0;
142 }
143