博客地址已更改,文章數量較多不便批量修改,若想訪問源文請到 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. 支持的算法
Constant | Value | Description |
---|---|---|
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. 提供的接口
- QCryptographicHash(Algorithm method)
- ~QCryptographicHash()
- void addData(const char *data, int length)
- void addData(const QByteArray &data)
- bool addData(QIODevice *device)
- void reset()
- QByteArray result() const
- static QByteArray hash(const QByteArray &data, Algorithm method)
可以實例化此類,構造時需要提供算法類型,然后通過addData需要計算hash的數據,最后通過result獲取結果,可以利用reset清空數據但不能修改算法。
還給了一個方便易用的靜態方法,直接提供算法類型及數據內容即可。
2. 范例
- #include <QCoreApplication>
- #include <QDebug>
- #include <QCryptographicHash>
- int main(int argc, char *argv[]) {
- QCoreApplication a(argc,argv);
- QString text("test");
- qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash::Md5);//16進制結果
- qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash::Md5).toHex();//轉換為字符串
- qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash::Keccak_512);//16進制結果
- qDebug()<<QCryptographicHash::hash(text.toLatin1(),QCryptographicHash::Keccak_512).toHex();//轉換為字符串
- return 0;
- }
結果
- "\t\x8Fk\xCD""F!\xD3s\xCA\xDEN\x83&'\xB4\xF6"
- "098f6bcd4621d373cade4e832627b4f6"
- "\x1E.\x9F\xC2\x00+\x00-u\x19\x8Bu\x03!\f\x05\xA1\xBA\xAC""E`\x91j<m\x93\xBC\xCE:P\xD7\xF0\x0F\xD3\x95\xBF\x16G\xB9\xAB\xB8\xD1\xAF\xCC\x9Cv\xC2\x89\xB0\xC9""8;\xA3\x86\xA9V\xDAK8\x93""D\x17x\x9E"
- "1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e"
其中test計算md5的結果是098f6bcd4621d373cade4e832627b4f6 可以在相關網站反查結果:http://www.cmd5.com/,可以證明計算正確。
轉載請以鏈接形式標明本文標題和地址:
Techie亮博客 »
QCryptographicHash實現哈希值計算,支持多種算法