]> granicus.if.org Git - php/commitdiff
Optimized digest generation in md5() and sha1() functions by using code
authorIlia Alshanetsky <iliaa@php.net>
Sat, 19 May 2007 20:30:35 +0000 (20:30 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Sat, 19 May 2007 20:30:35 +0000 (20:30 +0000)
from ext/hash. This makes the functions 20-50% faster on short strings.

NEWS
ext/standard/md5.c
ext/standard/md5.h
ext/standard/sha1.c

diff --git a/NEWS b/NEWS
index 03f1cce6004765c2d15619664e0241aa199a5a82..6a7d8d0a2840599938422a9888dcedd35b396738 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ PHP                                                                        NEWS
 ?? ??? 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)
index e1bf0843171296f3ccc7f22f3d06e7b70efb6475..3cf5fbaaea73e2502030106d8256d9ee523f533e 100644 (file)
 
 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 @@ PHP_NAMED_FUNCTION(php_if_md5)
        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 @@ PHP_NAMED_FUNCTION(php_if_md5_file)
        if (raw_output) {
                RETURN_STRINGL(digest, 16, 1);
        } else {
-               make_digest(md5str, digest);
+               make_digest_ex(md5str, digest, 16);
                RETVAL_STRING(md5str, 1);
        }
 }
index 82f2e75a65b75d3b112a8e11a11852af5a2cf0af..6926ee4fde6779ccecd6e62330c61ae6cf75b38d 100644 (file)
@@ -55,6 +55,7 @@ typedef struct {
 } 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 *);
index 5cd0a9201e074ddced86869937399c32a3e88ca2..5f1ac43a5e2ec19b718d7e3303999078f95b2084 100644 (file)
 /* 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 @@ PHP_FUNCTION(sha1)
        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 @@ PHP_FUNCTION(sha1_file)
        if (raw_output) {
                RETURN_STRINGL(digest, 20, 1);
        } else {
-               make_sha1_digest(sha1str, digest);
+               make_digest_ex(sha1str, digest, 20);
                RETVAL_STRING(sha1str, 1);
        }
 }