From: Mike Gelfand Date: Thu, 4 Dec 2014 20:53:56 +0000 (+0000) Subject: #4400, #5462: Move SHA1/HEX helpers to crypto-utils X-Git-Tag: 2.90~331 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=42de056a4bd70aa5f246c30a84ee24cdbcfc385a;p=transmission #4400, #5462: Move SHA1/HEX helpers to crypto-utils On a way to factoring out OpenSSL support to a standalone file to ease addition of other crypto libraries support in the future, move helpers providing SHA1/HEX conversion to crypto-utils.{c,h}. Rename functions: * tr_sha1_to_hex -> tr_binary_to_hex (add length argument), * tr_hex_to_sha1 -> tr_hex_to_binary (add length argument). Make tr_sha1_to_hex and tr_hex_to_sha1 wrappers around above functions. --- diff --git a/libtransmission/crypto-utils.h b/libtransmission/crypto-utils.h index f6bc0a988..7078da3f4 100644 --- a/libtransmission/crypto-utils.h +++ b/libtransmission/crypto-utils.h @@ -13,6 +13,7 @@ #include #include +#include "transmission.h" /* SHA_DIGEST_LENGTH */ #include "utils.h" /* TR_GNUC_MALLOC, TR_GNUC_NULL_TERMINATED */ #ifdef __cplusplus @@ -211,6 +212,26 @@ void * tr_base64_decode_impl (const void * input, size_t input_length, size_t * output_length) TR_GNUC_MALLOC; +/** + * @brief Wrapper around tr_binary_to_hex () for SHA_DIGEST_LENGTH. + */ +static inline void +tr_sha1_to_hex (char * hex, + const uint8_t * sha1) +{ + tr_binary_to_hex (sha1, hex, SHA_DIGEST_LENGTH); +} + +/** + * @brief Wrapper around tr_hex_to_binary () for SHA_DIGEST_LENGTH. + */ +static inline void +tr_hex_to_sha1 (uint8_t * sha1, + const char * hex) +{ + tr_hex_to_binary (hex, sha1, SHA_DIGEST_LENGTH); +} + /** @} */ #ifdef __cplusplus diff --git a/libtransmission/magnet.c b/libtransmission/magnet.c index f5d2bb994..8a95bed35 100644 --- a/libtransmission/magnet.c +++ b/libtransmission/magnet.c @@ -12,8 +12,8 @@ #include /* sscanf () */ #include "transmission.h" +#include "crypto-utils.h" /* tr_hex_to_sha1 () */ #include "magnet.h" -#include "utils.h" #include "variant.h" #include "web.h" diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index d9fb93091..64cfde187 100644 --- a/libtransmission/utils-test.c +++ b/libtransmission/utils-test.c @@ -277,11 +277,11 @@ test_hex (void) { char hex1[41]; char hex2[41]; - uint8_t sha1[20]; + uint8_t binary[20]; memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41); - tr_hex_to_sha1 (sha1, hex1); - tr_sha1_to_hex (hex2, sha1); + tr_hex_to_binary (hex1, binary, 20); + tr_binary_to_hex (binary, hex2, 20); check_streq (hex1, hex2); return 0; diff --git a/libtransmission/utils.c b/libtransmission/utils.c index d1f98729a..7e0f0f707 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -18,7 +18,7 @@ #endif #include -#include /* isdigit (), isalpha (), tolower () */ +#include /* isdigit (), tolower () */ #include #include /* DBL_EPSILON */ #include /* localeconv () */ @@ -629,32 +629,38 @@ tr_getRatio (uint64_t numerator, uint64_t denominator) } void -tr_sha1_to_hex (char * out, const uint8_t * sha1) +tr_binary_to_hex (const void * input, + char * output, + size_t byte_length) { - int i; static const char hex[] = "0123456789abcdef"; + const uint8_t * input_octets = input; + size_t i; - for (i=0; i<20; ++i) + for (i = 0; i < byte_length; ++i) { - const unsigned int val = *sha1++; - *out++ = hex[val >> 4]; - *out++ = hex[val & 0xf]; + const unsigned int val = *input_octets++; + *output++ = hex[val >> 4]; + *output++ = hex[val & 0xf]; } - *out = '\0'; + *output = '\0'; } void -tr_hex_to_sha1 (uint8_t * out, const char * in) +tr_hex_to_binary (const char * input, + void * output, + size_t byte_length) { - int i; static const char hex[] = "0123456789abcdef"; + uint8_t * output_octets = output; + size_t i; - for (i=0; i<20; ++i) + for (i = 0; i < byte_length; ++i) { - const int hi = strchr (hex, tolower (*in++)) - hex; - const int lo = strchr (hex, tolower (*in++)) - hex; - *out++ = (uint8_t)((hi<<4) | lo); + const int hi = strchr (hex, tolower (*input++)) - hex; + const int lo = strchr (hex, tolower (*input++)) - hex; + *output_octets++ = (uint8_t) ((hi << 4) | lo); } } diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 3e6f2d88f..f32d568b4 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -319,9 +319,8 @@ char* tr_strsep (char ** str, const char * delim); int compareInt (const void * va, const void * vb); -void tr_sha1_to_hex (char * out, const uint8_t * sha1) TR_GNUC_NONNULL (1,2); - -void tr_hex_to_sha1 (uint8_t * out, const char * hex) TR_GNUC_NONNULL (1,2); +void tr_binary_to_hex (const void * input, char * output, size_t byte_length) TR_GNUC_NONNULL (1,2); +void tr_hex_to_binary (const char * input, void * output, size_t byte_length) TR_GNUC_NONNULL (1,2); /** @brief convenience function to determine if an address is an IP address (IPv4 or IPv6) */ bool tr_addressIsIP (const char * address);