Class for encoding/decoding algorithms. More...
#include <utilities/string_utilities.h>
Static Public Member Functions | |
static void | XOREncode (char *OutputBuffer, std::size_t BufferSize, char *CodeWord) |
static void | XORDecode (char *InputBuffer, std::size_t BufferSize, char *CodeWord) |
static void | EncodeBase64 (const char *bf, std::size_t count, char *outbf) |
template<class str_type > | |
static str_type | EncodeBase64 (const char *bf, std::size_t count) |
static void | DecodeBase64 (const char *InBuf, char *OutBuf, std::size_t &Count) |
template<class str_type > | |
static str_type | DecodeBase64 (const char *bf) |
static void | md5 (const char *Data, std::size_t DataLength, char *Hash) |
template<class str_type > | |
static void | md5 (const char *Data, std::size_t DataLength) |
Class for encoding/decoding algorithms.
Definition at line 740 of file string_utilities.h.
void nitro::Encoders::DecodeBase64 | ( | const char * | InBuf, | |
char * | OutBuf, | |||
std::size_t & | Count | |||
) | [static] |
Function to decode data from base64.
InBuf | - Buffer to decode. | |
OutBuf | - Buffer to store result. | |
Count | - Size of output buffer. |
nitro::exception | Throws exception with error description. |
Definition at line 341 of file string_utilities.cpp.
References nitro::exception::code(), and nitro::exception::what().
Referenced by DecodeBase64().
{ try { std::string EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; char a , b , c , d; char x , y , z; int t = 0; if( std::string( InBuf ) == "" ) { Count = 0; return; } if( strlen( InBuf ) % 4 != 0 ) { throw( std::string( "Illegal input string" ) ); } for( std::size_t i( 0 ) ; i < strlen( InBuf ) / 4 ; i++ ) { std::size_t r; r = EncodeTable.find( InBuf[ 4 * i + 0 ] , 0 ); a = ( r == std::string::npos ? -1 : ( char ) r ); r = EncodeTable.find( InBuf[ 4 * i + 1 ] , 0 ); b = ( r == std::string::npos ? -1 : ( char ) r ); r = EncodeTable.find( InBuf[ 4 * i + 2 ] , 0 ); c = ( r == std::string::npos ? -1 : ( char ) r ); r = EncodeTable.find( InBuf[ 4 * i + 3 ] , 0 ); d = ( r == std::string::npos ? -1 : ( char ) r ); if( ( a == -1 ) || ( b == -1 ) || ( c == -1 ) || ( d == -1 ) || ( a == 64 ) || ( b == 64 ) ) { throw( std::string( "An error occured while decoding" ) ); } if( c == 64 ) { x = ( a << 2 ) | ( b >> 4 ); OutBuf[ t++ ] = x; } else if( d == 64 ) { x = ( a << 2 ) | ( b >> 4 ); y = ( b << 4 ) | ( c >> 2 ); OutBuf[ t++ ] = x; OutBuf[ t++ ] = y; } else { x = ( a << 2 ) | ( b >> 4 ); y = ( b << 4 ) | ( c >> 2 ); z = ( c << 6 ) | d; OutBuf[ t++ ] = x; OutBuf[ t++ ] = y; OutBuf[ t++ ] = z; } } Count = t; } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * InBuf , char * OutBuf , std::size_t & Count )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * InBuf , char * OutBuf , std::size_t & Count )::An error occured while encoding string to base64" ) , 1 ) ); } }
str_type nitro::Encoders::DecodeBase64 | ( | const char * | bf | ) | [static] |
Function to decode data from base64.
bf | - Buffer to decode. |
nitro::exception | Throws exception with error description. |
Definition at line 987 of file string_utilities.h.
References nitro::exception::code(), DecodeBase64(), and nitro::exception::what().
{ try { str_type out; //Pointer< char > OutBuffer( strlen( bf ) ); char * OutBuffer( new char[ strlen( bf ) ] ); memset( OutBuffer , 0 , strlen( bf ) ); std::size_t Count; DecodeBase64( bf , OutBuffer , Count ); out.AppendData( OutBuffer , Count ); delete [] OutBuffer; return( out ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * bf )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Encoders::DecodeBase64( const char * bf )::An error occured" ) , 0 ) ); } }
void nitro::Encoders::EncodeBase64 | ( | const char * | bf, | |
std::size_t | count, | |||
char * | outbf | |||
) | [static] |
Function encodes buffer in base64.
bf | - Encoding buffer. | |
count | - Size of buffer. | |
outbf | - Encoded string. |
nitro::exception | Throws exception with error description. |
Definition at line 278 of file string_utilities.cpp.
References nitro::exception::code(), and nitro::exception::what().
Referenced by EncodeBase64().
{ try { std::string EncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; std::string Sout = ""; unsigned char a , b , c , i; for( std::size_t k( 0 ) ; k < count ; k++ ) { if( count - k == 1 ) { a = bf[ k ]; b = 0; i = a >> 2; Sout += EncodeTable.substr( i , 1 ); i = ( ( a & 3 ) << 4 ) | ( b >> 4 ); Sout += EncodeTable.substr( i , 1 ); Sout += EncodeTable.substr( 64 , 1 ); Sout += EncodeTable.substr( 64 , 1 ); } else if( count - k == 2 ) { a = bf[ k ]; b = bf[ ++k ]; c = 0; i = a >> 2; Sout += EncodeTable.substr( i , 1 ); i = ( ( a & 3 ) << 4 ) | ( b >> 4 ); Sout += EncodeTable.substr( i , 1 ); i = ( ( b & 15 ) << 2 ) | ( c >> 6 ); Sout += EncodeTable.substr( i , 1 ); Sout += EncodeTable.substr( 64 , 1 ); } else { a = bf[ k ]; b = bf[ ++k ]; c = bf[ ++k ]; i = a >> 2; Sout += EncodeTable.substr( i , 1 ); i = ( ( a & 3 ) << 4 ) | ( b >> 4 ); Sout += EncodeTable.substr( i , 1 ); i = ( ( b & 15 ) << 2 ) | ( c >> 6 ); Sout += EncodeTable.substr( i , 1 ); i = c & 63; Sout += EncodeTable.substr( i , 1 ); } } strcpy( outbf , Sout.c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count , char * outbf )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count , char * outbf )::An error occured while encoding string to base64" ) , 1 ) ); } }
str_type nitro::Encoders::EncodeBase64 | ( | const char * | bf, | |
std::size_t | count | |||
) | [static] |
Function encodes data in base64.
bf | - Buffer to be encoded. | |
count | - Size of buffer (in bytes). |
nitro::exception | Throws exception with error description. |
Definition at line 960 of file string_utilities.h.
References nitro::exception::code(), EncodeBase64(), and nitro::exception::what().
{ try { str_type out; //Pointer< char > OutBuffer( ( std::size_t )( count * 1.5 ) ); char * OutBuffer( new char[ ( std::size_t )( count * 1.5 ) ] ); memset( OutBuffer , 0 , ( std::size_t )( count * 1.5 ) ); EncodeBase64( bf , count , OutBuffer ); out.AppendData( OutBuffer , ( std::size_t )( count * 1.5 ) ); delete [] OutBuffer; return( out ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Encoders::EncodeBase64( const char * bf , std::size_t count )::An error occured" ) , 0 ) ); } }
void nitro::Encoders::md5 | ( | const char * | Data, | |
std::size_t | DataLength | |||
) | [static] |
Function calculates md5 hash.
Data | - Data to be hashed. | |
DataLength | - Data size. |
nitro::exception | Throws exception with error description. |
Definition at line 1017 of file string_utilities.h.
References nitro::exception::code(), md5(), and nitro::exception::what().
{ try { char Hash[ 33 ]; md5( Data , DataLength , Hash ); return( str_type( Hash ) ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength )::An error occured" ) , 0 ) ); } }
void nitro::Encoders::md5 | ( | const char * | Data, | |
std::size_t | DataLength, | |||
char * | Hash | |||
) | [static] |
Function calculates md5 hash.
Data | - Data to be hashed. | |
DataLength | - Data size. | |
Hash | - Hash (size of the buffer must be at least 33 bytes). |
nitro::exception | Throws exception with error description. |
Definition at line 726 of file string_utilities.cpp.
References nitro::exception::code(), nitro::MDFinal(), nitro::MDInit(), nitro::MDUpdate(), and nitro::exception::what().
Referenced by md5().
{ try { MD5_CTX Context; UC Digest[ 16 ]; MDInit( & Context ); MDUpdate( & Context , ( UC * )Data , DataLength ); MDFinal( Digest , & Context ); // Формируем возвращаемую строку memset( Hash , 0 , 32 ); for( std::size_t i( 0 ) ; i < 16 ; i++ ) { sprintf( & Hash[ i * 2 ] , "%02x" , Digest[ i ] ); } Hash[ 32 ] = '\0'; } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength , char * Hash )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Encoders::md5( const char * Data , std::size_t DataLength , char * Hash )::An error occured while encoding string to base64" ) , 1 ) ); } }
void nitro::Encoders::XORDecode | ( | char * | InputBuffer, | |
std::size_t | BufferSize, | |||
char * | CodeWord | |||
) | [static] |
Function decodes string from XORed string.
InputBuffer | - Buffer to decode. | |
BufferSize | - Size of buffer. | |
CodeWord | - Password to decode. |
nitro::exception | Throws exception with error description. |
Definition at line 254 of file string_utilities.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { for( std::size_t i( 0 ) ; i < BufferSize ; i++ ) { InputBuffer[ i ] ^= CodeWord[ i % strlen( CodeWord ) ]; for( int j( ( int )strlen( CodeWord ) - 1 ) ; j >= 0 ; j-- ) { InputBuffer[ i ] ^= CodeWord[ j ]; } } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Encoders::XORDecode( char * InputBuffer , std::size_t BufferSize , char * CodeWord )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Encoders::XORDecode( char * InputBuffer , std::size_t BufferSize , char * CodeWord )::An error occured" ) , 1 ) ); } }
void nitro::Encoders::XOREncode | ( | char * | OutputBuffer, | |
std::size_t | BufferSize, | |||
char * | CodeWord | |||
) | [static] |
Functione encodes string using simple XOR algorithm.
OutputBuffer | - Buffer to encode. | |
BufferSize | - Size of buffer. | |
CodeWord | - Password to XOR with. |
nitro::exception | Throws exception with error description. |
Definition at line 230 of file string_utilities.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { for( std::size_t i( 0 ) ; i < BufferSize ; i++ ) { for( std::size_t j( 0 ) ; j < strlen( CodeWord ) ; j++ ) { OutputBuffer[ i ] ^= CodeWord[ j ]; } OutputBuffer[ i ] ^= CodeWord[ i % strlen( CodeWord ) ]; } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Encoders::XOREncode( char * OutputBuffer , std::size_t BufferSize , char * CodeWord )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Encoders::XOREncode( char * OutputBuffer , std::size_t BufferSize , char * CodeWord )::An error occured" ) , 1 ) ); } }