返回顶部

收藏

Qt HMAC hash

更多

Qt HMAC hash 算法

[C/C++]代码

QString hmacSha1(QByteArray key, QByteArray baseString)
 {
     int blockSize = 64; // HMAC-SHA-1 block size, defined in SHA-1 standard
     if (key.length() > blockSize) { // if key is longer than block size (64), reduce key length with SHA-1 compression
         key = QCryptographicHash::hash(key, QCryptographicHash::Sha1);
     }
     QByteArray innerPadding(blockSize, char(0x36)); // initialize inner padding with char "6"
     QByteArray outerPadding(blockSize, char(0x5c)); // initialize outer padding with char "/"
     // ascii characters 0x36 ("6") and 0x5c ("/") are selected because they have large
     // Hamming distance (http://en.wikipedia.org/wiki/Hamming_distance)
     for (int i = 0; i < key.length(); i++) {
         innerPadding[i] = innerPadding[i] ^ key.at(i); // XOR operation between every byte in key and innerpadding, of key length
         outerPadding[i] = outerPadding[i] ^ key.at(i); // XOR operation between every byte in key and outerpadding, of key length
     }
     // result = hash ( outerPadding CONCAT hash ( innerPadding CONCAT baseString ) ).toBase64
     QByteArray total = outerPadding;
     QByteArray part = innerPadding;
     part.append(baseString);
     total.append(QCryptographicHash::hash(part, QCryptographicHash::Sha1));
     QByteArray hashed = QCryptographicHash::hash(total, QCryptographicHash::Sha1);
     return hashed.toBase64();
 }

标签:QT

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 豆子 发表 2018-07-23 06:23:39 带有加密功能的 SQLite Qt 插件(v1.0)
  2. xinlu 发表 2018-07-05 08:33:52 使用匿名函数,简易地把同步转异步
  3. xinlu 发表 2018-06-28 11:43:30 Xperf与ETW脚本
  4. xinlu 发表 2018-06-24 10:32:09 预读处理
  5. 华华 发表 2018-06-12 00:21:49 LinuxStory 资讯速递 20180612 F2FS 针对 Linux 4.18 内核的更新已
  6. xinlu 发表 2018-04-19 20:54:07 cef的Qt的封装库及webrtc的测试
  7. xinlu 发表 2018-04-19 04:32:14 带动态链接库的启动脚本
  8. xinlu 发表 2018-03-13 09:57:54 Apple相关离线下载
  9. xinlu 发表 2018-02-12 04:28:40 Qt的国际语言怎么用
  10. xinlu 发表 2018-02-11 09:50:08 Qt的Centos7安装编译错误
  11. eryar 发表 2018-02-05 14:50:00 Qt源码编译
  12. 豆子 发表 2017-05-08 15:18:21 Qt Creator 源码学习 09:IPlugin

发表评论