File: sha1.c

package info (click to toggle)
sleuthkit 4.12.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,608 kB
  • sloc: ansic: 143,795; cpp: 52,225; java: 37,892; xml: 2,416; python: 1,076; perl: 874; makefile: 439; sh: 184
file content (96 lines) | stat: -rw-r--r-- 2,488 bytes parent folder | download | duplicates (5)
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
/* sha1.c : Implementation of the Secure Hash Algorithm */

/* SHA: NIST's Secure Hash Algorithm */

/*	This version written November 2000 by David Ireland of 
	DI Management Services Pty Limited <code@di-mgt.com.au>

	Adapted from code in the Python Cryptography Toolkit, 
	version 1.0.0 by A.M. Kuchling 1995.
*/

/* AM Kuchling's posting:- 
   Based on SHA code originally posted to sci.crypt by Peter Gutmann
   in message <30ajo5$oe8@ccu2.auckland.ac.nz>.
   Modified to test for endianness on creation of SHA objects by AMK.
   Also, the original specification of SHA was found to have a weakness
   by NSA/NIST.  This code implements the fixed version of SHA.
*/

/* Here's the first paragraph of Peter Gutmann's posting:
   
The following is my SHA (FIPS 180) code updated to allow use of the "fixed"
SHA, thanks to Jim Gillogly and an anonymous contributor for the information on
what's changed in the new version.  The fix is a simple change which involves
adding a single rotate in the initial expansion function.  It is unknown
whether this is an optimal solution to the problem which was discovered in the
SHA or whether it's simply a bandaid which fixes the problem with a minimum of
effort (for example the reengineering of a great many Capstone chips).
*/


#include <stdio.h>
#include <string.h>
#include "tsk/libtsk.h"


#define SHA_HASH_LENGTH 	20
#define SHA_BUFSIZ 1024

char *
crunch(fp)
  FILE *fp;
{

    unsigned char sum[SHA_HASH_LENGTH];

    unsigned char buf[SHA_BUFSIZ];

    static char result[2 * SHA_HASH_LENGTH + 1];
    static char hex[] = "0123456789abcdef";

    TSK_SHA_CTX sha;
    int i;
    int buflen;

    TSK_SHA_Init(&sha);
    while ((buflen = fread(buf, 1, SHA_BUFSIZ, fp)) > 0)
        TSK_SHA_Update(&sha, buf, buflen);

    TSK_SHA_Final(sum, &sha);

    for (i = 0; i < SHA_HASH_LENGTH; i++) {
        result[2 * i] = hex[(sum[i] >> 4) & 0xf];
        result[2 * i + 1] = hex[sum[i] & 0xf];
    }
    return (result);
}




int
main(argc, argv)
  int argc;
  char **argv;
{
    char *myname = argv[0];
    char *crunch();

    if (argc < 2) {
        printf("%s\n", crunch(stdin));
    }
    else {
        while (--argc && *++argv) {
            FILE *fp;
            if ((fp = fopen(*argv, "r")) == 0) {
                fprintf(stderr, "%s: ", myname);
                perror(*argv);
                return (1);
            }
            printf("%s  %s\n", crunch(fp), *argv);
            fclose(fp);
        }
    }
    return (0);
}