2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 of the PHP license, |
8
| that is bundled with this package in the file LICENSE, and is |
9
| available through the world-wide-web at the following url: |
10
| http://www.php.net/license/3_0.txt. |
11
| If you did not receive a copy of the PHP license and are unable to |
12
| obtain it through the world-wide-web, please send a note to |
13
| license@php.net so we can mail you a copy immediately. |
14
+----------------------------------------------------------------------+
15
| Author: Stefan Esser <sesser@php.net> |
16
+----------------------------------------------------------------------+
19
/* $Id: sha1.c,v 1.9.2.2 2005/04/16 09:50:13 thetaphi Exp $ */
23
/* This code is heavily based on the PHP md5 implementation */
27
PHPAPI void make_sha1_digest(char *sha1str, unsigned char *digest)
31
for (i = 0; i < 20; i++) {
32
sprintf(sha1str, "%02x", digest[i]);
39
/* {{{ proto string sha1(string str [, bool raw_output])
40
Calculate the sha1 hash of a string */
45
zend_bool raw_output = 0;
48
unsigned char digest[20];
50
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
55
PHP_SHA1Init(&context);
56
PHP_SHA1Update(&context, arg, arg_len);
57
PHP_SHA1Final(digest, &context);
59
RETURN_STRINGL(digest, 20, 1);
61
make_sha1_digest(sha1str, digest);
62
RETVAL_STRING(sha1str, 1);
70
/* {{{ proto string sha1_file(string filename [, bool raw_output])
71
Calculate the sha1 hash of given filename */
72
PHP_FUNCTION(sha1_file)
76
zend_bool raw_output = 0;
78
unsigned char buf[1024];
79
unsigned char digest[20];
84
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
88
stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL);
93
PHP_SHA1Init(&context);
95
while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
96
PHP_SHA1Update(&context, buf, n);
99
PHP_SHA1Final(digest, &context);
101
php_stream_close(stream);
108
RETURN_STRINGL(digest, 20, 1);
110
make_sha1_digest(sha1str, digest);
111
RETVAL_STRING(sha1str, 1);
117
static void SHA1Transform(php_uint32[5], const unsigned char[64]);
118
static void SHA1Encode(unsigned char *, php_uint32 *, unsigned int);
119
static void SHA1Decode(php_uint32 *, const unsigned char *, unsigned int);
121
static unsigned char PADDING[64] =
123
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
128
/* F, G, H and I are basic SHA1 functions.
130
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
131
#define G(x, y, z) ((x) ^ (y) ^ (z))
132
#define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
133
#define I(x, y, z) ((x) ^ (y) ^ (z))
135
/* ROTATE_LEFT rotates x left n bits.
137
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
141
#define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
142
(x[i&15]=ROTATE_LEFT(tmp, 1)) )
144
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
146
#define FF(a, b, c, d, e, w) { \
147
(e) += F ((b), (c), (d)) + (w) + (php_uint32)(0x5A827999); \
148
(e) += ROTATE_LEFT ((a), 5); \
149
(b) = ROTATE_LEFT((b), 30); \
151
#define GG(a, b, c, d, e, w) { \
152
(e) += G ((b), (c), (d)) + (w) + (php_uint32)(0x6ED9EBA1); \
153
(e) += ROTATE_LEFT ((a), 5); \
154
(b) = ROTATE_LEFT((b), 30); \
156
#define HH(a, b, c, d, e, w) { \
157
(e) += H ((b), (c), (d)) + (w) + (php_uint32)(0x8F1BBCDC); \
158
(e) += ROTATE_LEFT ((a), 5); \
159
(b) = ROTATE_LEFT((b), 30); \
161
#define II(a, b, c, d, e, w) { \
162
(e) += I ((b), (c), (d)) + (w) + (php_uint32)(0xCA62C1D6); \
163
(e) += ROTATE_LEFT ((a), 5); \
164
(b) = ROTATE_LEFT((b), 30); \
169
* SHA1 initialization. Begins an SHA1 operation, writing a new context.
171
PHPAPI void PHP_SHA1Init(PHP_SHA1_CTX * context)
173
context->count[0] = context->count[1] = 0;
174
/* Load magic initialization constants.
176
context->state[0] = 0x67452301;
177
context->state[1] = 0xefcdab89;
178
context->state[2] = 0x98badcfe;
179
context->state[3] = 0x10325476;
180
context->state[4] = 0xc3d2e1f0;
184
/* {{{ PHP_SHA1Update
185
SHA1 block update operation. Continues an SHA1 message-digest
186
operation, processing another message block, and updating the
189
PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
190
unsigned int inputLen)
192
unsigned int i, index, partLen;
194
/* Compute number of bytes mod 64 */
195
index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
197
/* Update number of bits */
198
if ((context->count[0] += ((php_uint32) inputLen << 3))
199
< ((php_uint32) inputLen << 3))
201
context->count[1] += ((php_uint32) inputLen >> 29);
203
partLen = 64 - index;
205
/* Transform as many times as possible.
207
if (inputLen >= partLen) {
209
((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
210
SHA1Transform(context->state, context->buffer);
212
for (i = partLen; i + 63 < inputLen; i += 64)
213
SHA1Transform(context->state, &input[i]);
219
/* Buffer remaining input */
221
((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
227
SHA1 finalization. Ends an SHA1 message-digest operation, writing the
228
the message digest and zeroizing the context.
230
PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
232
unsigned char bits[8];
233
unsigned int index, padLen;
235
/* Save number of bits */
236
bits[7] = context->count[0] & 0xFF;
237
bits[6] = (context->count[0] >> 8) & 0xFF;
238
bits[5] = (context->count[0] >> 16) & 0xFF;
239
bits[4] = (context->count[0] >> 24) & 0xFF;
240
bits[3] = context->count[1] & 0xFF;
241
bits[2] = (context->count[1] >> 8) & 0xFF;
242
bits[1] = (context->count[1] >> 16) & 0xFF;
243
bits[0] = (context->count[1] >> 24) & 0xFF;
245
/* Pad out to 56 mod 64.
247
index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
248
padLen = (index < 56) ? (56 - index) : (120 - index);
249
PHP_SHA1Update(context, PADDING, padLen);
251
/* Append length (before padding) */
252
PHP_SHA1Update(context, bits, 8);
254
/* Store state in digest */
255
SHA1Encode(digest, context->state, 20);
257
/* Zeroize sensitive information.
259
memset((unsigned char*) context, 0, sizeof(*context));
264
* SHA1 basic transformation. Transforms state based on block.
266
static void SHA1Transform(state, block)
268
const unsigned char block[64];
270
php_uint32 a = state[0], b = state[1], c = state[2];
271
php_uint32 d = state[3], e = state[4], x[16], tmp;
273
SHA1Decode(x, block, 64);
276
FF(a, b, c, d, e, x[0]); /* 1 */
277
FF(e, a, b, c, d, x[1]); /* 2 */
278
FF(d, e, a, b, c, x[2]); /* 3 */
279
FF(c, d, e, a, b, x[3]); /* 4 */
280
FF(b, c, d, e, a, x[4]); /* 5 */
281
FF(a, b, c, d, e, x[5]); /* 6 */
282
FF(e, a, b, c, d, x[6]); /* 7 */
283
FF(d, e, a, b, c, x[7]); /* 8 */
284
FF(c, d, e, a, b, x[8]); /* 9 */
285
FF(b, c, d, e, a, x[9]); /* 10 */
286
FF(a, b, c, d, e, x[10]); /* 11 */
287
FF(e, a, b, c, d, x[11]); /* 12 */
288
FF(d, e, a, b, c, x[12]); /* 13 */
289
FF(c, d, e, a, b, x[13]); /* 14 */
290
FF(b, c, d, e, a, x[14]); /* 15 */
291
FF(a, b, c, d, e, x[15]); /* 16 */
292
FF(e, a, b, c, d, W(16)); /* 17 */
293
FF(d, e, a, b, c, W(17)); /* 18 */
294
FF(c, d, e, a, b, W(18)); /* 19 */
295
FF(b, c, d, e, a, W(19)); /* 20 */
298
GG(a, b, c, d, e, W(20)); /* 21 */
299
GG(e, a, b, c, d, W(21)); /* 22 */
300
GG(d, e, a, b, c, W(22)); /* 23 */
301
GG(c, d, e, a, b, W(23)); /* 24 */
302
GG(b, c, d, e, a, W(24)); /* 25 */
303
GG(a, b, c, d, e, W(25)); /* 26 */
304
GG(e, a, b, c, d, W(26)); /* 27 */
305
GG(d, e, a, b, c, W(27)); /* 28 */
306
GG(c, d, e, a, b, W(28)); /* 29 */
307
GG(b, c, d, e, a, W(29)); /* 30 */
308
GG(a, b, c, d, e, W(30)); /* 31 */
309
GG(e, a, b, c, d, W(31)); /* 32 */
310
GG(d, e, a, b, c, W(32)); /* 33 */
311
GG(c, d, e, a, b, W(33)); /* 34 */
312
GG(b, c, d, e, a, W(34)); /* 35 */
313
GG(a, b, c, d, e, W(35)); /* 36 */
314
GG(e, a, b, c, d, W(36)); /* 37 */
315
GG(d, e, a, b, c, W(37)); /* 38 */
316
GG(c, d, e, a, b, W(38)); /* 39 */
317
GG(b, c, d, e, a, W(39)); /* 40 */
320
HH(a, b, c, d, e, W(40)); /* 41 */
321
HH(e, a, b, c, d, W(41)); /* 42 */
322
HH(d, e, a, b, c, W(42)); /* 43 */
323
HH(c, d, e, a, b, W(43)); /* 44 */
324
HH(b, c, d, e, a, W(44)); /* 45 */
325
HH(a, b, c, d, e, W(45)); /* 46 */
326
HH(e, a, b, c, d, W(46)); /* 47 */
327
HH(d, e, a, b, c, W(47)); /* 48 */
328
HH(c, d, e, a, b, W(48)); /* 49 */
329
HH(b, c, d, e, a, W(49)); /* 50 */
330
HH(a, b, c, d, e, W(50)); /* 51 */
331
HH(e, a, b, c, d, W(51)); /* 52 */
332
HH(d, e, a, b, c, W(52)); /* 53 */
333
HH(c, d, e, a, b, W(53)); /* 54 */
334
HH(b, c, d, e, a, W(54)); /* 55 */
335
HH(a, b, c, d, e, W(55)); /* 56 */
336
HH(e, a, b, c, d, W(56)); /* 57 */
337
HH(d, e, a, b, c, W(57)); /* 58 */
338
HH(c, d, e, a, b, W(58)); /* 59 */
339
HH(b, c, d, e, a, W(59)); /* 60 */
342
II(a, b, c, d, e, W(60)); /* 61 */
343
II(e, a, b, c, d, W(61)); /* 62 */
344
II(d, e, a, b, c, W(62)); /* 63 */
345
II(c, d, e, a, b, W(63)); /* 64 */
346
II(b, c, d, e, a, W(64)); /* 65 */
347
II(a, b, c, d, e, W(65)); /* 66 */
348
II(e, a, b, c, d, W(66)); /* 67 */
349
II(d, e, a, b, c, W(67)); /* 68 */
350
II(c, d, e, a, b, W(68)); /* 69 */
351
II(b, c, d, e, a, W(69)); /* 70 */
352
II(a, b, c, d, e, W(70)); /* 71 */
353
II(e, a, b, c, d, W(71)); /* 72 */
354
II(d, e, a, b, c, W(72)); /* 73 */
355
II(c, d, e, a, b, W(73)); /* 74 */
356
II(b, c, d, e, a, W(74)); /* 75 */
357
II(a, b, c, d, e, W(75)); /* 76 */
358
II(e, a, b, c, d, W(76)); /* 77 */
359
II(d, e, a, b, c, W(77)); /* 78 */
360
II(c, d, e, a, b, W(78)); /* 79 */
361
II(b, c, d, e, a, W(79)); /* 80 */
369
/* Zeroize sensitive information. */
370
memset((unsigned char*) x, 0, sizeof(x));
375
Encodes input (php_uint32) into output (unsigned char). Assumes len is
378
static void SHA1Encode(output, input, len)
379
unsigned char *output;
385
for (i = 0, j = 0; j < len; i++, j += 4) {
386
output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
387
output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
388
output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
389
output[j + 3] = (unsigned char) (input[i] & 0xff);
395
Decodes input (unsigned char) into output (php_uint32). Assumes len is
398
static void SHA1Decode(output, input, len)
400
const unsigned char *input;
405
for (i = 0, j = 0; j < len; i++, j += 4)
406
output[i] = ((php_uint32) input[j + 3]) | (((php_uint32) input[j + 2]) << 8) |
407
(((php_uint32) input[j + 1]) << 16) | (((php_uint32) input[j]) << 24);
416
* vim600: sw=4 ts=4 fdm=marker