Changeset 14361


Ignore:
Timestamp:
Dec 4, 2014, 8:53:56 PM (8 years ago)
Author:
mikedld
Message:

#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.

Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/crypto-utils.h

    r14360 r14361  
    1414#include <stddef.h>
    1515
     16#include "transmission.h" /* SHA_DIGEST_LENGTH */
    1617#include "utils.h" /* TR_GNUC_MALLOC, TR_GNUC_NULL_TERMINATED */
    1718
     
    212213                                        size_t         * output_length) TR_GNUC_MALLOC;
    213214
     215/**
     216 * @brief Wrapper around tr_binary_to_hex () for SHA_DIGEST_LENGTH.
     217 */
     218static inline void
     219tr_sha1_to_hex (char          * hex,
     220                const uint8_t * sha1)
     221{
     222  tr_binary_to_hex (sha1, hex, SHA_DIGEST_LENGTH);
     223}
     224
     225/**
     226 * @brief Wrapper around tr_hex_to_binary () for SHA_DIGEST_LENGTH.
     227 */
     228static inline void
     229tr_hex_to_sha1 (uint8_t    * sha1,
     230                const char * hex)
     231{
     232  tr_hex_to_binary (hex, sha1, SHA_DIGEST_LENGTH);
     233}
     234
    214235/** @} */
    215236
  • trunk/libtransmission/magnet.c

    r14241 r14361  
    1313
    1414#include "transmission.h"
     15#include "crypto-utils.h" /* tr_hex_to_sha1 () */
    1516#include "magnet.h"
    16 #include "utils.h"
    1717#include "variant.h"
    1818#include "web.h"
  • trunk/libtransmission/utils-test.c

    r14359 r14361  
    278278  char hex1[41];
    279279  char hex2[41];
    280   uint8_t sha1[20];
     280  uint8_t binary[20];
    281281
    282282  memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41);
    283   tr_hex_to_sha1 (sha1, hex1);
    284   tr_sha1_to_hex (hex2, sha1);
     283  tr_hex_to_binary (hex1, binary, 20);
     284  tr_binary_to_hex (binary, hex2, 20);
    285285  check_streq (hex1, hex2);
    286286
  • trunk/libtransmission/utils.c

    r14359 r14361  
    1919
    2020#include <assert.h>
    21 #include <ctype.h> /* isdigit (), isalpha (), tolower () */
     21#include <ctype.h> /* isdigit (), tolower () */
    2222#include <errno.h>
    2323#include <float.h> /* DBL_EPSILON */
     
    630630
    631631void
    632 tr_sha1_to_hex (char * out, const uint8_t * sha1)
    633 {
    634   int i;
     632tr_binary_to_hex (const void * input,
     633                  char       * output,
     634                  size_t       byte_length)
     635{
    635636  static const char hex[] = "0123456789abcdef";
    636 
    637   for (i=0; i<20; ++i)
    638     {
    639       const unsigned int val = *sha1++;
    640       *out++ = hex[val >> 4];
    641       *out++ = hex[val & 0xf];
    642     }
    643 
    644   *out = '\0';
     637  const uint8_t * input_octets = input;
     638  size_t i;
     639
     640  for (i = 0; i < byte_length; ++i)
     641    {
     642      const unsigned int val = *input_octets++;
     643      *output++ = hex[val >> 4];
     644      *output++ = hex[val & 0xf];
     645    }
     646
     647  *output = '\0';
    645648}
    646649
    647650void
    648 tr_hex_to_sha1 (uint8_t * out, const char * in)
    649 {
    650   int i;
     651tr_hex_to_binary (const char * input,
     652                  void       * output,
     653                  size_t       byte_length)
     654{
    651655  static const char hex[] = "0123456789abcdef";
    652 
    653   for (i=0; i<20; ++i)
    654     {
    655       const int hi = strchr (hex, tolower (*in++)) - hex;
    656       const int lo = strchr (hex, tolower (*in++)) - hex;
    657       *out++ = (uint8_t)((hi<<4) | lo);
     656  uint8_t * output_octets = output;
     657  size_t i;
     658
     659  for (i = 0; i < byte_length; ++i)
     660    {
     661      const int hi = strchr (hex, tolower (*input++)) - hex;
     662      const int lo = strchr (hex, tolower (*input++)) - hex;
     663      *output_octets++ = (uint8_t) ((hi << 4) | lo);
    658664    }
    659665}
  • trunk/libtransmission/utils.h

    r14359 r14361  
    320320int compareInt (const void * va, const void * vb);
    321321
    322 void tr_sha1_to_hex (char * out, const uint8_t * sha1) TR_GNUC_NONNULL (1,2);
    323 
    324 void tr_hex_to_sha1 (uint8_t * out, const char * hex) TR_GNUC_NONNULL (1,2);
     322void tr_binary_to_hex (const void * input, char * output, size_t byte_length) TR_GNUC_NONNULL (1,2);
     323void tr_hex_to_binary (const char * input, void * output, size_t byte_length) TR_GNUC_NONNULL (1,2);
    325324
    326325/** @brief convenience function to determine if an address is an IP address (IPv4 or IPv6) */
Note: See TracChangeset for help on using the changeset viewer.