Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
crypto: rewrite HexDecode without snprintf
Browse files Browse the repository at this point in the history
No need to use snprintf to create a hex string. It creates
more overhead than is needed. This new version is much faster.
  • Loading branch information
defunctzombie authored and bnoordhuis committed Dec 16, 2011
1 parent cc2861e commit 4b123f9
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/node_crypto.cc
Expand Up @@ -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) \
Expand Down

0 comments on commit 4b123f9

Please sign in to comment.