TTKMusicPlayer  4.3.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, bool base64)
7 {
8  const QByteArray &v = QCryptographicHash::hash(data, QCryptographicHash::Md5);
9  return base64 ? v.toBase64() : v.toHex().toLower();
10 }
11 
12 QByteArray TTK::Algorithm::sha1(const QByteArray &data)
13 {
14  return QCryptographicHash::hash(data, QCryptographicHash::Sha1);
15 }
16 
17 QByteArray TTK::Algorithm::hmacSha1(const QByteArray &data, const QByteArray &key)
18 {
19  const int blockSize = 64;
20  QByteArray newSecretKey = key;
21  if(newSecretKey.length() > blockSize)
22  {
23  newSecretKey = TTK::Algorithm::sha1(newSecretKey);
24  }
25 
26  QByteArray innerPadding(blockSize, char(0x36));
27  QByteArray outerPadding(blockSize, char(0x5C));
28 
29  for(int i = 0; i < key.length(); ++i)
30  {
31  innerPadding[i] = innerPadding[i] ^ key.at(i);
32  outerPadding[i] = outerPadding[i] ^ key.at(i);
33  }
34 
35  QByteArray total = outerPadding;
36  QByteArray part = innerPadding;
37  part.append(data);
38  total.append(TTK::Algorithm::sha1(part));
39  return TTK::Algorithm::sha1(total);
40 }
41 
42 QString TTK::Algorithm::mdII(const QString &data, bool encode)
43 {
45  return encode ? hash.encrypt(data, MDII_URL_KEY) : hash.decrypt(data, MDII_URL_KEY);
46 }
47 
48 QString TTK::Algorithm::mdII(const QString &data, const char *key, bool encode)
49 {
51  return encode ? hash.encrypt(data, key) : hash.decrypt(data, key);
52 }
static constexpr const char * MDII_URL_KEY
TTK_MODULE_EXPORT QByteArray hmacSha1(const QByteArray &data, const QByteArray &key)
TTK_MODULE_EXPORT QByteArray md5(const QByteArray &data, bool base64=false)
static constexpr wchar_t key[]
The class of the string cryptographic hash.
TTK_MODULE_EXPORT QByteArray sha1(const QByteArray &data)
QString decrypt(const QString &data, const char *key)
QString encrypt(const QString &data, const char *key)
TTK_MODULE_EXPORT QString mdII(const QString &data, bool encode)