diff options
author | Davi Arnaut <Davi.Arnaut@Sun.COM> | 2010-01-26 15:05:19 -0200 |
---|---|---|
committer | Davi Arnaut <Davi.Arnaut@Sun.COM> | 2010-01-26 15:05:19 -0200 |
commit | 7920e89a47a6cc8f65783528291ce87ba5b19074 (patch) | |
tree | 3d169a32bfc2fa40d24488c0714671f9be9aa496 /sql | |
parent | 9ac2343cc3fbba20a7615dfcb34a88d183c7bea5 (diff) | |
download | mariadb-git-7920e89a47a6cc8f65783528291ce87ba5b19074.tar.gz |
Bug#49491: Much overhead for MD5() and SHA1() on short strings
MySQL's hash functions MD5 and SHA relied on the somewhat slow
sprintf function to convert the digests to hex representations.
This patch replaces the sprintf with a specific and inline hex
conversion function.
Patch contributed by Jan Steemann.
sql/item_strfunc.cc:
Add a hex conversion function.
Diffstat (limited to 'sql')
-rw-r--r-- | sql/item_strfunc.cc | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index ecd839d8378..4cdeeef6c78 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -42,6 +42,20 @@ C_MODE_END String my_empty_string("",default_charset_info); +/* + Convert an array of bytes to a hexadecimal representation. + + Used to generate a hexadecimal representation of a message digest. +*/ +static void array_to_hex(char *to, const char *str, uint len) +{ + const char *str_end= str + len; + for (; str != str_end; ++str) + { + *to++= _dig_vec_lower[((uchar) *str) >> 4]; + *to++= _dig_vec_lower[((uchar) *str) & 0x0F]; + } +} bool Item_str_func::fix_fields(THD *thd, Item **ref) @@ -114,12 +128,7 @@ String *Item_func_md5::val_str(String *str) null_value=1; return 0; } - sprintf((char *) str->ptr(), - "%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], - digest[8], digest[9], digest[10], digest[11], - digest[12], digest[13], digest[14], digest[15]); + array_to_hex((char *) str->ptr(), (const char*) digest, 16); str->length((uint) 32); return str; } @@ -160,15 +169,7 @@ String *Item_func_sha::val_str(String *str) if (!( str->alloc(SHA1_HASH_SIZE*2) || (mysql_sha1_result(&context,digest)))) { - sprintf((char *) str->ptr(), - "%02x%02x%02x%02x%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], - digest[8], digest[9], digest[10], digest[11], - digest[12], digest[13], digest[14], digest[15], - digest[16], digest[17], digest[18], digest[19]); - + array_to_hex((char *) str->ptr(), (const char*) digest, SHA1_HASH_SIZE); str->length((uint) SHA1_HASH_SIZE*2); null_value=0; return str; |