如何将两个字节*(秒块)与运算符连接起来'secblock<T, A>::operator+='
How can I concatenate two byte* (secblock) with operator 'secblock<T, A>::operator+='
请举个小例子。我试着在文档中使用这个,但我不明白如何使用。
消息:
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);
运算符接受对象,而不是指针。
相关文章:
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 'operator='已弃用:改用 QDir::setPath()
- 过载'operator new'如何导致无限循环?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 处理"no operator found"
- 如何编写 operator= 用于使用虚拟方法与非平凡成员的匿名联合
- 运算符重载:"operator+"必须采用零个或一个参数
- 使用 operator() 扩展 Eigen::EigenBase
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 有没有办法修改'operator->',以便'z->im'返回复数的虚部
- "operator +="行为异常的定义
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- 为什么 operator() 处的指针成员不起作用?
- 为什么 string_view::operator== 按值接受参数
- "operator()"在重载运算符方法中是什么意思,在priority_queue(STL)中用作C++中的比较器?
- Valarray 切片数组与 'operator*' 不匹配
- 如何将两个字节*(秒块)与运算符连接起来'secblock<T, A>::operator+='