35 unsigned char digest[20];
64 unsigned char buf[1024];
65 unsigned char digest[20];
101static void SHA1Transform(uint32_t[5],
const unsigned char[64]);
102static void SHA1Encode(
unsigned char *, uint32_t *,
unsigned int);
103static void SHA1Decode(uint32_t *,
const unsigned char *,
unsigned int);
105static const unsigned char PADDING[64] =
107 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
114#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
115#define G(x, y, z) ((x) ^ (y) ^ (z))
116#define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
117#define I(x, y, z) ((x) ^ (y) ^ (z))
121#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
125#define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
126 (x[i&15]=ROTATE_LEFT(tmp, 1)) )
130#define FF(a, b, c, d, e, w) { \
131 (e) += F ((b), (c), (d)) + (w) + (uint32_t)(0x5A827999); \
132 (e) += ROTATE_LEFT ((a), 5); \
133 (b) = ROTATE_LEFT((b), 30); \
135#define GG(a, b, c, d, e, w) { \
136 (e) += G ((b), (c), (d)) + (w) + (uint32_t)(0x6ED9EBA1); \
137 (e) += ROTATE_LEFT ((a), 5); \
138 (b) = ROTATE_LEFT((b), 30); \
140#define HH(a, b, c, d, e, w) { \
141 (e) += H ((b), (c), (d)) + (w) + (uint32_t)(0x8F1BBCDC); \
142 (e) += ROTATE_LEFT ((a), 5); \
143 (b) = ROTATE_LEFT((b), 30); \
145#define II(a, b, c, d, e, w) { \
146 (e) += I ((b), (c), (d)) + (w) + (uint32_t)(0xCA62C1D6); \
147 (e) += ROTATE_LEFT ((a), 5); \
148 (b) = ROTATE_LEFT((b), 30); \
160 context->state[0] = 0x67452301;
161 context->state[1] = 0xefcdab89;
162 context->state[2] = 0x98badcfe;
163 context->state[3] = 0x10325476;
164 context->state[4] = 0xc3d2e1f0;
176 unsigned int index, partLen;
180 index = (
unsigned int) ((
context->count[0] >> 3) & 0x3F);
183 if ((
context->count[0] += ((uint32_t) inputLen << 3))
184 < ((uint32_t) inputLen << 3))
186 context->count[1] += (uint32_t) (inputLen >> 29);
188 partLen = 64 - index;
192 if (inputLen >= partLen) {
194 ((
unsigned char*) &
context->buffer[index], (
unsigned char*) input, partLen);
197 for (i = partLen; i + 63 < inputLen; i += 64)
198 SHA1Transform(
context->state, &input[i]);
206 ((
unsigned char*) &
context->buffer[index], (
unsigned char*) & input[i],
217 unsigned char bits[8];
218 unsigned int index, padLen;
221 bits[7] =
context->count[0] & 0xFF;
222 bits[6] = (
context->count[0] >> 8) & 0xFF;
223 bits[5] = (
context->count[0] >> 16) & 0xFF;
224 bits[4] = (
context->count[0] >> 24) & 0xFF;
225 bits[3] =
context->count[1] & 0xFF;
226 bits[2] = (
context->count[1] >> 8) & 0xFF;
227 bits[1] = (
context->count[1] >> 16) & 0xFF;
228 bits[0] = (
context->count[1] >> 24) & 0xFF;
232 index = (
unsigned int) ((
context->count[0] >> 3) & 0x3f);
233 padLen = (index < 56) ? (56 - index) : (120 - index);
240 SHA1Encode(digest,
context->state, 20);
251static void SHA1Transform(uint32_t
state[5],
const unsigned char block[64])
254 uint32_t d =
state[3], e =
state[4], x[16], tmp;
256 SHA1Decode(x, block, 64);
259 FF(
a, b, c, d, e, x[0]);
260 FF(e,
a, b, c, d, x[1]);
261 FF(d, e,
a, b, c, x[2]);
262 FF(c, d, e,
a, b, x[3]);
263 FF(b, c, d, e,
a, x[4]);
264 FF(
a, b, c, d, e, x[5]);
265 FF(e,
a, b, c, d, x[6]);
266 FF(d, e,
a, b, c, x[7]);
267 FF(c, d, e,
a, b, x[8]);
268 FF(b, c, d, e,
a, x[9]);
269 FF(
a, b, c, d, e, x[10]);
270 FF(e,
a, b, c, d, x[11]);
271 FF(d, e,
a, b, c, x[12]);
272 FF(c, d, e,
a, b, x[13]);
273 FF(b, c, d, e,
a, x[14]);
274 FF(
a, b, c, d, e, x[15]);
275 FF(e,
a, b, c, d,
W(16));
276 FF(d, e,
a, b, c,
W(17));
277 FF(c, d, e,
a, b,
W(18));
278 FF(b, c, d, e,
a,
W(19));
281 GG(
a, b, c, d, e,
W(20));
282 GG(e,
a, b, c, d,
W(21));
283 GG(d, e,
a, b, c,
W(22));
284 GG(c, d, e,
a, b,
W(23));
285 GG(b, c, d, e,
a,
W(24));
286 GG(
a, b, c, d, e,
W(25));
287 GG(e,
a, b, c, d,
W(26));
288 GG(d, e,
a, b, c,
W(27));
289 GG(c, d, e,
a, b,
W(28));
290 GG(b, c, d, e,
a,
W(29));
291 GG(
a, b, c, d, e,
W(30));
292 GG(e,
a, b, c, d,
W(31));
293 GG(d, e,
a, b, c,
W(32));
294 GG(c, d, e,
a, b,
W(33));
295 GG(b, c, d, e,
a,
W(34));
296 GG(
a, b, c, d, e,
W(35));
297 GG(e,
a, b, c, d,
W(36));
298 GG(d, e,
a, b, c,
W(37));
299 GG(c, d, e,
a, b,
W(38));
300 GG(b, c, d, e,
a,
W(39));
303 HH(
a, b, c, d, e,
W(40));
304 HH(e,
a, b, c, d,
W(41));
305 HH(d, e,
a, b, c,
W(42));
306 HH(c, d, e,
a, b,
W(43));
307 HH(b, c, d, e,
a,
W(44));
308 HH(
a, b, c, d, e,
W(45));
309 HH(e,
a, b, c, d,
W(46));
310 HH(d, e,
a, b, c,
W(47));
311 HH(c, d, e,
a, b,
W(48));
312 HH(b, c, d, e,
a,
W(49));
313 HH(
a, b, c, d, e,
W(50));
314 HH(e,
a, b, c, d,
W(51));
315 HH(d, e,
a, b, c,
W(52));
316 HH(c, d, e,
a, b,
W(53));
317 HH(b, c, d, e,
a,
W(54));
318 HH(
a, b, c, d, e,
W(55));
319 HH(e,
a, b, c, d,
W(56));
320 HH(d, e,
a, b, c,
W(57));
321 HH(c, d, e,
a, b,
W(58));
322 HH(b, c, d, e,
a,
W(59));
325 II(
a, b, c, d, e,
W(60));
326 II(e,
a, b, c, d,
W(61));
327 II(d, e,
a, b, c,
W(62));
328 II(c, d, e,
a, b,
W(63));
329 II(b, c, d, e,
a,
W(64));
330 II(
a, b, c, d, e,
W(65));
331 II(e,
a, b, c, d,
W(66));
332 II(d, e,
a, b, c,
W(67));
333 II(c, d, e,
a, b,
W(68));
334 II(b, c, d, e,
a,
W(69));
335 II(
a, b, c, d, e,
W(70));
336 II(e,
a, b, c, d,
W(71));
337 II(d, e,
a, b, c,
W(72));
338 II(c, d, e,
a, b,
W(73));
339 II(b, c, d, e,
a,
W(74));
340 II(
a, b, c, d, e,
W(75));
341 II(e,
a, b, c, d,
W(76));
342 II(d, e,
a, b, c,
W(77));
343 II(c, d, e,
a, b,
W(78));
344 II(b, c, d, e,
a,
W(79));
361static void SHA1Encode(
unsigned char *output, uint32_t *input,
unsigned int len)
365 for (i = 0,
j = 0;
j <
len; i++,
j += 4) {
366 output[
j] = (
unsigned char) ((input[i] >> 24) & 0xff);
367 output[
j + 1] = (
unsigned char) ((input[i] >> 16) & 0xff);
368 output[
j + 2] = (
unsigned char) ((input[i] >> 8) & 0xff);
369 output[
j + 3] = (
unsigned char) (input[i] & 0xff);
378static void SHA1Decode(uint32_t *output,
const unsigned char *input,
unsigned int len)
382 for (i = 0,
j = 0;
j <
len; i++,
j += 4)
383 output[i] = ((uint32_t) input[
j + 3]) | (((uint32_t) input[
j + 2]) << 8) |
384 (((uint32_t) input[
j + 1]) << 16) | (((uint32_t) input[
j]) << 24);
sha1_file(string $filename, bool $binary=false)
sha1(string $string, bool $binary=false)
zend_ffi_ctype_name_buf buf
PHPAPI void make_digest_ex(char *md5str, const unsigned char *digest, int len)
struct _php_stream php_stream
#define php_stream_read(stream, buf, count)
#define php_stream_close(stream)
#define php_stream_open_wrapper(path, mode, options, opened)
#define HH(a, b, c, d, e, w)
PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX *context)
PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX *context, const unsigned char *input, size_t inputLen)
#define FF(a, b, c, d, e, w)
PHPAPI void make_sha1_digest(char *sha1str, const unsigned char *digest)
#define II(a, b, c, d, e, w)
PHPAPI void PHP_SHA1InitArgs(PHP_SHA1_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
#define GG(a, b, c, d, e, w)
#define PHP_SHA1Init(ctx)
#define RETURN_STRINGL(s, l)
#define ZEND_PARSE_PARAMETERS_END()
#define RETVAL_NEW_STR(s)
#define Z_PARAM_STR(dest)
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
#define Z_PARAM_BOOL(dest)
#define Z_PARAM_PATH(dest, dest_len)
struct _zend_string zend_string
#define ZEND_ATTRIBUTE_UNUSED
#define ZEND_SECURE_ZERO(var, size)
#define Z_STRVAL_P(zval_p)
struct _zend_array HashTable