Virgil Security Crypto library  2.2.2
VirgilByteArray.h
1 
37 #ifndef VIRGIL_BYTE_ARRAY_H
38 #define VIRGIL_BYTE_ARRAY_H
39 
40 #include <cstring>
41 #include <string>
42 #include <sstream>
43 #include <iomanip>
44 #include <vector>
45 #include <algorithm>
46 #include <tuple>
47 
48 namespace virgil { namespace crypto {
49 
54 typedef std::vector<unsigned char> VirgilByteArray;
55 
56 }}
57 
61 #define VIRGIL_BYTE_ARRAY_TO_PTR_AND_LEN(array) reinterpret_cast<const unsigned char *>(array.data()), array.size()
63 
64 #define VIRGIL_BYTE_ARRAY_FROM_PTR_AND_LEN(ptr, len)\
65  virgil::crypto::VirgilByteArray(reinterpret_cast<virgil::crypto::VirgilByteArray::const_pointer >(ptr), \
66  reinterpret_cast<virgil::crypto::VirgilByteArray::const_pointer >((ptr) + (len)))
67 
69 namespace virgil { namespace crypto {
70 
74 inline VirgilByteArray str2bytes(const std::string& str) {
75  return VIRGIL_BYTE_ARRAY_FROM_PTR_AND_LEN(str.data(), str.size());
76 }
77 
81 inline std::string bytes2str(const VirgilByteArray& array) {
82  return std::string(reinterpret_cast<const char*>(array.data()), array.size());
83 }
84 
90 inline VirgilByteArray hex2bytes(const std::string hexStr) {
91  VirgilByteArray result;
92  std::istringstream istr(hexStr);
93  char hexChars[3] = {0x00};
94  while (istr.read(hexChars, 2)) {
95  int byte = 0;
96  std::istringstream(hexChars) >> std::hex >> byte;
97  result.push_back((unsigned char) byte);
98  }
99  return result;
100 }
101 
109 inline std::string bytes2hex(const VirgilByteArray& array, bool formatted = false) {
110  std::ostringstream hexStream;
111  hexStream << std::setfill('0');
112  for (size_t i = 0; i < array.size(); ++i) {
113  hexStream << std::hex << std::setw(2) << (int) array[i];
114  if (formatted) {
115  hexStream << (((i + 1) % 16 == 0) ? "\n" : " ");
116  }
117  }
118  return hexStream.str();
119 }
123 
127 inline void bytes_zeroize(VirgilByteArray& array) {
128  size_t n = array.size();
129  volatile unsigned char* p = const_cast<unsigned char*>(array.data());
130  while (n--) { *p++ = 0; }
131 }
132 
136 inline void string_zeroize(std::string& str) {
137  size_t n = str.size();
138  volatile char* p = const_cast<char*>(str.c_str());
139  while (n--) { *p++ = '\0'; }
140 }
142 
150  dst.insert(dst.end(), src.cbegin(), src.cend());
151  return dst;
152 }
153 
160 inline std::tuple<VirgilByteArray, VirgilByteArray> bytes_split(const VirgilByteArray& src, size_t pos) {
161  return std::make_tuple(
162  VirgilByteArray(src.cbegin(), src.cbegin() + pos),
163  VirgilByteArray(src.cbegin() + pos, src.cend()));
164 }
165 
171 inline std::tuple<VirgilByteArray, VirgilByteArray> bytes_split_half(const VirgilByteArray& src) {
172  const auto halfPos = src.size() >> 1;
173  return bytes_split(src, halfPos);
174 }
175 
176 }}
177 #endif /* VIRGIL_BYTE_ARRAY_H */
VirgilByteArray str2bytes(const std::string &str)
Represents given string as byte array.
Definition: VirgilByteArray.h:74
VirgilByteArray & bytes_append(VirgilByteArray &dst, const VirgilByteArray &src)
Append given source bytes to the existing destination bytes.
Definition: VirgilByteArray.h:149
std::tuple< VirgilByteArray, VirgilByteArray > bytes_split_half(const VirgilByteArray &src)
Split given bytes to two sequences of the same size.
Definition: VirgilByteArray.h:171
VirgilByteArray hex2bytes(const std::string hexStr)
Translate given HEX string to the byte array.
Definition: VirgilByteArray.h:90
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:54
void bytes_zeroize(VirgilByteArray &array)
Make all bytes zero.
Definition: VirgilByteArray.h:127
std::string bytes2hex(const VirgilByteArray &array, bool formatted=false)
Translate given byte array to the HEX string.
Definition: VirgilByteArray.h:109
void string_zeroize(std::string &str)
Make all chars zero.
Definition: VirgilByteArray.h:136
std::string bytes2str(const VirgilByteArray &array)
Represent given byte array as string.
Definition: VirgilByteArray.h:81
std::tuple< VirgilByteArray, VirgilByteArray > bytes_split(const VirgilByteArray &src, size_t pos)
Split given bytes to two sequences.
Definition: VirgilByteArray.h:160