gpt4 book ai didi

c++ - 将 Crypto++ 对象保存到 std::vector

转载 作者:搜寻专家 更新时间:2023-10-31 00:56:33 24 4
gpt4 key购买 nike

我想将 Crypto++ key 保存到 std::vector<uint8_t> .不幸的是只有CryptoPP::StringSink ,这需要 std::string引用但没有 CryptoPP::VectorSink这将引用 std::vector .

以下代码工作正常

std::string spki;
CryptoPP::StringSink ss(spki);

CryptoPP::RSA::PublicKey publicKey(...);
publicKey.Save(ss);

但我想要这个

std::vector<uint8_t> spki;
CryptoPP::VectorSink vs(spki);

CryptoPP::RSA::PublicKey publicKey(...);
publicKey.Save(vs);

问题

VectorSink由于 traits_type::char_type 不能仅使用 typedef 创建里面StringSinkTemplate :

using CryptoPP::StringSinkTemplate;
typedef StringSinkTemplate< std::vector<byte> > VectorSink;

In file included from cryptopp-test.cpp:65:
In file included from /usr/local/include/cryptopp/files.h:5:
/usr/local/include/cryptopp/filters.h:590:22: error: no member named
'traits_type' in 'std::vector<unsigned char, std::allocator<unsigned char>
>'
typedef typename T::traits_type::char_type char_type;
~~~^
cryptopp-test.cpp:243:20: note: in instantiation of template class
'CryptoPP::StringSinkTemplate<std::vector<unsigned char,
std::allocator<unsigned char> > >' requested here
VectorSink vs(spki);

如何创建 VectorSink

最佳答案

VectorSink 的工作实现

// Written and placed in the public domain by rrmmnn
// Copyright assigned to the Crypto++ project.

namespace CryptoPP {

class VectorSink : public Bufferless<Sink> {
public:

VectorSink(std::vector<uint8_t>& out)
: _out(&out) {
}

size_t Put2(const byte *inString, size_t length, int /*messageEnd*/, bool /*blocking*/) {
_out->insert(_out->end(), inString, inString + length);
return 0;
}

private:
std::vector<uint8_t>* _out;
};

}

关于c++ - 将 Crypto++ 对象保存到 std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39350337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com