zoukankan      html  css  js  c++  java
  • QCryptographicHash实现哈希值计算,支持多种算法

    博客地址已更改,文章数量较多不便批量修改,若想访问源文请到 coologic博客 查阅,网址:www.coologic.cn

    如本文记录地址为 techieliang.com/A/B/C/ 请改为 www.coologic.cn/A/B/C/ 即可查阅

    版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
    本文标题:QCryptographicHash实现哈希值计算,支持多种算法     本文地址:http://techieliang.com/2017/12/668/

    1. 介绍

    多看看Qt core模块会发现很多惊喜呀,里面包含的类很多涉及到很多方面的功能实现

    先附上所有core类:Qt Core,再直接给出QCryptographicHash的帮助:QCryptographicHash

    此类用于提供密码散列,哈希值。可以生成二进制或文本形式的hash值,并支持多种算法,算法可以由QCryptographicHash::Algorithm选择

    1.1. 支持的算法

    ConstantValueDescription
    QCryptographicHash::Md4 0 Generate an MD4 hash sum
    QCryptographicHash::Md5 1 Generate an MD5 hash sum
    QCryptographicHash::Sha1 2 Generate an SHA-1 hash sum
    QCryptographicHash::Sha224 3 Generate an SHA-224 hash sum (SHA-2). Introduced in Qt 5.0
    QCryptographicHash::Sha256 4 Generate an SHA-256 hash sum (SHA-2). Introduced in Qt 5.0
    QCryptographicHash::Sha384 5 Generate an SHA-384 hash sum (SHA-2). Introduced in Qt 5.0
    QCryptographicHash::Sha512 6 Generate an SHA-512 hash sum (SHA-2). Introduced in Qt 5.0
    QCryptographicHash::Sha3_224 RealSha3_224 Generate an SHA3-224 hash sum. Introduced in Qt 5.1
    QCryptographicHash::Sha3_256 RealSha3_256 Generate an SHA3-256 hash sum. Introduced in Qt 5.1
    QCryptographicHash::Sha3_384 RealSha3_384 Generate an SHA3-384 hash sum. Introduced in Qt 5.1
    QCryptographicHash::Sha3_512 RealSha3_512 Generate an SHA3-512 hash sum. Introduced in Qt 5.1
    QCryptographicHash::Keccak_224 7 Generate a Keccak-224 hash sum. Introduced in Qt 5.9.2
    QCryptographicHash::Keccak_256 8 Generate a Keccak-256 hash sum. Introduced in Qt 5.9.2
    QCryptographicHash::Keccak_384 9 Generate a Keccak-384 hash sum. Introduced in Qt 5.9.2
    QCryptographicHash::Keccak_512 10 Generate a Keccak-512 hash sum. Introduced in Qt 5.9.2

    1.2. 提供的接口

    1. QCryptographicHash(Algorithm method)
    2. ~QCryptographicHash()
    3. void addData(const char *data, int length)
    4. void addData(const QByteArray &data)
    5. bool addData(QIODevice *device)
    6. void reset()
    7. QByteArray result() const
    8.  
    9. static QByteArray hash(const QByteArray &data, Algorithm method)

    可以实例化此类,构造时需要提供算法类型,然后通过addData需要计算hash的数据,最后通过result获取结果,可以利用reset清空数据但不能修改算法。

    还给了一个方便易用的静态方法,直接提供算法类型及数据内容即可。

    2. 范例

    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QCryptographicHash>
    4. int main(int argc, char *argv[]) {
    5. QCoreApplication a(argc,argv);
    6. QString text("test");
    7. qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash::Md5);//16进制结果
    8. qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash::Md5).toHex();//转换为字符串
    9. qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash::Keccak_512);//16进制结果
    10. qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash::Keccak_512).toHex();//转换为字符串
    11. return 0;
    12. }

    结果

    1. " x8FkxCD""F!xD3sxCAxDENx83&'xB4xF6"
    2. "098f6bcd4621d373cade4e832627b4f6"
    3. "x1E.x9FxC2x00+x00-ux19x8Bux03!fx05xA1xBAxAC""E`x91j<mx93xBCxCE:PxD7xF0x0FxD3x95xBFx16GxB9xABxB8xD1xAFxCCx9CvxC2x89xB0xC9""8;xA3x86xA9VxDAK8x93""Dx17xx9E"
    4. "1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e"

    其中test计算md5的结果是098f6bcd4621d373cade4e832627b4f6 可以在相关网站反查结果:http://www.cmd5.com/,可以证明计算正确。

    转载请以链接形式标明本文标题和地址:Techie亮博客 » QCryptographicHash实现哈希值计算,支持多种算法
  • 相关阅读:
    __doPostBack的使用
    【转】function,new,constructor and prototye
    谈谈一些网页游戏失败的原因到底有哪些?(转)
    全面剖析页游巨头发家史(转)
    2013.02.20开通博客
    老子喜欢的女人
    如何成为强大的程序员?(转)
    注重健康
    学习方法总结
    数据库知识点滴积累
  • 原文地址:https://www.cnblogs.com/techiel/p/8027587.html
Copyright © 2011-2022 走看看