37 #ifndef VIRGIL_BYTE_ARRAY_H
38 #define VIRGIL_BYTE_ARRAY_H
47 namespace virgil {
namespace crypto {
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 {
74 return VIRGIL_BYTE_ARRAY_FROM_PTR_AND_LEN(str.data(), str.size());
81 return std::string(reinterpret_cast<const char*>(array.data()), array.size());
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);
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();
127 size_t n = array.size();
128 volatile unsigned char* p =
const_cast<unsigned char*
>(array.data());
129 while (n--) { *p++ = 0; }
136 size_t n = str.size();
137 volatile char* p =
const_cast<char*
>(str.c_str());
138 while (n--) { *p++ =
'\0'; }
VirgilByteArray str2bytes(const std::string &str)
Represents given string as byte array.
Definition: VirgilByteArray.h:73
VirgilByteArray hex2bytes(const std::string hexStr)
Translate given HEX string to the byte array.
Definition: VirgilByteArray.h:89
Root namespace for all Virgil Security libraries.
Definition: VirgilAsn1Compatible.h:46
std::vector< unsigned char > VirgilByteArray
This type represents a sequence of bytes.
Definition: VirgilByteArray.h:53
void bytes_zeroize(VirgilByteArray &array)
Make all bytes zero.
Definition: VirgilByteArray.h:126
std::string bytes2hex(const VirgilByteArray &array, bool formatted=false)
Translate given byte array to the HEX string.
Definition: VirgilByteArray.h:108
void string_zeroize(std::string &str)
Make all chars zero.
Definition: VirgilByteArray.h:135
std::string bytes2str(const VirgilByteArray &array)
Represent given byte array as string.
Definition: VirgilByteArray.h:80