iliaa Sat May 19 20:30:35 2007 UTC Modified files: (Branch: PHP_5_2) /php-src NEWS /php-src/ext/standard md5.c md5.h sha1.c Log: Optimized digest generation in md5() and sha1() functions by using code from ext/hash. This makes the functions 20-50% faster on short strings. http://cvs.php.net/viewvc.cgi/php-src/NEWS?r1=1.2027.2.547.2.722&r2=1.2027.2.547.2.723&diff_format=u Index: php-src/NEWS diff -u php-src/NEWS:1.2027.2.547.2.722 php-src/NEWS:1.2027.2.547.2.723 --- php-src/NEWS:1.2027.2.547.2.722 Sat May 19 08:10:52 2007 +++ php-src/NEWS Sat May 19 20:30:35 2007 @@ -3,6 +3,7 @@ ?? ??? 2007, PHP 5.2.3 - Improved compilation of heredocs and interpolated strings. (Matt, Dmitry) - Optimized out a couple of per-request syscalls (Rasmus) +- Optimized digest generation in md5() and sha1() functions. (Ilia) - Upgraded SQLite 3 to version 3.3.16 (Ilia) - Added PDO::FETCH_KEY_PAIR mode that will fetch a 2 column result set into an associated array. (Ilia) http://cvs.php.net/viewvc.cgi/php-src/ext/standard/md5.c?r1=1.39.2.1.2.1&r2=1.39.2.1.2.2&diff_format=u Index: php-src/ext/standard/md5.c diff -u php-src/ext/standard/md5.c:1.39.2.1.2.1 php-src/ext/standard/md5.c:1.39.2.1.2.2 --- php-src/ext/standard/md5.c:1.39.2.1.2.1 Mon Jan 1 09:36:08 2007 +++ php-src/ext/standard/md5.c Sat May 19 20:30:35 2007 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: md5.c,v 1.39.2.1.2.1 2007/01/01 09:36:08 sebastian Exp $ */ +/* $Id: md5.c,v 1.39.2.1.2.2 2007/05/19 20:30:35 iliaa Exp $ */ /* * md5.c - Copyright 1997 Lachlan Roche @@ -28,14 +28,19 @@ PHPAPI void make_digest(char *md5str, unsigned char *digest) { + return make_digest_ex(md5str, digest, strlen(digest)); +} + +PHPAPI void make_digest_ex(char *md5str, unsigned char *digest, int len) +{ + static const char hexits[17] = "0123456789abcdef"; int i; - for (i = 0; i < 16; i++) { - sprintf(md5str, "%02x", digest[i]); - md5str += 2; + for (i = 0; i < len; i++) { + md5str[i * 2] = hexits[digest[i] >> 4]; + md5str[(i * 2) + 1] = hexits[digest[i] & 0x0F]; } - - *md5str = '\0'; + md5str[len * 2] = '\0'; } /* {{{ proto string md5(string str, [ bool raw_output]) @@ -60,7 +65,7 @@ if (raw_output) { RETURN_STRINGL(digest, 16, 1); } else { - make_digest(md5str, digest); + make_digest_ex(md5str, digest, 16); RETVAL_STRING(md5str, 1); } @@ -107,7 +112,7 @@ if (raw_output) { RETURN_STRINGL(digest, 16, 1); } else { - make_digest(md5str, digest); + make_digest_ex(md5str, digest, 16); RETVAL_STRING(md5str, 1); } } http://cvs.php.net/viewvc.cgi/php-src/ext/standard/md5.h?r1=1.17.2.1.2.1&r2=1.17.2.1.2.2&diff_format=u Index: php-src/ext/standard/md5.h diff -u php-src/ext/standard/md5.h:1.17.2.1.2.1 php-src/ext/standard/md5.h:1.17.2.1.2.2 --- php-src/ext/standard/md5.h:1.17.2.1.2.1 Mon Jan 1 09:36:08 2007 +++ php-src/ext/standard/md5.h Sat May 19 20:30:35 2007 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: md5.h,v 1.17.2.1.2.1 2007/01/01 09:36:08 sebastian Exp $ */ +/* $Id: md5.h,v 1.17.2.1.2.2 2007/05/19 20:30:35 iliaa Exp $ */ #ifndef MD5_H #define MD5_H @@ -55,6 +55,7 @@ } PHP_MD5_CTX; PHPAPI void make_digest(char *md5str, unsigned char *digest); +PHPAPI void make_digest_ex(char *md5str, unsigned char *digest, int len); PHPAPI void PHP_MD5Init(PHP_MD5_CTX *); PHPAPI void PHP_MD5Update(PHP_MD5_CTX *, const unsigned char *, unsigned int); PHPAPI void PHP_MD5Final(unsigned char[16], PHP_MD5_CTX *); http://cvs.php.net/viewvc.cgi/php-src/ext/standard/sha1.c?r1=1.13.2.1.2.1&r2=1.13.2.1.2.2&diff_format=u Index: php-src/ext/standard/sha1.c diff -u php-src/ext/standard/sha1.c:1.13.2.1.2.1 php-src/ext/standard/sha1.c:1.13.2.1.2.2 --- php-src/ext/standard/sha1.c:1.13.2.1.2.1 Mon Jan 1 09:36:08 2007 +++ php-src/ext/standard/sha1.c Sat May 19 20:30:35 2007 @@ -16,24 +16,18 @@ +----------------------------------------------------------------------+ */ -/* $Id: sha1.c,v 1.13.2.1.2.1 2007/01/01 09:36:08 sebastian Exp $ */ +/* $Id: sha1.c,v 1.13.2.1.2.2 2007/05/19 20:30:35 iliaa Exp $ */ #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) { - int i; - - for (i = 0; i < 20; i++) { - sprintf(sha1str, "%02x", digest[i]); - sha1str += 2; - } - - *sha1str = '\0'; + return make_digest_ex(sha1str, digest, 20); } /* {{{ proto string sha1(string str [, bool raw_output]) @@ -58,7 +52,7 @@ if (raw_output) { RETURN_STRINGL(digest, 20, 1); } else { - make_sha1_digest(sha1str, digest); + make_digest_ex(sha1str, digest, 20); RETVAL_STRING(sha1str, 1); } @@ -107,7 +101,7 @@ if (raw_output) { RETURN_STRINGL(digest, 20, 1); } else { - make_sha1_digest(sha1str, digest); + make_digest_ex(sha1str, digest, 20); RETVAL_STRING(sha1str, 1); } }
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php