37 #ifndef VIRGIL_BYTE_ARRAY_H
38 #define VIRGIL_BYTE_ARRAY_H
47 namespace virgil {
namespace crypto {
53 typedef std::vector<unsigned char> VirgilByteArray;
60 #define VIRGIL_BYTE_ARRAY_TO_PTR_AND_LEN(array) reinterpret_cast<const unsigned char *>(array.data()), array.size()
63 #define VIRGIL_BYTE_ARRAY_FROM_PTR_AND_LEN(ptr, len)\
64 virgil::crypto::VirgilByteArray(reinterpret_cast<virgil::crypto::VirgilByteArray::const_pointer >(ptr), \
65 reinterpret_cast<virgil::crypto::VirgilByteArray::const_pointer >((ptr) + (len)))
68 namespace virgil {
namespace crypto {
73 inline VirgilByteArray str2bytes(
const std::string& str) {
74 return VIRGIL_BYTE_ARRAY_FROM_PTR_AND_LEN(str.data(), str.size());
80 inline std::string bytes2str(
const VirgilByteArray& array) {
81 return std::string(reinterpret_cast<const char*>(array.data()), array.size());
89 inline VirgilByteArray hex2bytes(
const std::string hexStr) {
90 VirgilByteArray result;
91 std::istringstream istr(hexStr);
92 char hexChars[3] = {0x00};
93 while (istr.read(hexChars, 2)) {
95 std::istringstream(hexChars) >> std::hex >> byte;
96 result.push_back((
unsigned char) byte);
108 inline std::string bytes2hex(
const VirgilByteArray& array,
bool formatted =
false) {
109 std::ostringstream hexStream;
110 hexStream << std::setfill(
'0');
111 for (
size_t i = 0; i < array.size(); ++i) {
112 hexStream << std::hex << std::setw(2) << (int) array[i];
114 hexStream << (((i + 1) % 16 == 0) ?
"\n" :
" ");
117 return hexStream.str();
126 inline void bytes_zeroize(VirgilByteArray& array) {
127 size_t n = array.size();
128 volatile unsigned char* p =
const_cast<unsigned char*
>(array.data());
129 while (n--) { *p++ = 0; }
135 inline void string_zeroize(std::string& str) {
136 size_t n = str.size();
137 volatile char* p =
const_cast<char*
>(str.c_str());
138 while (n--) { *p++ =
'\0'; }
Definition: VirgilAsn1Compatible.h:46