diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index 6e0b6425..308552b9 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -96,17 +96,22 @@ char * sodium_bin2hex(char * const hex, const size_t hex_maxlen, const unsigned char * const bin, const size_t bin_len) { - size_t i = (size_t) 0U; - int b; + size_t i = (size_t) 0U; + unsigned int x; + int b; + int c; if (bin_len >= SIZE_MAX / 2 || hex_maxlen < bin_len * 2U) { abort(); /* LCOV_EXCL_LINE */ } while (i < bin_len) { + c = bin[i] & 0xf; b = bin[i] >> 4; - hex[i * 2U] = (char) (87 + b + (((b - 10) >> 31) & -39)); - b = bin[i] & 0xf; - hex[i * 2U + 1U] = (char) (87 + b + (((b - 10) >> 31) & -39)); + x = (unsigned char) (87 + c + (((c - 10) >> 31) & -39)) << 8 | + (unsigned char) (87 + b + (((b - 10) >> 31) & -39)); + hex[i * 2U] = (char) x; + x >>= 8; + hex[i * 2U + 1U] = (char) x; i++; } hex[i * 2U] = 0;