This source file includes following definitions.
- make_sha1_digest
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_SHA1Init
- PHP_SHA1Update
- PHP_SHA1Final
- SHA1Transform
- SHA1Encode
- SHA1Decode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include "php.h"
22
23
24
25 #include "sha1.h"
26 #include "md5.h"
27
28 PHPAPI void make_sha1_digest(char *sha1str, unsigned char *digest)
29 {
30 make_digest_ex(sha1str, digest, 20);
31 }
32
33
34
35 PHP_FUNCTION(sha1)
36 {
37 char *arg;
38 int arg_len;
39 zend_bool raw_output = 0;
40 char sha1str[41];
41 PHP_SHA1_CTX context;
42 unsigned char digest[20];
43
44 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
45 return;
46 }
47
48 sha1str[0] = '\0';
49 PHP_SHA1Init(&context);
50 PHP_SHA1Update(&context, arg, arg_len);
51 PHP_SHA1Final(digest, &context);
52 if (raw_output) {
53 RETURN_STRINGL(digest, 20, 1);
54 } else {
55 make_digest_ex(sha1str, digest, 20);
56 RETVAL_STRING(sha1str, 1);
57 }
58
59 }
60
61
62
63
64
65
66 PHP_FUNCTION(sha1_file)
67 {
68 char *arg;
69 int arg_len;
70 zend_bool raw_output = 0;
71 char sha1str[41];
72 unsigned char buf[1024];
73 unsigned char digest[20];
74 PHP_SHA1_CTX context;
75 int n;
76 php_stream *stream;
77
78 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|b", &arg, &arg_len, &raw_output) == FAILURE) {
79 return;
80 }
81
82 stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
83 if (!stream) {
84 RETURN_FALSE;
85 }
86
87 PHP_SHA1Init(&context);
88
89 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
90 PHP_SHA1Update(&context, buf, n);
91 }
92
93 PHP_SHA1Final(digest, &context);
94
95 php_stream_close(stream);
96
97 if (n<0) {
98 RETURN_FALSE;
99 }
100
101 if (raw_output) {
102 RETURN_STRINGL(digest, 20, 1);
103 } else {
104 make_digest_ex(sha1str, digest, 20);
105 RETVAL_STRING(sha1str, 1);
106 }
107 }
108
109
110
111 static void SHA1Transform(php_uint32[5], const unsigned char[64]);
112 static void SHA1Encode(unsigned char *, php_uint32 *, unsigned int);
113 static void SHA1Decode(php_uint32 *, const unsigned char *, unsigned int);
114
115 static unsigned char PADDING[64] =
116 {
117 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
120 };
121
122
123
124 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
125 #define G(x, y, z) ((x) ^ (y) ^ (z))
126 #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
127 #define I(x, y, z) ((x) ^ (y) ^ (z))
128
129
130
131 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
132
133
134
135 #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
136 (x[i&15]=ROTATE_LEFT(tmp, 1)) )
137
138
139
140 #define FF(a, b, c, d, e, w) { \
141 (e) += F ((b), (c), (d)) + (w) + (php_uint32)(0x5A827999); \
142 (e) += ROTATE_LEFT ((a), 5); \
143 (b) = ROTATE_LEFT((b), 30); \
144 }
145 #define GG(a, b, c, d, e, w) { \
146 (e) += G ((b), (c), (d)) + (w) + (php_uint32)(0x6ED9EBA1); \
147 (e) += ROTATE_LEFT ((a), 5); \
148 (b) = ROTATE_LEFT((b), 30); \
149 }
150 #define HH(a, b, c, d, e, w) { \
151 (e) += H ((b), (c), (d)) + (w) + (php_uint32)(0x8F1BBCDC); \
152 (e) += ROTATE_LEFT ((a), 5); \
153 (b) = ROTATE_LEFT((b), 30); \
154 }
155 #define II(a, b, c, d, e, w) { \
156 (e) += I ((b), (c), (d)) + (w) + (php_uint32)(0xCA62C1D6); \
157 (e) += ROTATE_LEFT ((a), 5); \
158 (b) = ROTATE_LEFT((b), 30); \
159 }
160
161
162
163
164
165 PHPAPI void PHP_SHA1Init(PHP_SHA1_CTX * context)
166 {
167 context->count[0] = context->count[1] = 0;
168
169
170 context->state[0] = 0x67452301;
171 context->state[1] = 0xefcdab89;
172 context->state[2] = 0x98badcfe;
173 context->state[3] = 0x10325476;
174 context->state[4] = 0xc3d2e1f0;
175 }
176
177
178
179
180
181
182
183 PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
184 unsigned int inputLen)
185 {
186 unsigned int i, index, partLen;
187
188
189 index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
190
191
192 if ((context->count[0] += ((php_uint32) inputLen << 3))
193 < ((php_uint32) inputLen << 3))
194 context->count[1]++;
195 context->count[1] += ((php_uint32) inputLen >> 29);
196
197 partLen = 64 - index;
198
199
200
201 if (inputLen >= partLen) {
202 memcpy
203 ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
204 SHA1Transform(context->state, context->buffer);
205
206 for (i = partLen; i + 63 < inputLen; i += 64)
207 SHA1Transform(context->state, &input[i]);
208
209 index = 0;
210 } else
211 i = 0;
212
213
214 memcpy
215 ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
216 inputLen - i);
217 }
218
219
220
221
222
223
224 PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
225 {
226 unsigned char bits[8];
227 unsigned int index, padLen;
228
229
230 bits[7] = context->count[0] & 0xFF;
231 bits[6] = (context->count[0] >> 8) & 0xFF;
232 bits[5] = (context->count[0] >> 16) & 0xFF;
233 bits[4] = (context->count[0] >> 24) & 0xFF;
234 bits[3] = context->count[1] & 0xFF;
235 bits[2] = (context->count[1] >> 8) & 0xFF;
236 bits[1] = (context->count[1] >> 16) & 0xFF;
237 bits[0] = (context->count[1] >> 24) & 0xFF;
238
239
240
241 index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
242 padLen = (index < 56) ? (56 - index) : (120 - index);
243 PHP_SHA1Update(context, PADDING, padLen);
244
245
246 PHP_SHA1Update(context, bits, 8);
247
248
249 SHA1Encode(digest, context->state, 20);
250
251
252
253 memset((unsigned char*) context, 0, sizeof(*context));
254 }
255
256
257
258
259
260 static void SHA1Transform(state, block)
261 php_uint32 state[5];
262 const unsigned char block[64];
263 {
264 php_uint32 a = state[0], b = state[1], c = state[2];
265 php_uint32 d = state[3], e = state[4], x[16], tmp;
266
267 SHA1Decode(x, block, 64);
268
269
270 FF(a, b, c, d, e, x[0]);
271 FF(e, a, b, c, d, x[1]);
272 FF(d, e, a, b, c, x[2]);
273 FF(c, d, e, a, b, x[3]);
274 FF(b, c, d, e, a, x[4]);
275 FF(a, b, c, d, e, x[5]);
276 FF(e, a, b, c, d, x[6]);
277 FF(d, e, a, b, c, x[7]);
278 FF(c, d, e, a, b, x[8]);
279 FF(b, c, d, e, a, x[9]);
280 FF(a, b, c, d, e, x[10]);
281 FF(e, a, b, c, d, x[11]);
282 FF(d, e, a, b, c, x[12]);
283 FF(c, d, e, a, b, x[13]);
284 FF(b, c, d, e, a, x[14]);
285 FF(a, b, c, d, e, x[15]);
286 FF(e, a, b, c, d, W(16));
287 FF(d, e, a, b, c, W(17));
288 FF(c, d, e, a, b, W(18));
289 FF(b, c, d, e, a, W(19));
290
291
292 GG(a, b, c, d, e, W(20));
293 GG(e, a, b, c, d, W(21));
294 GG(d, e, a, b, c, W(22));
295 GG(c, d, e, a, b, W(23));
296 GG(b, c, d, e, a, W(24));
297 GG(a, b, c, d, e, W(25));
298 GG(e, a, b, c, d, W(26));
299 GG(d, e, a, b, c, W(27));
300 GG(c, d, e, a, b, W(28));
301 GG(b, c, d, e, a, W(29));
302 GG(a, b, c, d, e, W(30));
303 GG(e, a, b, c, d, W(31));
304 GG(d, e, a, b, c, W(32));
305 GG(c, d, e, a, b, W(33));
306 GG(b, c, d, e, a, W(34));
307 GG(a, b, c, d, e, W(35));
308 GG(e, a, b, c, d, W(36));
309 GG(d, e, a, b, c, W(37));
310 GG(c, d, e, a, b, W(38));
311 GG(b, c, d, e, a, W(39));
312
313
314 HH(a, b, c, d, e, W(40));
315 HH(e, a, b, c, d, W(41));
316 HH(d, e, a, b, c, W(42));
317 HH(c, d, e, a, b, W(43));
318 HH(b, c, d, e, a, W(44));
319 HH(a, b, c, d, e, W(45));
320 HH(e, a, b, c, d, W(46));
321 HH(d, e, a, b, c, W(47));
322 HH(c, d, e, a, b, W(48));
323 HH(b, c, d, e, a, W(49));
324 HH(a, b, c, d, e, W(50));
325 HH(e, a, b, c, d, W(51));
326 HH(d, e, a, b, c, W(52));
327 HH(c, d, e, a, b, W(53));
328 HH(b, c, d, e, a, W(54));
329 HH(a, b, c, d, e, W(55));
330 HH(e, a, b, c, d, W(56));
331 HH(d, e, a, b, c, W(57));
332 HH(c, d, e, a, b, W(58));
333 HH(b, c, d, e, a, W(59));
334
335
336 II(a, b, c, d, e, W(60));
337 II(e, a, b, c, d, W(61));
338 II(d, e, a, b, c, W(62));
339 II(c, d, e, a, b, W(63));
340 II(b, c, d, e, a, W(64));
341 II(a, b, c, d, e, W(65));
342 II(e, a, b, c, d, W(66));
343 II(d, e, a, b, c, W(67));
344 II(c, d, e, a, b, W(68));
345 II(b, c, d, e, a, W(69));
346 II(a, b, c, d, e, W(70));
347 II(e, a, b, c, d, W(71));
348 II(d, e, a, b, c, W(72));
349 II(c, d, e, a, b, W(73));
350 II(b, c, d, e, a, W(74));
351 II(a, b, c, d, e, W(75));
352 II(e, a, b, c, d, W(76));
353 II(d, e, a, b, c, W(77));
354 II(c, d, e, a, b, W(78));
355 II(b, c, d, e, a, W(79));
356
357 state[0] += a;
358 state[1] += b;
359 state[2] += c;
360 state[3] += d;
361 state[4] += e;
362
363
364 memset((unsigned char*) x, 0, sizeof(x));
365 }
366
367
368
369
370
371
372 static void SHA1Encode(output, input, len)
373 unsigned char *output;
374 php_uint32 *input;
375 unsigned int len;
376 {
377 unsigned int i, j;
378
379 for (i = 0, j = 0; j < len; i++, j += 4) {
380 output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
381 output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
382 output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
383 output[j + 3] = (unsigned char) (input[i] & 0xff);
384 }
385 }
386
387
388
389
390
391
392 static void SHA1Decode(output, input, len)
393 php_uint32 *output;
394 const unsigned char *input;
395 unsigned int len;
396 {
397 unsigned int i, j;
398
399 for (i = 0, j = 0; j < len; i++, j += 4)
400 output[i] = ((php_uint32) input[j + 3]) | (((php_uint32) input[j + 2]) << 8) |
401 (((php_uint32) input[j + 1]) << 16) | (((php_uint32) input[j]) << 24);
402 }
403
404
405
406
407
408
409
410
411
412