6a26becaf2d5dea31d2d9a6fc6a9d208089ce3ad
[public/netxms.git] / src / libnetxms / sha1.h
1 /*
2 SHA-1 in C
3 By Steve Reid <sreid@sea-to-sky.net>
4 100% Public Domain
5
6 -----------------
7 Modified 7/98
8 By James H. Brown <jbrown@burgoyne.com>
9 Still 100% Public Domain
10
11 Corrected a problem which generated improper hash values on 16 bit machines
12 Routine SHA1Update changed from
13 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int
14 len)
15 to
16 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned
17 long len)
18
19 The 'len' parameter was declared an int which works fine on 32 bit machines.
20 However, on 16 bit machines an int is too small for the shifts being done
21 against
22 it. This caused the hash function to generate incorrect values if len was
23 greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
24
25 Since the file IO in main() reads 16K at a time, any file 8K or larger would
26 be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
27 "a"s).
28
29 I also changed the declaration of variables i & j in SHA1Update to
30 unsigned long from unsigned int for the same reason.
31
32 These changes should make no difference to any 32 bit implementations since
33 an
34 int and a long are the same size in those environments.
35
36 --
37 I also corrected a few compiler warnings generated by Borland C.
38 1. Added #include <process.h> for exit() prototype
39 2. Removed unused variable 'j' in SHA1Final
40 3. Changed exit(0) to return(0) at end of main.
41
42 ALL changes I made can be located by searching for comments containing 'JHB'
43 -----------------
44 Modified 8/98
45 By Steve Reid <sreid@sea-to-sky.net>
46 Still 100% public domain
47
48 1- Removed #include <process.h> and used return() instead of exit()
49 2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
50 3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
51
52 -----------------
53 Modified 4/01
54 By Saul Kravitz <Saul.Kravitz@celera.com>
55 Still 100% PD
56 Modified to run on Compaq Alpha hardware.
57
58
59 -----------------
60 Modified 10/03
61 By Victor Kirhenshtein (victor@securityprojects.org)
62 Still 100% public domain
63 Modified to be used as part of network mamagement system agent
64
65
66 */
67
68 /*
69 Test Vectors (from FIPS PUB 180-1)
70 "abc"
71 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
72 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
73 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
74 A million repetitions of "a"
75 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
76 */
77
78 #ifndef _sha1_h_
79 #define _sha1_h_
80
81 #include <string.h>
82
83 #if defined(_WIN32) || defined(_NETWARE)
84
85 typedef unsigned long uint32;
86
87 #define LITTLE_ENDIAN
88
89 #else
90
91 #if SIZEOF_LONG == 4
92 typedef unsigned long uint32;
93 #elif SIZEOF_INT == 4
94 typedef unsigned int uint32;
95 #endif
96
97 #if WORDS_BIGENDIAN
98 #undef LITTLE_ENDIAN
99 #else
100 #ifndef LITTLE_ENDIAN
101 #define LITTLE_ENDIAN
102 #endif
103 #endif
104
105 #endif /* _WIN32 */
106
107 typedef struct {
108 uint32 state[5];
109 uint32 count[2];
110 unsigned char buffer[64];
111 } SHA1_CTX;
112
113 void SHA1Init(SHA1_CTX* context);
114 void SHA1Update(SHA1_CTX* context, unsigned char* data, uint32 len); /*
115 JHB */
116 void SHA1Final(unsigned char digest[20], SHA1_CTX* context);
117
118 #endif /* _sha1_h_ */