mirror of
https://github.com/transmission/transmission
synced 2025-03-12 07:03:44 +00:00
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.
This commit is contained in:
parent
748f8a75d2
commit
42de056a4b
5 changed files with 47 additions and 21 deletions
|
@ -13,6 +13,7 @@
|
|||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#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
|
||||
|
|
|
@ -12,8 +12,8 @@
|
|||
#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"
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
#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 () */
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue