c++代码实现QCryptographicHash::hash

有一段qt代码:

QByteArray ba;
ba = QCryptographicHash::hash(ba, QCryptographicHash::Md5);
QString ret = ba.toHex().toUpper();

现要用c++实现这段代码,
给定std::string ba;得到std::string ret

需要怎么实现

该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
以下是用C++实现QCryptographicHash::hash的示例代码:

#include <openssl/md5.h>
#include <cstring>
#include <string>
#include <sstream>
#include <iomanip>

std::string QCryptographicHash(const std::string& input, const QCryptographicHash::Algorithm algorithm)
{
    std::string result;
    unsigned char hash[MD5_DIGEST_LENGTH];
    memset(hash, 0, sizeof(hash));

    switch (algorithm) {
        case QCryptographicHash::Md5:
            MD5(reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), hash);
            result.resize(MD5_DIGEST_LENGTH);
            memcpy(&result[0], hash, MD5_DIGEST_LENGTH);
            break;
        case QCryptographicHash::Sha1:
            // TODO: Implement SHA1 hash
            break;
        case QCryptographicHash::Sha256:
            // TODO: Implement SHA256 hash
            break;
        case QCryptographicHash::Sha512:
            // TODO: Implement SHA512 hash
            break;
        default:
            // Invalid algorithm
            break;
    }

    std::stringstream ss;
    ss << std::hex << std::uppercase << std::setfill('0');
    for (unsigned char h : hash) {
        ss << std::setw(2) << static_cast<unsigned int>(h);
    }
    result = ss.str();
    return result;
}

这个示例代码使用了OpenSSL库来实现MD5哈希算法。我们可以传递一个字符串作为输入和所需的算法,然后返回一个十六进制字符串作为哈希结果。

要使用这个函数来替代上面的Qt代码,可以这样做:

std::string ba = "your input string";
std::string ret = QCryptographicHash(ba, QCryptographicHash::Md5);

请注意,这个函数只实现了MD5算法。如果您需要其他哈希算法,您需要根据需要实现它们,并将它们添加到switch语句中。


如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

这个有讲实现QCryptographicHash::hash的代码:

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答由chatgpt基于相关博客总结生成:

    根据参考资料的内容,可以按照以下步骤在C++中实现Qt的QCryptographicHash::hash函数:

    1. 引入Qt的QCryptographicHash头文件:
    #include <QCryptographicHash>
    
    1. 定义输入数据和散列算法:
    QByteArray input = "password";  // 输入数据
    QCryptographicHash::Algorithm algorithm = QCryptographicHash::Md5;  // 散列算法
    
    1. 使用静态公共函数QCryptographicHash::hash计算哈希值:
    QByteArray hash = QCryptographicHash::hash(input, algorithm);  // 计算哈希值
    QString strHash = hash.toHex();  // 转换为16进制字符串
    

    完整的代码示例如下:

    #include <QCoreApplication>
    #include <QCryptographicHash>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QByteArray input = "password";  // 输入数据
        QCryptographicHash::Algorithm algorithm = QCryptographicHash::Md5;  // 散列算法
    
        QByteArray hash = QCryptographicHash::hash(input, algorithm);
        QString strHash = hash.toHex();
    
        qDebug() << "Hash value: " << strHash;
    
        return a.exec();
    }
    

    这样就可以在C++中实现Qt的QCryptographicHash::hash函数了。运行该程序,可以得到哈希值的输出结果。

    注:需要注意的是,此示例中使用的是Qt的QCryptographicHash类,如果你不在Qt环境下使用,可能会编译出错。如果你需要在其他环境下实现该功能,可以考虑使用其他开源的哈希函数库(如:OpenSSL)或者自行实现相应的哈希函数算法。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^