【发布时间】:2012-10-02 19:39:17
【问题描述】:
我正在尝试使用 Botan 库创建几个 QStrings 的 SHA-256 哈希 - 一个密码和一个盐。我做了很多尝试,试图将连接的盐+密码转换为正确的类型,以便输入到 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);
}
【问题讨论】:
-
你的转换应该没问题,因为 buffer.data() 返回 char* 来表示字符(不能是负值)。在这种情况下,plain char 和 unsigned char 是相同的。 stackoverflow.com/questions/2054939/…