本文介绍了将Crypto ++对象保存到std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to save Crypto++ keys to std::vector<uint8_t>. Unfortunately there is only CryptoPP::StringSink, that takes std::string reference but no CryptoPP::VectorSink that would take a reference to 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

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;
};

}

这篇关于将Crypto ++对象保存到std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 20:23