#include <inttypes.h>
#include <stddef.h>
+#include "transmission.h" /* SHA_DIGEST_LENGTH */
#include "utils.h" /* TR_GNUC_MALLOC, TR_GNUC_NULL_TERMINATED */
#ifdef __cplusplus
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
#include <stdio.h> /* sscanf () */
#include "transmission.h"
+#include "crypto-utils.h" /* tr_hex_to_sha1 () */
#include "magnet.h"
-#include "utils.h"
#include "variant.h"
#include "web.h"
{
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;
#endif
#include <assert.h>
-#include <ctype.h> /* isdigit (), isalpha (), tolower () */
+#include <ctype.h> /* isdigit (), tolower () */
#include <errno.h>
#include <float.h> /* DBL_EPSILON */
#include <locale.h> /* localeconv () */
}
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);
}
}
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);