/*! @file @id $Id$ */ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 #ifndef __CRYPTAUX_HXX__ #define __CRYPTAUX_HXX__ #include #include #include #include #include /*! @defgroup gcrypto Auxiliary Crypto-Functions Auxiliary often used funcions in cryptographic environment, such as logging, converting binary from and to hexadecimal or creating readable texts from binary data. To enable logging, add @c -DDEBUG to @c CPPFLAGS */ //@{ #define CRYPTOLOG_QUOTE(X) CRYPTOLOG_QUOTE2(X) #define CRYPTOLOG_QUOTE2(X) #X #if __GNUC__ >= 2 # define CRYPTOLOG_END " -- "<<__PRETTY_FUNCTION__< # define CRYPTOLOG(X) { \ std::string __C_FILE__(__FILE__); \ std::string __C_LINE__(CRYPTOLOG_QUOTE(__LINE__)); \ std::string::size_type __LOC_POS__(__C_FILE__.rfind('/')); \ if (__LOC_POS__!=std::string::npos) \ __C_FILE__=__C_FILE__.substr(__LOC_POS__+1); \ std::string __LOC_SPC1__(18>__C_FILE__.size() \ ?std::string(18-__C_FILE__.size(), ' ') \ :std::string()); \ std::string __LOC_SPC2__(4>__C_LINE__.size() \ ?std::string(4-__C_LINE__.size(), ' ') \ :std::string()); \ std::clog<<"CRYPTO: "<<__LOC_SPC1__<<__C_FILE__ \ <<':'<<__LOC_SPC2__<<__C_LINE__<<" -- "< # define CRYPTOLOG_VERBOSE(X) { \ std::string __C_FILE__(__FILE__); \ std::string __C_LINE__(CRYPTOLOG_QUOTE(__LINE__)); \ std::string::size_type __LOC_POS__(__C_FILE__.rfind('/')); \ if (__LOC_POS__!=std::string::npos) \ __C_FILE__=__C_FILE__.substr(__LOC_POS__+1); \ std::string __LOC_SPC1__(18>__C_FILE__.size() \ ?std::string(18-__C_FILE__.size(), ' ') \ :std::string()); \ std::string __LOC_SPC2__(4>__C_LINE__.size() \ ?std::string(4-__C_LINE__.size(), ' ') \ :std::string()); \ std::clog<<"CRYPTO: "<<__LOC_SPC1__<<__C_FILE__<<':' \ <<__LOC_SPC2__<<__C_LINE__<<" -- "<?[\\]^_{|}~@"); static const std::string BLANK_CHARS (" "); static const std::string VALID_CHARS (LETTER_CHARS+NUMBER_CHARS+GRAFIC_CHARS+BLANK_CHARS); inline std::string hex(const std::string& data) { std::stringstream res; for (std::string::const_iterator it(data.begin()); it!=data.end(); ++it) res<"; else if (data.find_first_not_of(VALID_CHARS)>=8; } return res; } /// convert integer from binary of given size inline unsigned long ulongFromBinary(const std::string& data) { unsigned long res(0); for (std::string::const_iterator it(data.begin()); it!=data.end(); ++it) (res<<=8)+=(unsigned long)(unsigned char)*it; return res; } } //@} #endif