• 5906阅读
  • 1回复

[提问]HMAC-SHA1 这段加密完为什么和其他语言的不一样呢? [复制链接]

上一主题 下一主题
离线feixing1
 

只看楼主 倒序阅读 楼主  发表于: 2011-11-03
  1. QString hmacSha1(QByteArray key, QByteArray baseString)
  2. {
  3.     int blockSize = 64; // HMAC-SHA-1 block size, defined in SHA-1 standard
  4.     if (key.length() > blockSize) { // if key is longer than block size (64), reduce key length with SHA-1 compression
  5.         key = QCryptographicHash::hash(key, QCryptographicHash::Sha1);
  6.     }
  7.     QByteArray innerPadding(blockSize, char(0x36)); // initialize inner padding with char "6"
  8.     QByteArray outerPadding(blockSize, char(0x5c)); // initialize outer padding with char "\"
  9.     // ascii characters 0x36 ("6") and 0x5c ("\") are selected because they have large
  10.     // Hamming distance (http://en.wikipedia.org/wiki/Hamming_distance)
  11.     for (int i = 0; i < key.length(); i++) {
  12.         innerPadding[i] = innerPadding[i] ^ key.at(i); // XOR operation between every byte in key and innerpadding, of key length
  13.         outerPadding[i] = outerPadding[i] ^ key.at(i); // XOR operation between every byte in key and outerpadding, of key length
  14.     }
  15.     // result = hash ( outerPadding CONCAT hash ( innerPadding CONCAT baseString ) ).toBase64
  16.     QByteArray total = outerPadding;
  17.     QByteArray part = innerPadding;
  18.     part.append(baseString);
  19.     total.append(QCryptographicHash::hash(part, QCryptographicHash::Sha1));
  20.     QByteArray hashed = QCryptographicHash::hash(total, QCryptographicHash::Sha1);
  21.     return hashed.toBase64();
  22. }

http://developer.qt.nokia.com/wiki/HMAC-SHA1 诺基亚官网上给出的一段代码,但是我尝试了一下好像加密完的字符串和其他语言不一样,高手能帮我看下是哪里出错了么?谢谢
离线steamx

只看该作者 1楼 发表于: 2015-06-29
/**
* Hashes the given string using the HMAC-SHA1 algorithm.
*
* \param key The string to be hashed
* \param secret The string that contains secret word
* \return The hashed string
*/
static QString hmac_sha1(const QString &key,
                       const QString &secret)
{
   //Length of the text, that will be hashed
   int   text_lenght;
    
   //For secret word.
   QByteArray K;
   //Lenght of secret word
   int   K_lenght;
    
   K_lenght = secret.size();
   text_lenght = key.size();
    
   //Need to do for XOR operation. Transforms QString to
   //unsigned char
    
    
   K = secret.toAscii();
    
   //Inner padding
   QByteArray ipad;
   //Outer pad
   QByteArray opad;
    
   //if secret key > 64 bytes use this to obtain sha1 key
   if(K_lenght > 64)
   {
      QByteArray tempSecret;
      
      tempSecret.append(secret);
      
      K = QCryptographicHash::hash(tempSecret,
                                  QCryptographicHash::Sha1);
      
      K_lenght = 20;
   }
    
   //Fills ipad and opad with zeros
   //bzero( ipad, sizeof ipad);
   //bzero( opad, sizeof opad);
    
   ipad.fill( 0, 64);
   opad.fill(0, 64);
    
   //Copies Secret to ipad and opad
   //bcopy( K, ipad, K_lenght);
   //bcopy( K, opad, K_lenght);
    
   ipad.replace(0, K_lenght, K);
   opad.replace(0, K_lenght, K);
    
    
    
   //XOR operation for inner and outer pad
   for (int i=0; i<64; i++)
   {
      ipad = ipad ^ 0x36;
      opad = opad ^ 0x5c;
   }
    
   //Stores hashed content
   QByteArray context;
    
   //Appends XOR:ed ipad to context
   context.append(ipad,64);
   //Appends key to context
   context.append(key);
    
   //Hashes Inner pad
   QByteArray Sha1   = QCryptographicHash::hash(context,
                                                QCryptographicHash::Sha1);
    
   context.clear();
   //Appends opad to context
   context.append(opad,64);
   //Appends hashed inner pad to context
   context.append(Sha1);
    
   Sha1.clear();
    
   // Hashes outerpad
   Sha1 = QCryptographicHash::hash(context,
                                   QCryptographicHash::Sha1);
    
   // String to return hashed stuff in Base64 format
   QByteArray str(Sha1.toBase64());
    
   return str;
}
快速回复
限100 字节
 
上一个 下一个