TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicalgorithmutils.cpp
Go to the documentation of this file.
1 #include "musicalgorithmutils.h"
2 #include "ttkcryptographichash.h"
3 
4 #include <QCryptographicHash>
5 
6 QByteArray TTK::Algorithm::md5(const QByteArray &data)
7 {
8  return QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex().toLower();
9 }
10 
11 QByteArray TTK::Algorithm::sha1(const QByteArray &data)
12 {
13  return QCryptographicHash::hash(data, QCryptographicHash::Sha1);
14 }
15 
16 QByteArray TTK::Algorithm::hmacSha1(const QByteArray &data, const QByteArray &key)
17 {
18  const int blockSize = 64;
19  QByteArray newSecretKey = key;
20  if(newSecretKey.length() > blockSize)
21  {
22  newSecretKey = TTK::Algorithm::sha1(newSecretKey);
23  }
24 
25  QByteArray innerPadding(blockSize, char(0x36));
26  QByteArray outerPadding(blockSize, char(0x5C));
27 
28  for(int i = 0; i < key.length(); ++i)
29  {
30  innerPadding[i] = innerPadding[i] ^ key.at(i);
31  outerPadding[i] = outerPadding[i] ^ key.at(i);
32  }
33 
34  QByteArray total = outerPadding;
35  QByteArray part = innerPadding;
36  part.append(data);
37  total.append(TTK::Algorithm::sha1(part));
38  return TTK::Algorithm::sha1(total);
39 }
40 
41 QString TTK::Algorithm::mdII(const QString &data, bool encode)
42 {
44  return encode ? hash.encrypt(data, ALG_URL_KEY) : hash.decrypt(data, ALG_URL_KEY);
45 }
46 
47 QString TTK::Algorithm::mdII(const QString &data, const QString &key, bool encode)
48 {
50  return encode ? hash.encrypt(data, key) : hash.decrypt(data, key);
51 }
TTK_MODULE_EXPORT QByteArray md5(const QByteArray &data)
static constexpr wchar_t key[]
TTK_MODULE_EXPORT QByteArray hmacSha1(const QByteArray &data, const QByteArray &key)
QString encrypt(const QString &data, const QString &key)
The class of the string cryptographic hash.
TTK_MODULE_EXPORT QByteArray sha1(const QByteArray &data)
static constexpr const char * ALG_URL_KEY
QString decrypt(const QString &data, const QString &key)
TTK_MODULE_EXPORT QString mdII(const QString &data, bool encode)