00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <string.h>
00041 #include <stdio.h>
00042 #include "sha.h"
00043
00044 static void shaHashBlock(SHA_CTX *ctx);
00045
00046 void shaInit(SHA_CTX *ctx) {
00047 int i;
00048
00049 ctx->lenW = 0;
00050 ctx->sizeHi = ctx->sizeLo = 0;
00051
00052
00053
00054 ctx->H[0] = 0x67452301L;
00055 ctx->H[1] = 0xefcdab89L;
00056 ctx->H[2] = 0x98badcfeL;
00057 ctx->H[3] = 0x10325476L;
00058 ctx->H[4] = 0xc3d2e1f0L;
00059
00060 for (i = 0; i < 80; i++)
00061 ctx->W[i] = 0;
00062 }
00063
00064
00065 void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, int len) {
00066 int i;
00067
00068
00069
00070 for (i = 0; i < len; i++) {
00071 ctx->W[ctx->lenW / 4] <<= 8;
00072 ctx->W[ctx->lenW / 4] |= (unsigned long)dataIn[i];
00073 if ((++ctx->lenW) % 64 == 0) {
00074 shaHashBlock(ctx);
00075 ctx->lenW = 0;
00076 }
00077 ctx->sizeLo += 8;
00078 ctx->sizeHi += (ctx->sizeLo < 8);
00079 }
00080 }
00081
00082
00083 void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]) {
00084 unsigned char pad0x80 = 0x80;
00085 unsigned char pad0x00 = 0x00;
00086 unsigned char padlen[8];
00087 int i;
00088
00089
00090
00091 padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
00092 padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
00093 padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
00094 padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
00095 padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
00096 padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
00097 padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
00098 padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
00099 shaUpdate(ctx, &pad0x80, 1);
00100 while (ctx->lenW != 56)
00101 shaUpdate(ctx, &pad0x00, 1);
00102 shaUpdate(ctx, padlen, 8);
00103
00104
00105
00106 for (i = 0; i < 20; i++) {
00107 hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
00108 ctx->H[i / 4] <<= 8;
00109 }
00110
00111
00112
00113
00114 shaInit(ctx);
00115 }
00116
00117
00118 void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]) {
00119 SHA_CTX ctx;
00120
00121 shaInit(&ctx);
00122 shaUpdate(&ctx, dataIn, len);
00123 shaFinal(&ctx, hashout);
00124 }
00125
00126
00127 #define SHA_ROTL(X,n) (((X) << (n)) | ((X) >> (32-(n))))
00128
00129 static void shaHashBlock(SHA_CTX *ctx) {
00130 int t;
00131 unsigned long A,B,C,D,E,TEMP;
00132
00133 for (t = 16; t <= 79; t++)
00134 ctx->W[t] =
00135 SHA_ROTL(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
00136
00137 A = ctx->H[0];
00138 B = ctx->H[1];
00139 C = ctx->H[2];
00140 D = ctx->H[3];
00141 E = ctx->H[4];
00142
00143 for (t = 0; t <= 19; t++) {
00144 TEMP = SHA_ROTL(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999L;
00145 E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00146 }
00147 for (t = 20; t <= 39; t++) {
00148 TEMP = SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1L;
00149 E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00150 }
00151 for (t = 40; t <= 59; t++) {
00152 TEMP = SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdcL;
00153 E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00154 }
00155 for (t = 60; t <= 79; t++) {
00156 TEMP = SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6L;
00157 E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00158 }
00159
00160 ctx->H[0] += A;
00161 ctx->H[1] += B;
00162 ctx->H[2] += C;
00163 ctx->H[3] += D;
00164 ctx->H[4] += E;
00165 }
00166
00167
00168
00169
00170
00171
00172 char *shahash(char *str)
00173 {
00174 static char final[41];
00175 char *pos;
00176 unsigned char hashval[20];
00177 int x;
00178
00179 if(!str || strlen(str) == 0)
00180 return NULL;
00181
00182 shaBlock((unsigned char *)str, strlen(str), hashval);
00183
00184 pos = final;
00185 for(x=0;x<20;x++)
00186 {
00187 snprintf(pos, 3, "%02x", hashval[x]);
00188 pos += 2;
00189 }
00190 return (char *)final;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200 void shahash_r(const char* str, char hashbuf[41])
00201 {
00202 int x;
00203 char *pos;
00204 unsigned char hashval[20];
00205
00206 if(!str || strlen(str) == 0)
00207 return;
00208
00209 shaBlock((unsigned char *)str, strlen(str), hashval);
00210
00211 pos = hashbuf;
00212 for(x=0;x<20;x++)
00213 {
00214 snprintf(pos, 3, "%02x", hashval[x]);
00215 pos += 2;
00216 }
00217
00218 return;
00219 }
00220
00221
00222
00223