/* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2016 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Stefan Esser <sesser@php.net> | +----------------------------------------------------------------------+ */ /* $Id$ */ #include "php.h" /* This code is heavily based on the PHP md5 implementation */ #include "sha1.h" #include "md5.h" PHPAPI void make_sha1_digest(char *sha1str, unsigned char *digest) { make_digest_ex(sha1str, digest, 20); } /* {{{ proto string sha1(string str [, bool raw_output]) Calculate the sha1 hash of a string */ PHP_FUNCTION(sha1) { char *arg; int arg_len; zend_bool raw_output = 0; char sha1str[41]; PHP_SHA1_CTX context; unsigned char digest[20]; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) { return; } sha1str[0] = '\0'; PHP_SHA1Init(&context); PHP_SHA1Update(&context, arg, arg_len); PHP_SHA1Final(digest, &context); if (raw_output) { RETURN_STRINGL(digest, 20, 1); } else { make_digest_ex(sha1str, digest, 20); RETVAL_STRING(sha1str, 1); } } /* }}} */ /* {{{ proto string sha1_file(string filename [, bool raw_output]) Calculate the sha1 hash of given filename */ PHP_FUNCTION(sha1_file) { char *arg; int arg_len; zend_bool raw_output = 0; char sha1str[41]; unsigned char buf[1024]; unsigned char digest[20]; PHP_SHA1_CTX context; int n; php_stream *stream; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|b", &arg, &arg_len, &raw_output) == FAILURE) { return; } stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL); if (!stream) { RETURN_FALSE; } PHP_SHA1Init(&context); while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) { PHP_SHA1Update(&context, buf, n); } PHP_SHA1Final(digest, &context); php_stream_close(stream); if (n<0) { RETURN_FALSE; } if (raw_output) { RETURN_STRINGL(digest, 20, 1); } else { make_digest_ex(sha1str, digest, 20); RETVAL_STRING(sha1str, 1); } } /* }}} */ static void SHA1Transform(php_uint32[5], const unsigned char[64]); static void SHA1Encode(unsigned char *, php_uint32 *, unsigned int); static void SHA1Decode(php_uint32 *, const unsigned char *, unsigned int); static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* F, G, H and I are basic SHA1 functions. */ #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) #define G(x, y, z) ((x) ^ (y) ^ (z)) #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y)))) #define I(x, y, z) ((x) ^ (y) ^ (z)) /* ROTATE_LEFT rotates x left n bits. */ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) /* W[i] */ #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \ (x[i&15]=ROTATE_LEFT(tmp, 1)) ) /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. */ #define FF(a, b, c, d, e, w) { \ (e) += F ((b), (c), (d)) + (w) + (php_uint32)(0x5A827999); \ (e) += ROTATE_LEFT ((a), 5); \ (b) = ROTATE_LEFT((b), 30); \ } #define GG(a, b, c, d, e, w) { \ (e) += G ((b), (c), (d)) + (w) + (php_uint32)(0x6ED9EBA1); \ (e) += ROTATE_LEFT ((a), 5); \ (b) = ROTATE_LEFT((b), 30); \ } #define HH(a, b, c, d, e, w) { \ (e) += H ((b), (c), (d)) + (w) + (php_uint32)(0x8F1BBCDC); \ (e) += ROTATE_LEFT ((a), 5); \ (b) = ROTATE_LEFT((b), 30); \ } #define II(a, b, c, d, e, w) { \ (e) += I ((b), (c), (d)) + (w) + (php_uint32)(0xCA62C1D6); \ (e) += ROTATE_LEFT ((a), 5); \ (b) = ROTATE_LEFT((b), 30); \ } /* {{{ PHP_SHA1Init * SHA1 initialization. Begins an SHA1 operation, writing a new context. */ PHPAPI void PHP_SHA1Init(PHP_SHA1_CTX * context) { context->count[0] = context->count[1] = 0; /* Load magic initialization constants. */ context->state[0] = 0x67452301; context->state[1] = 0xefcdab89; context->state[2] = 0x98badcfe; context->state[3] = 0x10325476; context->state[4] = 0xc3d2e1f0; } /* }}} */ /* {{{ PHP_SHA1Update SHA1 block update operation. Continues an SHA1 message-digest operation, processing another message block, and updating the context. */ PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input, unsigned int inputLen) { unsigned int i, index, partLen; /* Compute number of bytes mod 64 */ index = (unsigned int) ((context->count[0] >> 3) & 0x3F); /* Update number of bits */ if ((context->count[0] += ((php_uint32) inputLen << 3)) < ((php_uint32) inputLen << 3)) context->count[1]++; context->count[1] += ((php_uint32) inputLen >> 29); partLen = 64 - index; /* Transform as many times as possible. */ if (inputLen >= partLen) { memcpy ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen); SHA1Transform(context->state, context->buffer); for (i = partLen; i + 63 < inputLen; i += 64) SHA1Transform(context->state, &input[i]); index = 0; } else i = 0; /* Buffer remaining input */ memcpy ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i); } /* }}} */ /* {{{ PHP_SHA1Final SHA1 finalization. Ends an SHA1 message-digest operation, writing the the message digest and zeroizing the context. */ PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context) { unsigned char bits[8]; unsigned int index, padLen; /* Save number of bits */ bits[7] = context->count[0] & 0xFF; bits[6] = (context->count[0] >> 8) & 0xFF; bits[5] = (context->count[0] >> 16) & 0xFF; bits[4] = (context->count[0] >> 24) & 0xFF; bits[3] = context->count[1] & 0xFF; bits[2] = (context->count[1] >> 8) & 0xFF; bits[1] = (context->count[1] >> 16) & 0xFF; bits[0] = (context->count[1] >> 24) & 0xFF; /* Pad out to 56 mod 64. */ index = (unsigned int) ((context->count[0] >> 3) & 0x3f); padLen = (index < 56) ? (56 - index) : (120 - index); PHP_SHA1Update(context, PADDING, padLen); /* Append length (before padding) */ PHP_SHA1Update(context, bits, 8); /* Store state in digest */ SHA1Encode(digest, context->state, 20); /* Zeroize sensitive information. */ memset((unsigned char*) context, 0, sizeof(*context)); } /* }}} */ /* {{{ SHA1Transform * SHA1 basic transformation. Transforms state based on block. */ static void SHA1Transform(state, block) php_uint32 state[5]; const unsigned char block[64]; { php_uint32 a = state[0], b = state[1], c = state[2]; php_uint32 d = state[3], e = state[4], x[16], tmp; SHA1Decode(x, block, 64); /* Round 1 */ FF(a, b, c, d, e, x[0]); /* 1 */ FF(e, a, b, c, d, x[1]); /* 2 */ FF(d, e, a, b, c, x[2]); /* 3 */ FF(c, d, e, a, b, x[3]); /* 4 */ FF(b, c, d, e, a, x[4]); /* 5 */ FF(a, b, c, d, e, x[5]); /* 6 */ FF(e, a, b, c, d, x[6]); /* 7 */ FF(d, e, a, b, c, x[7]); /* 8 */ FF(c, d, e, a, b, x[8]); /* 9 */ FF(b, c, d, e, a, x[9]); /* 10 */ FF(a, b, c, d, e, x[10]); /* 11 */ FF(e, a, b, c, d, x[11]); /* 12 */ FF(d, e, a, b, c, x[12]); /* 13 */ FF(c, d, e, a, b, x[13]); /* 14 */ FF(b, c, d, e, a, x[14]); /* 15 */ FF(a, b, c, d, e, x[15]); /* 16 */ FF(e, a, b, c, d, W(16)); /* 17 */ FF(d, e, a, b, c, W(17)); /* 18 */ FF(c, d, e, a, b, W(18)); /* 19 */ FF(b, c, d, e, a, W(19)); /* 20 */ /* Round 2 */ GG(a, b, c, d, e, W(20)); /* 21 */ GG(e, a, b, c, d, W(21)); /* 22 */ GG(d, e, a, b, c, W(22)); /* 23 */ GG(c, d, e, a, b, W(23)); /* 24 */ GG(b, c, d, e, a, W(24)); /* 25 */ GG(a, b, c, d, e, W(25)); /* 26 */ GG(e, a, b, c, d, W(26)); /* 27 */ GG(d, e, a, b, c, W(27)); /* 28 */ GG(c, d, e, a, b, W(28)); /* 29 */ GG(b, c, d, e, a, W(29)); /* 30 */ GG(a, b, c, d, e, W(30)); /* 31 */ GG(e, a, b, c, d, W(31)); /* 32 */ GG(d, e, a, b, c, W(32)); /* 33 */ GG(c, d, e, a, b, W(33)); /* 34 */ GG(b, c, d, e, a, W(34)); /* 35 */ GG(a, b, c, d, e, W(35)); /* 36 */ GG(e, a, b, c, d, W(36)); /* 37 */ GG(d, e, a, b, c, W(37)); /* 38 */ GG(c, d, e, a, b, W(38)); /* 39 */ GG(b, c, d, e, a, W(39)); /* 40 */ /* Round 3 */ HH(a, b, c, d, e, W(40)); /* 41 */ HH(e, a, b, c, d, W(41)); /* 42 */ HH(d, e, a, b, c, W(42)); /* 43 */ HH(c, d, e, a, b, W(43)); /* 44 */ HH(b, c, d, e, a, W(44)); /* 45 */ HH(a, b, c, d, e, W(45)); /* 46 */ HH(e, a, b, c, d, W(46)); /* 47 */ HH(d, e, a, b, c, W(47)); /* 48 */ HH(c, d, e, a, b, W(48)); /* 49 */ HH(b, c, d, e, a, W(49)); /* 50 */ HH(a, b, c, d, e, W(50)); /* 51 */ HH(e, a, b, c, d, W(51)); /* 52 */ HH(d, e, a, b, c, W(52)); /* 53 */ HH(c, d, e, a, b, W(53)); /* 54 */ HH(b, c, d, e, a, W(54)); /* 55 */ HH(a, b, c, d, e, W(55)); /* 56 */ HH(e, a, b, c, d, W(56)); /* 57 */ HH(d, e, a, b, c, W(57)); /* 58 */ HH(c, d, e, a, b, W(58)); /* 59 */ HH(b, c, d, e, a, W(59)); /* 60 */ /* Round 4 */ II(a, b, c, d, e, W(60)); /* 61 */ II(e, a, b, c, d, W(61)); /* 62 */ II(d, e, a, b, c, W(62)); /* 63 */ II(c, d, e, a, b, W(63)); /* 64 */ II(b, c, d, e, a, W(64)); /* 65 */ II(a, b, c, d, e, W(65)); /* 66 */ II(e, a, b, c, d, W(66)); /* 67 */ II(d, e, a, b, c, W(67)); /* 68 */ II(c, d, e, a, b, W(68)); /* 69 */ II(b, c, d, e, a, W(69)); /* 70 */ II(a, b, c, d, e, W(70)); /* 71 */ II(e, a, b, c, d, W(71)); /* 72 */ II(d, e, a, b, c, W(72)); /* 73 */ II(c, d, e, a, b, W(73)); /* 74 */ II(b, c, d, e, a, W(74)); /* 75 */ II(a, b, c, d, e, W(75)); /* 76 */ II(e, a, b, c, d, W(76)); /* 77 */ II(d, e, a, b, c, W(77)); /* 78 */ II(c, d, e, a, b, W(78)); /* 79 */ II(b, c, d, e, a, W(79)); /* 80 */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; /* Zeroize sensitive information. */ memset((unsigned char*) x, 0, sizeof(x)); } /* }}} */ /* {{{ SHA1Encode Encodes input (php_uint32) into output (unsigned char). Assumes len is a multiple of 4. */ static void SHA1Encode(output, input, len) unsigned char *output; php_uint32 *input; unsigned int len; { unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) { output[j] = (unsigned char) ((input[i] >> 24) & 0xff); output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff); output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff); output[j + 3] = (unsigned char) (input[i] & 0xff); } } /* }}} */ /* {{{ SHA1Decode Decodes input (unsigned char) into output (php_uint32). Assumes len is a multiple of 4. */ static void SHA1Decode(output, input, len) php_uint32 *output; const unsigned char *input; unsigned int len; { unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) output[i] = ((php_uint32) input[j + 3]) | (((php_uint32) input[j + 2]) << 8) | (((php_uint32) input[j + 1]) << 16) | (((php_uint32) input[j]) << 24); } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
array.c | File | 130.19 KB | 0644 |
|
assert.c | File | 9.31 KB | 0644 |
|
base64.c | File | 7.73 KB | 0644 |
|
base64.h | File | 1.57 KB | 0644 |
|
basic_functions.c | File | 169.08 KB | 0644 |
|
basic_functions.h | File | 7.42 KB | 0644 |
|
browscap.c | File | 16.82 KB | 0644 |
|
crc32.c | File | 1.77 KB | 0644 |
|
crc32.h | File | 4.78 KB | 0644 |
|
credits.c | File | 5.91 KB | 0644 |
|
credits.h | File | 1.7 KB | 0644 |
|
credits_ext.h | File | 5.51 KB | 0644 |
|
credits_sapi.h | File | 1.63 KB | 0644 |
|
crypt.c | File | 8.43 KB | 0644 |
|
crypt_blowfish.c | File | 31.68 KB | 0644 |
|
crypt_blowfish.h | File | 1.05 KB | 0644 |
|
crypt_freesec.c | File | 21.64 KB | 0644 |
|
crypt_freesec.h | File | 662 B | 0644 |
|
crypt_sha256.c | File | 21.77 KB | 0644 |
|
crypt_sha512.c | File | 26.45 KB | 0644 |
|
css.c | File | 2.43 KB | 0644 |
|
css.h | File | 1.21 KB | 0644 |
|
cyr_convert.c | File | 11.56 KB | 0644 |
|
datetime.c | File | 3.85 KB | 0644 |
|
dir.c | File | 15.08 KB | 0644 |
|
dl.c | File | 9.18 KB | 0644 |
|
dl.h | File | 1.57 KB | 0644 |
|
dns.c | File | 27.68 KB | 0644 |
|
exec.c | File | 13.13 KB | 0644 |
|
exec.h | File | 1.69 KB | 0644 |
|
file.c | File | 68.46 KB | 0644 |
|
file.h | File | 4.63 KB | 0644 |
|
filestat.c | File | 34.39 KB | 0644 |
|
filters.c | File | 56.51 KB | 0644 |
|
flock_compat.c | File | 6.9 KB | 0644 |
|
formatted_print.c | File | 20.19 KB | 0644 |
|
fsock.c | File | 3.89 KB | 0644 |
|
ftok.c | File | 2.22 KB | 0644 |
|
ftp_fopen_wrapper.c | File | 32.1 KB | 0644 |
|
head.c | File | 9.18 KB | 0644 |
|
head.h | File | 1.62 KB | 0644 |
|
html.c | File | 48.15 KB | 0644 |
|
html.h | File | 2.71 KB | 0644 |
|
html_tables.h | File | 471.57 KB | 0644 |
|
http.c | File | 7.7 KB | 0644 |
|
http_fopen_wrapper.c | File | 33.65 KB | 0644 |
|
image.c | File | 40.83 KB | 0644 |
|
incomplete_class.c | File | 5.61 KB | 0644 |
|
info.c | File | 44.03 KB | 0644 |
|
info.h | File | 20.2 KB | 0644 |
|
iptc.c | File | 9.85 KB | 0644 |
|
lcg.c | File | 3.11 KB | 0644 |
|
levenshtein.c | File | 4.05 KB | 0644 |
|
link.c | File | 5.83 KB | 0644 |
|
mail.c | File | 13.74 KB | 0644 |
|
math.c | File | 29.12 KB | 0644 |
|
md5.c | File | 10.65 KB | 0644 |
|
md5.h | File | 2.12 KB | 0644 |
|
metaphone.c | File | 11.84 KB | 0644 |
|
microtime.c | File | 4.36 KB | 0644 |
|
pack.c | File | 27.05 KB | 0644 |
|
pack.h | File | 1.25 KB | 0644 |
|
pageinfo.c | File | 3.92 KB | 0644 |
|
password.c | File | 12.06 KB | 0644 |
|
php_array.h | File | 4.62 KB | 0644 |
|
php_assert.h | File | 1.4 KB | 0644 |
|
php_browscap.h | File | 1.3 KB | 0644 |
|
php_crypt.h | File | 1.63 KB | 0644 |
|
php_crypt_r.c | File | 10.78 KB | 0644 |
|
php_crypt_r.h | File | 2 KB | 0644 |
|
php_dir.h | File | 1.67 KB | 0644 |
|
php_dns.h | File | 2.82 KB | 0644 |
|
php_ext_syslog.h | File | 1.47 KB | 0644 |
|
php_filestat.h | File | 3.28 KB | 0644 |
|
php_fopen_wrapper.c | File | 11.49 KB | 0644 |
|
php_fopen_wrappers.h | File | 1.92 KB | 0644 |
|
php_image.h | File | 2.37 KB | 0644 |
|
php_incomplete_class.h | File | 2.47 KB | 0644 |
|
php_lcg.h | File | 1.5 KB | 0644 |
|
php_mail.h | File | 1.37 KB | 0644 |
|
php_password.h | File | 1.58 KB | 0644 |
|
php_rand.h | File | 2.56 KB | 0644 |
|
php_smart_str.h | File | 6.57 KB | 0644 |
|
php_smart_str_public.h | File | 1.29 KB | 0644 |
|
php_standard.h | File | 2.21 KB | 0644 |
|
php_string.h | File | 6.23 KB | 0644 |
|
php_var.h | File | 7.33 KB | 0644 |
|
php_versioning.h | File | 1.37 KB | 0644 |
|
proc_open.c | File | 26 KB | 0644 |
|
proc_open.h | File | 1.81 KB | 0644 |
|
quot_print.c | File | 7.51 KB | 0644 |
|
quot_print.h | File | 1.51 KB | 0644 |
|
rand.c | File | 11.01 KB | 0644 |
|
scanf.c | File | 29.45 KB | 0644 |
|
scanf.h | File | 2.27 KB | 0644 |
|
sha1.c | File | 11.58 KB | 0644 |
|
sha1.h | File | 1.71 KB | 0644 |
|
soundex.c | File | 3.29 KB | 0644 |
|
streamsfuncs.c | File | 45.17 KB | 0644 |
|
string.c | File | 135.19 KB | 0644 |
|
strnatcmp.c | File | 4.57 KB | 0644 |
|
syslog.c | File | 6.35 KB | 0644 |
|
type.c | File | 9.06 KB | 0644 |
|
uniqid.c | File | 2.62 KB | 0644 |
|
url.c | File | 17.9 KB | 0644 |
|
url.h | File | 2.28 KB | 0644 |
|
url_scanner_ex.c | File | 27.89 KB | 0644 |
|
url_scanner_ex.h | File | 2.09 KB | 0644 |
|
user_filters.c | File | 18.47 KB | 0644 |
|
uuencode.c | File | 6.63 KB | 0644 |
|
var.c | File | 29.15 KB | 0644 |
|
var_unserializer.c | File | 29.18 KB | 0644 |
|
versioning.c | File | 5.87 KB | 0644 |
|