Virgil Security Crypto library  2.1.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 
47 namespace virgil { namespace crypto {
48 
53 typedef std::vector<unsigned char> VirgilByteArray;
54 
55 }}
56 
60 #define VIRGIL_BYTE_ARRAY_TO_PTR_AND_LEN(array) reinterpret_cast<const unsigned char *>(array.data()), array.size()
62 
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)))
66 
68 namespace virgil { namespace crypto {
69 
73 inline VirgilByteArray str2bytes(const std::string& str) {
74  return VIRGIL_BYTE_ARRAY_FROM_PTR_AND_LEN(str.data(), str.size());
75 }
76 
80 inline std::string bytes2str(const VirgilByteArray& array) {
81  return std::string(reinterpret_cast<const char*>(array.data()), array.size());
82 }
83 
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)) {
94  int byte = 0;
95  std::istringstream(hexChars) >> std::hex >> byte;
96  result.push_back((unsigned char) byte);
97  }
98  return result;
99 }
100 
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];
113  if (formatted) {
114  hexStream << (((i + 1) % 16 == 0) ? "\n" : " ");
115  }
116  }
117  return hexStream.str();
118 }
122 
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; }
130 }
131 
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'; }
139 }
141 
142 }}
143 #endif /* VIRGIL_BYTE_ARRAY_H */
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