Browse Source

S1/T2

api_change
Krzysztof KWIATKOWSKI 9 years ago
parent
commit
71dec29e71
3 changed files with 106 additions and 8 deletions
  1. +61
    -6
      utils/hex_to_base64.cpp
  2. +4
    -1
      utils/hex_to_base64.h
  3. +41
    -1
      utils/utils_tester.cpp

+ 61
- 6
utils/hex_to_base64.cpp View File

@@ -51,19 +51,38 @@ struct int24 {
unsigned int data:24;
};

void read_bytes(const char* hex_buff, int24& o_data, unsigned len=3)
void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char*& oHexArray)
{
unsigned char hex;
for(int i=0; i<len; ++i)
for(unsigned long i=0; i<iArrayLen; i+=2)
{
o_data.data <<= 8;
hex = char_to_hex[ iArray[i ] ] * 16;
hex += char_to_hex[ iArray[i+1] ];

oHexArray[i/2]=hex;
}
}

hex = char_to_hex[ hex_buff[0+(i*2)] ] * 16;
hex += char_to_hex[ hex_buff[1+(i*2)] ];
o_data.data |= hex;
void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char*& oStringBuf )
{
for(unsigned i=0; i<iHexArrayLen; i++)
{
sprintf(oStringBuf+(i*2), "%x", iHexArray[i]);
}
}

void read_bytes(const char* hex_buff, int24& o_data, unsigned len=3)
{
unsigned char* tmp = (unsigned char*)malloc(len);
convert_string_to_hex(hex_buff, len*2, tmp);
for(unsigned i=0; i<len; ++i)
{
o_data.data <<= 8;
o_data.data |= tmp[i];
}
free(tmp);
}

/* -----------------------------------------------------------------------------
* @brief Converts HEX to Base64
*
@@ -133,3 +152,39 @@ unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_bu
}
return j;
}


/* -----------------------------------------------------------------------------
* @brief xor_strings
*
* @param iLeftString : String of HEX numbers. Length of string needs to be even
* @param iRightString: Second string. Has to be same length as iLeftString
* @param oXorArray : Array will keep result of XOR. Array needs to be pre
* allocated with length at least strlen(iLeftString)/2
*
* @remarks remarks
*
-------------------------------------------------------------------------------- */
void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray)
{
const size_t aInLen = strlen(iLeftString);
// Both arrays need to have same length
if(strlen(iRightString) != aInLen) return;
// Length of both tables needs to be even
if(aInLen % 2 != 0) return;

unsigned char* xor_array1 = (unsigned char*)malloc( aInLen );
unsigned char* xor_array2 = (unsigned char*)malloc( aInLen );

convert_string_to_hex(iLeftString, aInLen, xor_array1);
convert_string_to_hex(iRightString, aInLen, xor_array2);

for(size_t i=0; i<aInLen/2; ++i) {
oXorArray[i] = xor_array1[i] ^ xor_array2[i];
}

free(xor_array1);
free(xor_array2);
}

+ 4
- 1
utils/hex_to_base64.h View File

@@ -1 +1,4 @@
unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_buff_len );
unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_buff_len );
void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char*& oHexArray);
void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char*& oStringBuf );
void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray);

+ 41
- 1
utils/utils_tester.cpp View File

@@ -1,4 +1,4 @@
#include "hex_to_base64.h"
#include "hex_to_base64.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>
@@ -44,6 +44,46 @@ void hex_to_base64_text() {
assert( strlen(expected_buff4) == strlen(output));
}

void convert_string_to_hex_test()
{
const char test_buff1[] = "49276d";
unsigned char out_buff[3];
unsigned char* p = out_buff;
convert_string_to_hex(test_buff1, strlen(test_buff1), p);
assert( p[0] == 0x49
&& p[1] == 0x27
&& p[2] == 0x6d);

// and back
char out_buf[6];
char* op = out_buf;
convert_hex_to_string(p, 3, op);
assert( memcmp(op, test_buff1, 6) == 0 );

}

void xor_strings_test()
{
const char i_buf_1[] = "1c0111001f010100061a024b53535009181c";
const char* p_ibuf1=i_buf_1;

const char i_buf_2[] = "686974207468652062756c6c277320657965";
const char* p_ibuf2=i_buf_2;

char out_buf[36];
char* p_out_buf=out_buf;

unsigned char buf[36/2];
unsigned char* p_buf = buf;

xor_strings(p_ibuf1, p_ibuf2, p_buf);

convert_hex_to_string(buf, 36/2, p_out_buf);
assert( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0);
}

int main() {
hex_to_base64_text();
convert_string_to_hex_test();
xor_strings_test();
}

Loading…
Cancel
Save