c++ - QString 到 const byte* 转换

标签 c++ qt botan

我正在尝试使用 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/

相关文章:

c++ - 如何处理读入 ASIO streambuf 的额外字符?

c++ - 具有边界检查的 char [] 安全拷贝的专用模板

c++ - 线程的 worker Qt中的定时器

security - 为什么这些 Botan 公钥如此相似?

c++ - 使用 dll 的 Heisenbug 问题。接下来我该做什么?

c++ - 列表初始化分配给二维数组 EXC_BAD_ACCESS 错误

android - 设置 QTimer 间隔时 Qt 应用程序崩溃

c++ - 如何在QTreeView的第二列添加项目

c++ - 使用 Botan 和 Qt 加密文件时出错

c++ - Botan 加载现有的 RSA 私钥