summaryrefslogtreecommitdiff
path: root/3rdParty/sha1/shacmp.cpp
blob: 476ad3f0bd21892e48877656da702c987dfead9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 *  shacmp.cpp
 *
 *  Copyright (C) 1998, 2009
 *  Paul E. Jones <paulej@packetizer.com>
 *  All Rights Reserved
 *
 *****************************************************************************
 *  $Id: shacmp.cpp 12 2009-06-22 19:34:25Z paulej $
 *****************************************************************************
 *
 *  Description:
 *      This utility will compare two files by producing a message digest
 *      for each file using the Secure Hashing Algorithm and comparing
 *      the message digests.  This function will return 0 if they
 *      compare or 1 if they do not or if there is an error.
 *      Errors result in a return code higher than 1.
 *
 *  Portability Issues:
 *      none.
 *
 */

#include <stdio.h>
#include <string.h>
#include "sha1.h"

/*
 *  Return codes
 */
#define SHA1_COMPARE        0
#define SHA1_NO_COMPARE     1
#define SHA1_USAGE_ERROR    2
#define SHA1_FILE_ERROR     3

/*
 *  Function prototype
 */
void usage();

/*  
 *  main
 *
 *  Description:
 *      This is the entry point for the program
 *
 *  Parameters:
 *      argc: [in]
 *          This is the count of arguments in the argv array
 *      argv: [in]
 *          This is an array of filenames for which to compute message digests
 *
 *  Returns:
 *      Nothing.
 *
 *  Comments:
 *
 */
int main(int argc, char *argv[])
{
    SHA1        sha;                        // SHA-1 class
    FILE        *fp;                        // File pointer for reading files
    char        c;                          // Character read from file
    unsigned    message_digest[2][5];       // Message digest for files
    int         i;                          // Counter
    bool        message_match;              // Message digest match flag
    int         returncode;

    /*
     *  If we have two arguments, we will assume they are filenames.  If
     *  we do not have to arguments, call usage() and exit.
     */
    if (argc != 3)
    {
        usage();
        return SHA1_USAGE_ERROR;
    }

    /*
     *  Get the message digests for each file
     */
    for(i = 1; i <= 2; i++)
    {
        sha.Reset();

        if (!(fp = fopen(argv[i],"rb")))
        {
            fprintf(stderr, "sha: unable to open file %s\n", argv[i]);
            return SHA1_FILE_ERROR;
        }

        c = fgetc(fp);
        while(!feof(fp))
        {
            sha.Input(c);
            c = fgetc(fp);
        }

        fclose(fp);

        if (!sha.Result(message_digest[i-1]))
        {
            fprintf(stderr,"shacmp: could not compute message digest for %s\n",
                    argv[i]);
            return SHA1_FILE_ERROR;
        }
    }

    /*
     *  Compare the message digest values
     */
    message_match = true;
    for(i = 0; i < 5; i++)
    {
        if (message_digest[0][i] != message_digest[1][i])
        {
            message_match = false;
            break;
        }
    }

    if (message_match)
    {
        printf("Fingerprints match:\n");
        returncode = SHA1_COMPARE;
    }
    else
    {
        printf("Fingerprints do not match:\n");
        returncode = SHA1_NO_COMPARE;
    }

    printf( "\t%08X %08X %08X %08X %08X\n",
            message_digest[0][0],
            message_digest[0][1],
            message_digest[0][2],
            message_digest[0][3],
            message_digest[0][4]);
    printf( "\t%08X %08X %08X %08X %08X\n",
            message_digest[1][0],
            message_digest[1][1],
            message_digest[1][2],
            message_digest[1][3],
            message_digest[1][4]);

    return returncode;
}

/*  
 *  usage
 *
 *  Description:
 *      This function will display program usage information to the user.
 *
 *  Parameters:
 *      None.
 *
 *  Returns:
 *      Nothing.
 *
 *  Comments:
 *
 */
void usage()
{
    printf("usage: shacmp <file> <file>\n");
    printf("\tThis program will compare the message digests (fingerprints)\n");
    printf("\tfor two files using the Secure Hashing Algorithm (SHA-1).\n");
}