2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * Copyright (C) 1999-2000 Dave Smith & Julian Missig
22 Implements the Secure Hash Algorithm (SHA1)
24 Copyright (C) 1999 Scott G. Miller
26 Released under the terms of the GNU General Public License v2
27 see file COPYING for details
30 Robert Klep <robert@ilse.nl> -- Expansion function fix
31 Thomas "temas" Muldowney <temas@box5.net>:
32 -- shahash() for string fun
33 -- Will add the int32 stuff in a few
36 FIXME: This source takes int to be a 32 bit integer. This
37 may vary from system to system. I'd use autoconf if I was familiar
38 with it. Anyone want to help me out?
51 # include <sys/stat.h>
52 # include <sys/types.h>
59 # define INT64 long long
62 # define snprintf _snprintf
63 # define INT64 __int64
66 #define switch_endianness(x) (x<<24 & 0xff000000) | \
67 (x<<8 & 0x00ff0000) | \
68 (x>>8 & 0x0000ff00) | \
71 /* Initial hash values */
78 /* SHA1 round constants */
84 /* Round functions. Note that f2() is used in both rounds 2 and 4 */
85 #define f1(B,C,D) ((B & C) | ((~B) & D))
86 #define f2(B,C,D) (B ^ C ^ D)
87 #define f3(B,C,D) ((B & C) | (B & D) | (C & D))
89 /* left circular shift functions (rotate left) */
90 #define rol1(x) ((x<<1) | ((x>>31) & 1))
91 #define rol5(A) ((A<<5) | ((A>>27) & 0x1f))
92 #define rol30(B) ((B<<30) | ((B>>2) & 0x3fffffff))
95 Hashes 'data', which should be a pointer to 512 bits of data (sixteen
96 32 bit ints), into the ongoing 160 bit hash value (five 32 bit ints)
100 sha_hash(int *data, int *hash)
103 unsigned int A=hash[0], B=hash[1], C=hash[2], D=hash[3], E=hash[4];
104 unsigned int t, x, TEMP;
108 #ifndef WORDS_BIGENDIAN
109 W[t]=switch_endianness(data[t]);
116 /* SHA1 Data expansion */
117 for (t=16; t<80; t++)
119 x=W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
123 /* SHA1 main loop (t=0 to 79)
124 This is broken down into four subloops in order to use
125 the correct round function and constant */
128 TEMP=rol5(A) + f1(B,C,D) + E + W[t] + K1;
137 TEMP=rol5(A) + f2(B,C,D) + E + W[t] + K2;
146 TEMP=rol5(A) + f3(B,C,D) + E + W[t] + K3;
155 TEMP=rol5(A) + f2(B,C,D) + E + W[t] + K4;
171 Takes a pointer to a 160 bit block of data (five 32 bit ints) and
172 intializes it to the start constants of the SHA1 algorithm. This
173 must be called before using hash in the call to sha_hash
186 int strprintsha(char *dest, int *hashval)
189 char *hashstr = dest;
192 snprintf(hashstr, 9, "%08x", hashval[x]);
195 //snprintf(hashstr++, 1, "\0");
202 char *shahash(const char *str)
204 char read_buffer[65];
205 //int read_buffer[64];
211 static char final[41];
214 hashval = (int *)malloc(20);
222 memset(read_buffer, 0, 65);
223 read_buffer[0] = 0x80;
224 sha_hash((int *)read_buffer, hashval);
229 memset(read_buffer, 0, 65);
230 strncpy((char*)read_buffer, str, 64);
231 c = strlen((char *)read_buffer);
237 read_buffer[c]=(char)0x80;
238 for (i=c+1; i<64; i++)
242 /* we need to do an entire new block */
243 sha_hash((int *)read_buffer, hashval);
245 ((int*)read_buffer)[i]=0;
247 #ifndef WORDS_BIGENDIAN
250 read_buffer[56+i]=(char)(length>>(56-(i*8))) & 0xff;
253 memcpy(read_buffer+56, &length, 8);
257 sha_hash((int *)read_buffer, hashval);
261 strprintsha((char *)final, hashval);
263 return (char *)final;