4 * Copyright (C) 1998, 2009
5 * Paul E. Jones <paulej@packetizer.com>
8 *****************************************************************************
9 * $Id: sha.c 12 2009-06-22 19:34:25Z paulej $
10 *****************************************************************************
13 * This utility will display the message digest (fingerprint) for
14 * the specified file(s).
26 #include <string/stdstring.h>
38 * This is the entry point for the program
42 * This is the count of arguments in the argv array
44 * This is an array of filenames for which to compute message
53 typedef struct SHA1Context
55 unsigned Message_Digest[5]; /* Message Digest (output) */
57 unsigned Length_Low; /* Message length in bits */
58 unsigned Length_High; /* Message length in bits */
60 unsigned char Message_Block[64]; /* 512-bit message blocks */
61 int Message_Block_Index; /* Index into message block array */
63 int Computed; /* Is the digest computed? */
64 int Corrupted; /* Is the message digest corruped? */
67 int main(int argc, char *argv[])
69 struct SHA1Context sha; /* SHA-1 context */
70 FILE *fp; /* File pointer for reading files*/
71 char c; /* Character read from file */
73 int reading_stdin; /* Are we reading standard in? */
74 int read_stdin = 0; /* Have we read stdin? */
77 * Check the program arguments and print usage information if -?
78 * or --help is passed as the first argument.
80 if (argc > 1 && (string_is_equal(argv[1],"-?") ||
81 string_is_equal(argv[1],"--help")))
88 * For each filename passed in on the command line, calculate the
89 * SHA-1 value and display it.
91 for (i = 0; i < argc; i++)
94 * We start the counter at 0 to guarantee entry into the for
95 * loop. So if 'i' is zero, we will increment it now. If there
96 * is no argv[1], we will use STDIN below.
101 if (argc == 1 || string_is_equal(argv[i],"-"))
104 setmode(fileno(stdin), _O_BINARY);
111 if (!(fp = fopen(argv[i],"rb")))
114 "sha: unable to open file %s\n",
122 * We do not want to read STDIN multiple times
133 * Reset the SHA-1 context and process input
140 SHA1Input(&sha, &c, 1);
147 if (!SHA1Result(&sha))
150 "sha: could not compute message digest for %s\n",
151 reading_stdin?"STDIN":argv[i]);
155 printf( "%08X %08X %08X %08X %08X - %s\n",
156 sha.Message_Digest[0],
157 sha.Message_Digest[1],
158 sha.Message_Digest[2],
159 sha.Message_Digest[3],
160 sha.Message_Digest[4],
161 reading_stdin?"STDIN":argv[i]);
172 * This function will display program usage information to the
186 printf("usage: sha <file> [<file> ...]\n");
187 printf("\tThis program will display the message digest\n");
188 printf("\tfor files using the Secure Hashing Algorithm (SHA-1).\n");