0

我正在尝试使用 Botan 库创建几个 QStrings 的 SHA-256 哈希 - 一个密码和一个盐。我做了很多尝试,试图将连接的盐+密码转换为正确的类型,以便输入到 Botan 进程函数中。我试图在 Botan 中使用的类的文档可以在botan sha-256 文档中找到 我最近的尝试是

///
/// 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);
}
4

2 回答 2

1

有同样的问题: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);
...
于 2012-10-12T17:02:09.660 回答
0

您可以使用以下方法。

const char * QByteArray::data()
于 2012-10-12T03:16:44.793 回答