diff options
author | hemagx <ibrahem.h.basyone@gmail.com> | 2017-01-10 01:41:12 +0200 |
---|---|---|
committer | hemagx <ibrahem.h.basyone@gmail.com> | 2017-01-10 01:41:12 +0200 |
commit | 89e976aec097a2422f6faa08d4c8b0780df13acd (patch) | |
tree | 9db12a0b8dfd1877eeb17c1a035691ccc3a28630 /src | |
parent | dbc54eaa7e6091110e6437066b49a7cf8faea2b8 (diff) | |
download | hercules-89e976aec097a2422f6faa08d4c8b0780df13acd.tar.gz hercules-89e976aec097a2422f6faa08d4c8b0780df13acd.tar.bz2 hercules-89e976aec097a2422f6faa08d4c8b0780df13acd.tar.xz hercules-89e976aec097a2422f6faa08d4c8b0780df13acd.zip |
Update md5 calculation to be able to hash a binary buffer instead of only strings
Signed-off-by: hemagx <ibrahem.h.basyone@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/common/md5calc.c | 32 | ||||
-rw-r--r-- | src/common/md5calc.h | 2 |
2 files changed, 16 insertions, 18 deletions
diff --git a/src/common/md5calc.c b/src/common/md5calc.c index bd6b48f10..d2fc32371 100644 --- a/src/common/md5calc.c +++ b/src/common/md5calc.c @@ -168,16 +168,15 @@ static void md5_Round_Calculate(const unsigned char *block, } /// @copydoc md5_interface::binary() -static void md5_string2binary(const char *string, unsigned char *output) +static void md5_buf2binary(const uint8 *buf, const int buf_size, uint8 *output) { //var /*8bit*/ unsigned char padding_message[64]; //Extended message 512bit 64byte - const unsigned char *pstring; // The position of string in the present scanning notes is held. + const uint8 *pbuf; // The position of string in the present scanning notes is held. /*32bit*/ - unsigned int string_byte_len, //The byte chief of string is held. - string_bit_len, //The bit length of string is held. + unsigned int buf_bit_len, //The bit length of string is held. copy_len, //The number of bytes which is used by 1-3 and which remained msg_digest[4]; //Message digest 128bit 4byte unsigned int *A = &msg_digest[0], //The message digest in accordance with RFC (reference) @@ -195,16 +194,15 @@ static void md5_string2binary(const char *string, unsigned char *output) //Step 1.Append Padding Bits (extension of a mark bit) //1-1 - string_byte_len = (unsigned int)strlen(string); //The byte chief of a character sequence is acquired. - pstring = (const unsigned char *)string; // The position of the present character sequence is set. + pbuf = buf; // The position of the present character sequence is set. //1-2 Repeat calculation until length becomes less than 64 bytes. - for (i=string_byte_len; 64<=i; i-=64,pstring+=64) - md5_Round_Calculate(pstring, A,B,C,D); + for (i=buf_size; 64<=i; i-=64,pbuf+=64) + md5_Round_Calculate(pbuf, A,B,C,D); //1-3 - copy_len = string_byte_len % 64; //The number of bytes which remained is computed. - strncpy((char *)padding_message, (const char *)pstring, copy_len); // A message is copied to an extended bit sequence. + copy_len = buf_size % 64; //The number of bytes which remained is computed. + strncpy((char *)padding_message, (const char *)pbuf, copy_len); // A message is copied to an extended bit sequence. memset(padding_message+copy_len, 0, 64 - copy_len); //It buries by 0 until it becomes extended bit length. padding_message[copy_len] |= 0x80; //The next of a message is 1. @@ -216,12 +214,12 @@ static void md5_string2binary(const char *string, unsigned char *output) } //Step 2.Append Length (the information on length is added) - string_bit_len = string_byte_len * 8; //From the byte chief to bit length (32 bytes of low rank) - memcpy(&padding_message[56], &string_bit_len, 4); //32 bytes of low rank is set. + buf_bit_len = buf_size * 8; //From the byte chief to bit length (32 bytes of low rank) + memcpy(&padding_message[56], &buf_bit_len, 4); //32 bytes of low rank is set. //When bit length cannot be expressed in 32 bytes of low rank, it is a beam raising to a higher rank. - if (UINT_MAX / 8 < string_byte_len) { - unsigned int high = (string_byte_len - UINT_MAX / 8) * 8; + if (UINT_MAX / 8 < (unsigned int)buf_size) { + unsigned int high = (buf_size - UINT_MAX / 8) * 8; memcpy(&padding_message[60], &high, 4); } else { memset(&padding_message[60], 0, 4); //In this case, it is good for a higher rank at 0. @@ -237,12 +235,12 @@ static void md5_string2binary(const char *string, unsigned char *output) /// @copydoc md5_interface::string() void md5_string(const char *string, char *output) { - unsigned char digest[16]; + uint8 digest[16]; nullpo_retv(string); nullpo_retv(output); - md5->binary(string,digest); + md5->binary((const uint8 *)string, (int)strlen(string), digest); snprintf(output, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[ 0], digest[ 1], digest[ 2], digest[ 3], digest[ 4], digest[ 5], digest[ 6], digest[ 7], @@ -267,7 +265,7 @@ void md5_salt(int len, char *output) void md5_defaults(void) { md5 = &md5_s; - md5->binary = md5_string2binary; + md5->binary = md5_buf2binary; md5->string = md5_string; md5->salt = md5_salt; } diff --git a/src/common/md5calc.h b/src/common/md5calc.h index b4d4995f9..f55ebe312 100644 --- a/src/common/md5calc.h +++ b/src/common/md5calc.h @@ -46,7 +46,7 @@ struct md5_interface { * @param[in] string The source string. * @param[out] output Output buffer (at least 16 bytes available). */ - void (*binary) (const char *string, unsigned char *output); + void (*binary) (const uint8 *buf, const int buf_size, uint8 *output); /** * Generates a random salt. |