#include "php.h"
#include "sha1.h"
#include "md5.h"
PHPAPI void make_sha1_digest(char *sha1str, unsigned char *digest)
{
make_digest_ex(sha1str, digest, 20);
}
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);
}
}
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, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
return;
}
stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, 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
};
#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))
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#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)) )
#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); \
}
PHPAPI void PHP_SHA1Init(PHP_SHA1_CTX * context)
{
context->count[0] = context->count[1] = 0;
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
context->state[4] = 0xc3d2e1f0;
}
PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
unsigned int inputLen)
{
unsigned int i, index, partLen;
index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
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;
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;
memcpy
((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
inputLen - i);
}
PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
{
unsigned char bits[8];
unsigned int index, padLen;
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;
index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
PHP_SHA1Update(context, PADDING, padLen);
PHP_SHA1Update(context, bits, 8);
SHA1Encode(digest, context->state, 20);
memset((unsigned char*) context, 0, sizeof(*context));
}
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);
FF(a, b, c, d, e, x[0]);
FF(e, a, b, c, d, x[1]);
FF(d, e, a, b, c, x[2]);
FF(c, d, e, a, b, x[3]);
FF(b, c, d, e, a, x[4]);
FF(a, b, c, d, e, x[5]);
FF(e, a, b, c, d, x[6]);
FF(d, e, a, b, c, x[7]);
FF(c, d, e, a, b, x[8]);
FF(b, c, d, e, a, x[9]);
FF(a, b, c, d, e, x[10]);
FF(e, a, b, c, d, x[11]);
FF(d, e, a, b, c, x[12]);
FF(c, d, e, a, b, x[13]);
FF(b, c, d, e, a, x[14]);
FF(a, b, c, d, e, x[15]);
FF(e, a, b, c, d, W(16));
FF(d, e, a, b, c, W(17));
FF(c, d, e, a, b, W(18));
FF(b, c, d, e, a, W(19));
GG(a, b, c, d, e, W(20));
GG(e, a, b, c, d, W(21));
GG(d, e, a, b, c, W(22));
GG(c, d, e, a, b, W(23));
GG(b, c, d, e, a, W(24));
GG(a, b, c, d, e, W(25));
GG(e, a, b, c, d, W(26));
GG(d, e, a, b, c, W(27));
GG(c, d, e, a, b, W(28));
GG(b, c, d, e, a, W(29));
GG(a, b, c, d, e, W(30));
GG(e, a, b, c, d, W(31));
GG(d, e, a, b, c, W(32));
GG(c, d, e, a, b, W(33));
GG(b, c, d, e, a, W(34));
GG(a, b, c, d, e, W(35));
GG(e, a, b, c, d, W(36));
GG(d, e, a, b, c, W(37));
GG(c, d, e, a, b, W(38));
GG(b, c, d, e, a, W(39));
HH(a, b, c, d, e, W(40));
HH(e, a, b, c, d, W(41));
HH(d, e, a, b, c, W(42));
HH(c, d, e, a, b, W(43));
HH(b, c, d, e, a, W(44));
HH(a, b, c, d, e, W(45));
HH(e, a, b, c, d, W(46));
HH(d, e, a, b, c, W(47));
HH(c, d, e, a, b, W(48));
HH(b, c, d, e, a, W(49));
HH(a, b, c, d, e, W(50));
HH(e, a, b, c, d, W(51));
HH(d, e, a, b, c, W(52));
HH(c, d, e, a, b, W(53));
HH(b, c, d, e, a, W(54));
HH(a, b, c, d, e, W(55));
HH(e, a, b, c, d, W(56));
HH(d, e, a, b, c, W(57));
HH(c, d, e, a, b, W(58));
HH(b, c, d, e, a, W(59));
II(a, b, c, d, e, W(60));
II(e, a, b, c, d, W(61));
II(d, e, a, b, c, W(62));
II(c, d, e, a, b, W(63));
II(b, c, d, e, a, W(64));
II(a, b, c, d, e, W(65));
II(e, a, b, c, d, W(66));
II(d, e, a, b, c, W(67));
II(c, d, e, a, b, W(68));
II(b, c, d, e, a, W(69));
II(a, b, c, d, e, W(70));
II(e, a, b, c, d, W(71));
II(d, e, a, b, c, W(72));
II(c, d, e, a, b, W(73));
II(b, c, d, e, a, W(74));
II(a, b, c, d, e, W(75));
II(e, a, b, c, d, W(76));
II(d, e, a, b, c, W(77));
II(c, d, e, a, b, W(78));
II(b, c, d, e, a, W(79));
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
memset((unsigned char*) x, 0, sizeof(x));
}
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);
}
}
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);
}