32 static std::unique_ptr<CryptoDecoder>
fromDER(uint8_t* derData,
size_t derDataLen,
bool ownDerData =
false)
34 return std::unique_ptr<CryptoDecoder>(
new CryptoDecoder(derData, derDataLen, ownDerData));
41 static std::unique_ptr<CryptoDecoder>
fromDER(
const std::string& derData)
43 size_t derDataBufferLen = derData.length() / 2;
44 auto derDataBuffer = std::make_unique<uint8_t[]>(derDataBufferLen);
46 return std::unique_ptr<CryptoDecoder>(
new CryptoDecoder(std::move(derDataBuffer), derDataBufferLen));
53 static std::unique_ptr<CryptoDecoder>
fromDERFile(
const std::string& derFileName)
55 std::ifstream derFile(derFileName, std::ios::binary);
58 throw std::runtime_error(
"DER file doesn't exist or cannot be opened");
61 derFile.seekg(0, std::ios::end);
62 std::streamsize derDataLen = derFile.tellg();
65 throw std::runtime_error(
"Failed to determine DER file size");
67 derFile.seekg(0, std::ios::beg);
69 auto derData = std::make_unique<uint8_t[]>(derDataLen);
71 if (!derFile.read(
reinterpret_cast<char*
>(derData.get()), derDataLen))
73 throw std::runtime_error(
"Failed to read DER file");
75 return std::unique_ptr<CryptoDecoder>(
new CryptoDecoder(std::move(derData), derDataLen));
82 static std::unique_ptr<CryptoDecoder>
fromPEM(
const std::string& pemData)
85 auto derDataBuffer = std::make_unique<uint8_t[]>(derData.size());
86 std::copy(derData.begin(), derData.end(), derDataBuffer.get());
87 return std::unique_ptr<CryptoDecoder>(
new CryptoDecoder(std::move(derDataBuffer), derData.size()));
95 static std::unique_ptr<CryptoDecoder>
fromPEMFile(
const std::string& pemFileName)
97 std::ifstream pemFile(pemFileName, std::ios::in | std::ios::binary);
100 throw std::runtime_error(
"PEM file doesn't exist or cannot be opened");
103 pemFile.seekg(0, std::ios::end);
104 std::streamsize pemContentLen = pemFile.tellg();
105 if (pemContentLen < 0)
107 throw std::runtime_error(
"Failed to determine PEM file size");
109 pemFile.seekg(0, std::ios::beg);
111 std::string pemContent;
112 pemContent.resize(
static_cast<std::size_t
>(pemContentLen));
113 if (!pemFile.read(&pemContent[0], pemContentLen))
115 throw std::runtime_error(
"Failed to read PEM file");
static std::vector< uint8_t > decode(const std::string &pemData, const std::string &expectedLabel="")
A template helper class for reading and decoding cryptographic data in different formats (DER/PEM)
Definition: CryptoDataReader.h:23
static std::unique_ptr< CryptoDecoder > fromDER(uint8_t *derData, size_t derDataLen, bool ownDerData=false)
Definition: CryptoDataReader.h:32
static std::unique_ptr< CryptoDecoder > fromPEM(const std::string &pemData)
Definition: CryptoDataReader.h:82
static std::unique_ptr< CryptoDecoder > fromPEMFile(const std::string &pemFileName)
Definition: CryptoDataReader.h:95
static std::unique_ptr< CryptoDecoder > fromDERFile(const std::string &derFileName)
Definition: CryptoDataReader.h:53
static std::unique_ptr< CryptoDecoder > fromDER(const std::string &derData)
Definition: CryptoDataReader.h:41
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
size_t hexStringToByteArray(const std::string &hexString, uint8_t *resultByteArr, size_t resultByteArrSize)