在编写程序的时候经常会使用到一些加密的方法,在Qt中,提供了一些常用的加密方法:Md4,Md5,Sha1,Sha224,Sha256,Sha384,Sha512,Sha3_224,Sha3_256,Sha3_384,Sha3_512,如果我们需要使用这些加密方法时,可以直接使用Qt中的QCryptographicHash类进行加密。
#include <QCryptographicHash>
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QByteArray id = QUuid::createUuid().toByteArray();
qDebug() << id;
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Md4).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Md5).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha1).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha224).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha256).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha384).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha512).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha3_224).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha3_256).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha3_384).toHex();
qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha3_512).toHex();
return a.exec();
}
编译后执行可以看到结果:
liyunlong@liyunlong:~/test$ ./test
"{eb1d57e6-08d6-461d-b939-6bd4e70dfec7}"
"78089e38a3fd1ff9ded18973fca1897c"
"cc789871becfa025f42d72e679d5134d"
"45bcc2a203a8c2ac4b79f857fd516d3871f2447f"
"43e42a2c35bb8cd61fe4fb9cdc85dcb5e8b0ecc3bf9529fd29d1360e"
"c93c4c655c25d553022ef75141fd06118600654f6bc51fb6962ada512a5132bc"
"abd925d43aea8d611fba561192d2de035f20e55f9e087243339da8bcab35215fa1ff901ed14b9b08a4937f39688575e9"
"8f55d056676a8878da67e22076481a481b88afa0ffe80cdd2147a0706c48190d43f9b29256a87317f8a988b984d58fdafbd0eb9fb128e59093e19116955ee9a1"
"0d11ee27913bbf634a53e82929d0a8c58e16467a380710f346ef1016"
"238af0d1a880a4654c62654b843f248d34d6441138e9c2a0ef918299bde611e4"
"7e44e292d94b466f3230f4eb36a0aca44412f7acc241f07d6651199726ae54f463b3a6a11563b04f9a5a64cd4996920a"
"dd7765682d1d67e472bc31db7561fbd65e0a818ae702dc263a341230b08fc25c659a3f26def2bf13563a5ecfc7b5b7401163371f7f3c6bef2ae7773b44594a1d"
以后在使用简单的加密时可以采用这种方式,可以免去一些复杂的编写或者其他库的依赖。