我正在尝试使用 Botan 库创建几个 QString 的 SHA-256 散列 - 一个密码和一个盐。我做了很多尝试,试图将连接的 salt+password 转换为正确的类型,以便输入到 Botan 流程函数中。我试图在 Botan 中使用的类的文档位于 botan sha-256 documentation。我最近的尝试是
///
/// Computes salted data hash
///
QByteArray MyClass::HashData(QString salt, QString password)
{
QString concatenated = salt+password;
QByteArray input(concatenated.toLocal8Bit());
Botan::SHA_256 sha;
Botan::SecureVector<Botan::byte> saltedHash
= sha.process(&input,input.count()-1); // -1 to get rid of the \0
return QByteArray(saltedHash);
}
当我编译时我得到一个错误:没有匹配函数调用'Botan::SHA_256::process(QByteArray*, int)'... 候选者是:/usr/include/botan-1.10/botan/buf_comp .h:101: Botan::SecureVector Botan::Buffered_Computation::process(const byte*, size_t)
如何将 QString 或 QByteArray 转换或复制到 const byte*?
编辑: 在我发布问题后,我尝试了更多方法。一个似乎有效的附件附在下面,但我对使用 reinterpret_cast 感到不舒服,因为它看起来可能会导致我在我的 c++ noob 状态下没有意识到的问题。
Botan::SecureVector<Botan::byte> MyClass::HashData(QString salt, QString password)
{
QString concatenated = salt+password;
QByteArray buffer(concatenated.toLocal8Bit());
unsigned char * input = reinterpret_cast< unsigned char*>(buffer.data());
Botan::SHA_256 sha;
Botan::SecureVector<Botan::byte> saltedHash
= sha.process(input,buffer.count()-1); // -1 to get rid of the \0
return (saltedHash);
}
最佳答案
有同样的问题:QByteArray convert to/from unsigned char *
但是你为什么不使用 reinterpret_cast,例如这样:
... QString salt = "salt"; QString password = "password"; QString concatenated = QString("%1%2").arg(salt).arg(password); unsigned char * input = (unsigned char *) concatenated. toLocal8Bit().data(); printf("%s", input); ...
关于c++ - QString 到 const byte* 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12851441/