Optimized digest generation in md5() and sha1() functions by using code

from ext/hash. This makes the functions 20-50% faster on short strings.
This commit is contained in:
Ilia Alshanetsky 2007-05-19 20:30:35 +00:00
parent a349109e42
commit b828d1939f
4 changed files with 18 additions and 17 deletions

1
NEWS
View File

@ -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)

View File

@ -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 @@ 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);
}
}

View 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 *);

View File

@ -23,17 +23,11 @@
/* 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);
}
}