~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/crypto/hashes/sha1sum.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Modification History
 
3
 *
 
4
 * 2004-May-20   Jason Rohrer
 
5
 * Created.
 
6
 */
 
7
 
 
8
 
 
9
 
 
10
#include "sha1.h"
 
11
#include "minorGems/formats/encodingUtils.h"
 
12
 
 
13
 
 
14
#include <stdio.h>
 
15
 
 
16
 
 
17
 
 
18
/**
 
19
 * Prints usage message and exits.
 
20
 *
 
21
 * @param inAppName the name of the app.
 
22
 */
 
23
void usage( char *inAppName );
 
24
 
 
25
 
 
26
 
 
27
int main( int inNumArgs, char **inArgs ) {
 
28
 
 
29
 
 
30
    if( inNumArgs != 2 ) {
 
31
        usage( inArgs[0] );
 
32
        }
 
33
 
 
34
 
 
35
    FILE *file = fopen( inArgs[1], "rb" );
 
36
 
 
37
    if( file == NULL ) {
 
38
        printf( "Failed to open file %s for reading\n\n", inArgs[1] );
 
39
 
 
40
        usage( inArgs[0] );
 
41
        }
 
42
 
 
43
    
 
44
    SHA_CTX shaContext;
 
45
 
 
46
    SHA1_Init( &shaContext );
 
47
 
 
48
               
 
49
    int bufferSize = 5000;
 
50
    unsigned char *buffer = new unsigned char[ bufferSize ];
 
51
 
 
52
    int numRead = bufferSize;
 
53
 
 
54
    char error = false;
 
55
    
 
56
    // read bytes from file until we run out
 
57
    while( numRead == bufferSize && !error ) {
 
58
 
 
59
        numRead = fread( buffer, 1, bufferSize, file );
 
60
 
 
61
        if( numRead > 0 ) {
 
62
            SHA1_Update( &shaContext, buffer, numRead );
 
63
            }
 
64
        else{
 
65
            error = true;
 
66
            }
 
67
        }
 
68
 
 
69
    fclose( file ); 
 
70
    delete [] buffer;
 
71
 
 
72
 
 
73
    if( error ) {
 
74
        printf( "Error reading from file %s\n", inArgs[1] );
 
75
        }
 
76
    else {
 
77
        unsigned char *rawDigest = new unsigned char[ SHA1_DIGEST_LENGTH ];
 
78
        
 
79
        SHA1_Final( rawDigest, &shaContext );
 
80
        
 
81
        // else hash is correct
 
82
        char *digestHexString = hexEncode( rawDigest, SHA1_DIGEST_LENGTH );
 
83
        
 
84
        printf( "%s  %s\n", digestHexString, inArgs[1] );
 
85
 
 
86
        delete [] rawDigest;
 
87
        delete [] digestHexString;
 
88
        }
 
89
    
 
90
    return 0;
 
91
    }
 
92
 
 
93
 
 
94
 
 
95
void usage( char *inAppName ) {
 
96
 
 
97
    printf( "Usage:\n\n" );
 
98
    printf( "\t%s file_to_sum\n", inAppName );
 
99
 
 
100
    printf( "example:\n" );
 
101
 
 
102
    printf( "\t%s test.txt\n", inAppName );
 
103
 
 
104
    exit( 1 );
 
105
    }