diff options
author | Roman Shtylman <shtylman@gmail.com> | 2011-12-01 00:41:06 -0500 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2011-12-16 14:35:15 +0100 |
commit | 4b123f9ca22ee2622eb32233cb1d07af21e33826 (patch) | |
tree | e7872138f1e3c5c8a3d2c899a529b949fc94f597 | |
parent | cc2861ee44c000a4696514e5b3718a371a5c9720 (diff) | |
download | node-4b123f9ca22ee2622eb32233cb1d07af21e33826.tar.gz |
crypto: rewrite HexDecode without snprintf
No need to use snprintf to create a hex string. It creates
more overhead than is needed. This new version is much faster.
-rw-r--r-- | src/node_crypto.cc | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/node_crypto.cc b/src/node_crypto.cc index d96b4d15f..9f73df35c 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -1699,9 +1699,20 @@ static void HexEncode(unsigned char *md_value, int* md_hex_len) { *md_hex_len = (2*(md_len)); *md_hexdigest = new char[*md_hex_len + 1]; - for (int i = 0; i < md_len; i++) { - snprintf((char *)(*md_hexdigest + (i*2)), 3, "%02x", md_value[i]); + + char* buff = *md_hexdigest; + const int len = *md_hex_len; + for (int i = 0; i < len; i += 2) { + // nibble nibble + const int index = i / 2; + const char msb = (md_value[index] >> 4) & 0x0f; + const char lsb = md_value[index] & 0x0f; + + buff[i] = (msb < 10) ? msb + '0' : (msb - 10) + 'a'; + buff[i + 1] = (lsb < 10) ? lsb + '0' : (lsb - 10) + 'a'; } + // null terminator + buff[*md_hex_len] = '\0'; } #define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) \ |