Skip to content

Commit 3ea9c13

Browse files
committed
Improved md5() implementation (Solar Designer)
1 parent 54680a3 commit 3ea9c13

File tree

5 files changed

+286
-332
lines changed

5 files changed

+286
-332
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ PHP NEWS
102102
. Added support for OpenSSL digest functions
103103
. Added support for OpenSSL cipher functions
104104
. Added access to internal values of DSA, RSA and DH keys
105+
- Imoroved md5() implementation (Solar Designer, Dmitry)
105106

106107
- Fixed html_entity_decode() incorrectly converting numeric html entities
107108
to different characters with cp1251 and cp866. (Scott)

ext/hash/hash_md.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ const unsigned char block[64];
442442

443443
/* MD4 */
444444

445-
#define MD4_F(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
446-
#define MD4_G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
445+
#define MD4_F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
446+
#define MD4_G(x,y,z) (((x) & ((y) | (z))) | ((y) & (z)))
447447
#define MD4_H(x,y,z) ((x) ^ (y) ^ (z))
448448

449449
#define ROTL32(s,v) (((v) << (s)) | ((v) >> (32 - (s))))
@@ -518,8 +518,23 @@ static void MD4Transform(php_hash_uint32 state[4], const unsigned char block[64]
518518
state[3] += d;
519519
}
520520

521+
/* {{{ PHP_MD4Init
522+
* MD4 initialization. Begins an MD4 operation, writing a new context.
523+
*/
524+
PHP_HASH_API void PHP_MD4Init(PHP_MD4_CTX * context)
525+
{
526+
context->count[0] = context->count[1] = 0;
527+
/* Load magic initialization constants.
528+
*/
529+
context->state[0] = 0x67452301;
530+
context->state[1] = 0xefcdab89;
531+
context->state[2] = 0x98badcfe;
532+
context->state[3] = 0x10325476;
533+
}
534+
/* }}} */
535+
521536
/* {{{ PHP_MD4Update
522-
MD4 block update operation. Continues an MD5 message-digest
537+
MD4 block update operation. Continues an MD4 message-digest
523538
operation, processing another message block, and updating the
524539
context.
525540
*/

ext/hash/php_hash_md.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ typedef struct {
8181
unsigned char buffer[64];
8282
} PHP_MD4_CTX;
8383

84-
#define PHP_MD4Init PHP_MD5Init
84+
PHP_HASH_API void PHP_MD4Init(PHP_MD4_CTX *);
8585
PHP_HASH_API void PHP_MD4Update(PHP_MD4_CTX *context, const unsigned char *, unsigned int);
8686
PHP_HASH_API void PHP_MD4Final(unsigned char[16], PHP_MD4_CTX *);
8787

0 commit comments

Comments
 (0)