一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
  在编写程序的时候经常会使用到一些加密的方法,在Qt中,提供了一些常用的加密方法:Md4,Md5,Sha1,Sha224,Sha256,Sha384,Sha512,Sha3_224,Sha3_256,Sha3_384,Sha3_512,如果我们需要使用这些加密方法时,可以直接使用Qt中的QCryptographicHash类进行加密。
 1 #include <QCryptographicHash>
 2 #include <QCoreApplication>
 3 #include <QDebug>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QCoreApplication a(argc, argv);
 8     QByteArray id = QUuid::createUuid().toByteArray();
 9     qDebug() << id;
10     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Md4).toHex();
11     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Md5).toHex();
12     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha1).toHex();
13     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha224).toHex();
14     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha256).toHex();
15     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha384).toHex();
16     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha512).toHex();
17     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha3_224).toHex();
18     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha3_256).toHex();
19     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha3_384).toHex();
20     qDebug() << QCryptographicHash::hash(id, QCryptographicHash::Sha3_512).toHex();
21     return a.exec();
22 }

编译后执行可以看到结果:

 1 liyunlong@liyunlong:~/test$ ./test
 2 "{eb1d57e6-08d6-461d-b939-6bd4e70dfec7}"
 3 "78089e38a3fd1ff9ded18973fca1897c"
 4 "cc789871becfa025f42d72e679d5134d"
 5 "45bcc2a203a8c2ac4b79f857fd516d3871f2447f"
 6 "43e42a2c35bb8cd61fe4fb9cdc85dcb5e8b0ecc3bf9529fd29d1360e"
 7 "c93c4c655c25d553022ef75141fd06118600654f6bc51fb6962ada512a5132bc"
 8 "abd925d43aea8d611fba561192d2de035f20e55f9e087243339da8bcab35215fa1ff901ed14b9b08a4937f39688575e9"
 9 "8f55d056676a8878da67e22076481a481b88afa0ffe80cdd2147a0706c48190d43f9b29256a87317f8a988b984d58fdafbd0eb9fb128e59093e19116955ee9a1"
10 "0d11ee27913bbf634a53e82929d0a8c58e16467a380710f346ef1016"
11 "238af0d1a880a4654c62654b843f248d34d6441138e9c2a0ef918299bde611e4"
12 "7e44e292d94b466f3230f4eb36a0aca44412f7acc241f07d6651199726ae54f463b3a6a11563b04f9a5a64cd4996920a"
13 "dd7765682d1d67e472bc31db7561fbd65e0a818ae702dc263a341230b08fc25c659a3f26def2bf13563a5ecfc7b5b7401163371f7f3c6bef2ae7773b44594a1d"

以后在使用简单的加密时可以采用这种方式,可以免去一些复杂的编写或者其他库的依赖。

posted on 2024-02-19 14:28  一杯清酒邀明月  阅读(150)  评论(0编辑  收藏  举报