如何将两个字节*(秒块)与运算符连接起来'secblock<T, A>::operator+='

How can I concatenate two byte* (secblock) with operator 'secblock<T, A>::operator+='

本文关键字:secblock operator+ lt gt 连接 两个 字节 运算符 秒块 起来      更新时间:2023-10-16

请举个小例子。我试着在文档中使用这个,但我不明白如何使用。

消息:

main.cpp|97|error: no matching function for call to 
   'CryptoPP::SecBlock<unsigned char>::operator+=(CryptoPP::SecBlock<unsigned char>*)'
   secblock.h|568|note: candidate: 
   CryptoPP::SecBlock<T, A>& CryptoPP::SecBlock<T, A>::operator+=(const CryptoPP::SecBlock<T, A>&)
   [with T = unsigned char; A = CryptoPP::AllocatorWithCleanup<unsigned char>]
   secblock.h|568|note:   
   no known conversion for argument 1 from 'CryptoPP::SecBlock<unsigned char>*' 
   to 'const CryptoPP::SecBlock<unsigned char>&'

我的代码:

SecBlock<byte, AllocatorWithCleanup<byte> > hash_ripemd160_temp;
  RIPEMD160().CalculateDigest(hash_ripemd160_temp, hash_sha256, 32);
  SecBlock<byte, AllocatorWithCleanup<byte> > hash_ripemd160 = L0_byte;
   hash_ripemd160 = SecBlock< byte , AllocatorWithCleanup<byte > >::operator+= (&hash_ripemd160_temp);

In Docs为:

SecBlock<byte , AllocatorWithCleanup<byte > >& SecBlock< byte , AllocatorWithCleanup<byte > >::operator+= (const SecBlock< byte , AllocatorWithCleanup<byte > > &t)     
Append contents from another SecBlock.
Parameters
t   the other SecBlock
Internally, this SecBlock calls Grow and then copies the new content.
If the memory block is reduced in size, then the unused area is set to 0.

文件secblock.h第568行的定义。

调用运算符函数的最简单方法是只使用运算符:

  hash_ripemd160 += hash_ripemd160_temp;

如果你想直接调用它(我不建议),你必须这样调用它,因为它是一个成员函数:

hash_ripemd160.operator += (hash_ripemd160_temp);

如何将两个字节*(secblock)与operator 'secblock<T, A>::operator+=' 连接

在提取提交605744d8260c6ada之后,请尝试以下。(或者使用git clone https://github.com/weidai11/cryptopp.git执行新的签出)。

$ cat test.cxx 
#include "ripemd.h"
#include "files.h"
#include "hex.h"
using namespace CryptoPP;
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
  SecByteBlock digest(RIPEMD160::DIGESTSIZE);
  RIPEMD160 hash;
  std::string message("now is the time for all good men to come to the aide of their country");
  HexEncoder hexer(new FileSink(cout));
  // RIPEMD-160
  hash.Update((const byte*)message.data(), message.size());
  hash.TruncatedFinal(digest, digest.size());
  cout << "RIPEMD-160: ";
  hexer.Put(digest, digest.size());
  cout << endl;
  // Double it
  digest += digest;
  cout << "RIPEMD-160 (x2): ";
  hexer.Put(digest, digest.size());
  cout << endl;
  return 0;
}

你和Alan发现了assert中的拼写错误。我不知道自检没有锻炼代码路径。现在已经修复了,我们打开了另一个问题来解决工程过程中的缺陷:需要在发布过程中添加一个代码覆盖工具。


您还发现了自连接(即digest += digest)中的一个讨厌的小错误。已将其更改为以下内容以检测它:

SecBlock<T, A>& operator+=(const SecBlock<T, A> &t)
{
    assert((!t.m_ptr && !t.m_size) || (t.m_ptr && t.m_size));
    if(t.m_size)
    {
        if(this != &t)  // s += t
        {
            const size_type oldSize = m_size;
            Grow(m_size+t.m_size);
            memcpy_s(m_ptr+oldSize, (m_size-oldSize)*sizeof(T), t.m_ptr, t.m_size*sizeof(T));
        }
        else            // t += t
        {
            SecBlock result(m_size+t.m_size);
            if(m_size) {memcpy_s(result.m_ptr, result.m_size*sizeof(T), m_ptr, m_size*sizeof(T));}
            memcpy_s(result.m_ptr+m_size, (result.m_size-m_size)*sizeof(T), t.m_ptr, t.m_size*sizeof(T));
            swap(result);
        }
    }
    return *this;
}

在修复/提交之前,在大多数情况下,它似乎应该触发InvalidArgument异常。然而,Windows上有一些我们不太确定的角落案例,可能会导致静默截断。

静默截断令人担忧,我们正在讨论Crypto++5.6.4版本。

删除hash_ripemd160_temp 之前的&

hash_ripemd160 = SecBlock< byte , AllocatorWithCleanup<byte > >::operator+= (hash_ripemd160_temp);

运算符接受对象,而不是指针。