parent
5c60773269
commit
d3793f30a2
7 changed files with 151 additions and 69 deletions
@ -0,0 +1,64 @@ |
||||
/*! @file
|
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#ifndef __CRYPTAUX_HXX__ |
||||
#define __CRYPTAUX_HXX__ |
||||
|
||||
#include <string> |
||||
#include <sstream> |
||||
#include <iomanip> |
||||
|
||||
/*! @defgroup gcrypto Auxiliary Crypto-Functions */ |
||||
//@{
|
||||
|
||||
//! @see gcrypto
|
||||
namespace crypto { |
||||
|
||||
static const std::string LETTER_CHARS |
||||
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); |
||||
static const std::string NUMBER_CHARS |
||||
("0123456789"); |
||||
//! Contains @c @ in addition to standard characters.
|
||||
static const std::string GRAFIC_CHARS |
||||
("!\"#%&'()*+,-./:;<=>?[\\]^_{|}~@"); |
||||
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::string::size_type len=20) { |
||||
std::stringstream res; |
||||
std::string::size_type pos(0); |
||||
for (std::string::const_iterator it(data.begin()); it!=data.end(); ++it) { |
||||
res<<std::hex<<std::setfill('0')<<std::setw(2) |
||||
<<(unsigned int)(unsigned char)*it; |
||||
++pos; |
||||
if (pos%len==0 || pos==data.size()) { |
||||
res<<std::string(2*(len-(pos-1)%len), ' '); |
||||
for (std::string::size_type i(pos-(pos-1)%len-1); i<pos; ++i) |
||||
res<<(VALID_CHARS.find(data[i])==std::string::npos?'.':data[i]); |
||||
if (pos!=data.size()) res<<std::endl; |
||||
} |
||||
} |
||||
return res.str(); |
||||
} |
||||
|
||||
inline std::string readable(const std::string& data, |
||||
std::string::size_type len=20) { |
||||
if (!data.size()) |
||||
return "<empty>"; |
||||
else if (data.find_first_not_of(VALID_CHARS)<data.size()) |
||||
return hex(data); |
||||
else |
||||
return "\""+data+"\""; |
||||
} |
||||
} |
||||
|
||||
//@}
|
||||
|
||||
#endif |
Loading…
Reference in new issue