generic: use QCryptographicHash instead of KMD5

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2016-04-01 04:30:28 +00:00
parent 80f1debded
commit ccfed4ed1d
6 changed files with 62 additions and 106 deletions

View file

@ -37,8 +37,8 @@
#include <QPixmap>
#include <QLibrary>
#include <QDirIterator>
#include <QCryptographicHash>
#include <kcodecs.h>
#include <kurl.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
@ -708,8 +708,8 @@ bool ThumbnailProtocol::createSubThumbnail(QImage& thumbnail, const QString& fil
// check whether a cached version of the file is available for
// 128 x 128 or 256 x 256 pixels
int cacheSize = 0;
KMD5 md5(QFile::encodeName(fileName.url()));
const QString thumbName = QFile::encodeName(md5.hexDigest()) + ".png";
QByteArray md5 = QCryptographicHash::hash(QFile::encodeName(fileName.url()), QCryptographicHash::Md5);
const QString thumbName = QFile::encodeName(md5.toHex()) + ".png";
if (m_thumbBasePath.isEmpty()) {
m_thumbBasePath = QDir::homePath() + "/.thumbnails/";
KStandardDirs::makeDir(m_thumbBasePath + "normal/", 0700);

View file

@ -35,7 +35,6 @@
#include <kglobal.h>
#include <klocale.h>
#include <kcodecs.h>
#include <kstandarddirs.h>
#include <kstringhandler.h>
#include <ktemporaryfile.h>

View file

@ -20,11 +20,12 @@
#include <QIODevice>
#include <QFile>
#include <QTextDocument>
#include <assert.h>
#include <QCryptographicHash>
#include <ksavefile.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <klocalizedstring.h>
#include <assert.h>
#ifdef HAVE_QGPGME
#include <gpgme.h>
#include <gpgme++/context.h>
@ -189,7 +190,7 @@ int BlowfishPersistHandler::write(Backend* wb, KSaveFile& sf, QByteArray& versio
// Holds the hashes we write out
QByteArray hashes;
QDataStream hashStream(&hashes, QIODevice::WriteOnly);
KMD5 md5;
QCryptographicHash *md5 = new QCryptographicHash(QCryptographicHash::Md5);
hashStream << static_cast<quint32>(wb->_entries.count());
// Holds decrypted data prior to encryption
@ -205,9 +206,9 @@ int BlowfishPersistHandler::write(Backend* wb, KSaveFile& sf, QByteArray& versio
dStream << i.key();
dStream << static_cast<quint32>(i.value().count());
md5.reset();
md5.update(i.key().toUtf8());
hashStream.writeRawData(reinterpret_cast<const char*>(&(md5.rawDigest()[0])), 16);
md5->reset();
md5->addData(i.key().toUtf8());
hashStream.writeRawData(md5->result().toHex(), 16);
hashStream << static_cast<quint32>(i.value().count());
for (Backend::EntryMap::ConstIterator j = i.value().constBegin(); j != i.value().constEnd(); ++j) {
@ -215,9 +216,9 @@ int BlowfishPersistHandler::write(Backend* wb, KSaveFile& sf, QByteArray& versio
dStream << static_cast<qint32>(j.value()->type());
dStream << j.value()->value();
md5.reset();
md5.update(j.key().toUtf8());
hashStream.writeRawData(reinterpret_cast<const char*>(&(md5.rawDigest()[0])), 16);
md5->reset();
md5->addData(j.key().toUtf8());
hashStream.writeRawData(md5->result().toHex(), 16);
}
}
@ -323,19 +324,17 @@ int BlowfishPersistHandler::read(Backend* wb, QFile& db, WId)
}
for (size_t i = 0; i < n; ++i) {
KMD5::Digest d, d2; // judgment day
MD5Digest ba;
QMap<MD5Digest,QList<MD5Digest> >::iterator it;
QByteArray ba;
QMap<QByteArray,QList<QByteArray> >::iterator it;
quint32 fsz;
if (hds.atEnd()) return -43;
hds.readRawData(reinterpret_cast<char *>(d), 16);
hds.readRawData(ba.data(), 16);
hds >> fsz;
ba = MD5Digest(reinterpret_cast<char *>(d));
it = wb->_hashes.insert(ba, QList<MD5Digest>());
it = wb->_hashes.insert(ba, QList<QByteArray>());
for (size_t j = 0; j < fsz; ++j) {
hds.readRawData(reinterpret_cast<char *>(d2), 16);
ba = MD5Digest(reinterpret_cast<char *>(d2));
(*it).append(ba);
QByteArray d2;
hds.readRawData(d2.data(), 16);
(*it).append(d2);
}
}
@ -500,7 +499,7 @@ int GpgPersistHandler::write(Backend* wb, KSaveFile& sf, QByteArray& version, WI
QByteArray hashes;
QDataStream hashStream(&hashes, QIODevice::WriteOnly);
KMD5 md5;
QCryptographicHash *md5 = new QCryptographicHash(QCryptographicHash::Md5);
hashStream << static_cast<quint32>(wb->_entries.count());
QByteArray values;
@ -511,9 +510,9 @@ int GpgPersistHandler::write(Backend* wb, KSaveFile& sf, QByteArray& version, WI
valueStream << i.key();
valueStream << static_cast<quint32>(i.value().count());
md5.reset();
md5.update(i.key().toUtf8());
hashStream.writeRawData(reinterpret_cast<const char*>(&(md5.rawDigest()[0])), 16);
md5->reset();
md5->addData(i.key().toUtf8());
hashStream.writeRawData(md5->result().toHex(), 16);
hashStream << static_cast<quint32>(i.value().count());
Backend::EntryMap::ConstIterator j = i.value().constBegin();
@ -523,9 +522,9 @@ int GpgPersistHandler::write(Backend* wb, KSaveFile& sf, QByteArray& version, WI
valueStream << static_cast<qint32>(j.value()->type());
valueStream << j.value()->value();
md5.reset();
md5.update(j.key().toUtf8());
hashStream.writeRawData(reinterpret_cast<const char*>(&(md5.rawDigest()[0])), 16);
md5->reset();
md5->addData(j.key().toUtf8());
hashStream.writeRawData(md5->result().toHex(), 16);
}
}
@ -659,19 +658,17 @@ int GpgPersistHandler::read(Backend* wb, QFile& sf, WId w)
quint32 folderCount = hashCount;
while (hashCount--){
KMD5::Digest d;
hashStream.readRawData(reinterpret_cast<char *>(d), 16);
QByteArray ba;
hashStream.readRawData(ba.data(), 16);
quint32 folderSize;
hashStream >> folderSize;
MD5Digest ba = MD5Digest(reinterpret_cast<char *>(d));
QMap<MD5Digest, QList<MD5Digest> >::iterator it = wb->_hashes.insert(ba, QList<MD5Digest>());
QMap<QByteArray, QList<QByteArray> >::iterator it = wb->_hashes.insert(ba, QList<QByteArray>());
while (folderSize--){
KMD5::Digest d2;
hashStream.readRawData(reinterpret_cast<char *>(d2), 16);
ba = MD5Digest(reinterpret_cast<char *>(d2));
(*it).append(ba);
QByteArray d2;
hashStream.readRawData(d2.data(), 16);
(*it).append(d2);
}
}

View file

@ -25,7 +25,6 @@
#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kcodecs.h>
#include <ksavefile.h>
#include <kstandarddirs.h>
#ifdef HAVE_QGPGME
@ -37,6 +36,7 @@
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QRegExp>
#include <QtCore/QCryptographicHash>
#include "blowfish.h"
#include "sha1.h"
@ -510,9 +510,8 @@ bool Backend::createFolder(const QString& f) {
_entries.insert(f, EntryMap());
KMD5 folderMd5;
folderMd5.update(f.toUtf8());
_hashes.insert(MD5Digest(folderMd5.rawDigest()), QList<MD5Digest>());
QByteArray folderMd5 = QCryptographicHash::hash(f.toUtf8(), QCryptographicHash::Md5);
_hashes.insert(folderMd5.toHex(), QList<QByteArray>());
return true;
}
@ -528,16 +527,14 @@ int Backend::renameEntry(const QString& oldName, const QString& newName) {
emap.erase(oi);
emap[newName] = e;
KMD5 folderMd5;
folderMd5.update(_folder.toUtf8());
QByteArray folderMd5 = QCryptographicHash::hash(_folder.toUtf8(), QCryptographicHash::Md5);
HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest()));
HashMap::iterator i = _hashes.find(folderMd5.toHex());
if (i != _hashes.end()) {
KMD5 oldMd5, newMd5;
oldMd5.update(oldName.toUtf8());
newMd5.update(newName.toUtf8());
i.value().removeAll(MD5Digest(oldMd5.rawDigest()));
i.value().append(MD5Digest(newMd5.rawDigest()));
QByteArray oldMd5 = QCryptographicHash::hash(oldName.toUtf8(), QCryptographicHash::Md5);
QByteArray newMd5 = QCryptographicHash::hash(newName.toUtf8(), QCryptographicHash::Md5);
i.value().removeAll(oldMd5.toHex());
i.value().append(newMd5.toHex());
}
return 0;
}
@ -555,14 +552,12 @@ void Backend::writeEntry(Entry *e) {
}
_entries[_folder][e->key()]->copy(e);
KMD5 folderMd5;
folderMd5.update(_folder.toUtf8());
QByteArray folderMd5 = QCryptographicHash::hash(_folder.toUtf8(), QCryptographicHash::Md5);
HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest()));
HashMap::iterator i = _hashes.find(folderMd5.toHex());
if (i != _hashes.end()) {
KMD5 md5;
md5.update(e->key().toUtf8());
i.value().append(MD5Digest(md5.rawDigest()));
QByteArray md5 = QCryptographicHash::hash(e->key().toUtf8(), QCryptographicHash::Md5);
i.value().append(md5.toHex());
}
}
@ -583,14 +578,12 @@ bool Backend::removeEntry(const QString& key) {
if (fi != _entries.end() && ei != fi.value().end()) {
delete ei.value();
fi.value().erase(ei);
KMD5 folderMd5;
folderMd5.update(_folder.toUtf8());
QByteArray folderMd5 = QCryptographicHash::hash(_folder.toUtf8(), QCryptographicHash::Md5);
HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest()));
HashMap::iterator i = _hashes.find(folderMd5.toHex());
if (i != _hashes.end()) {
KMD5 md5;
md5.update(key.toUtf8());
i.value().removeAll(MD5Digest(md5.rawDigest()));
QByteArray md5 = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Md5);
i.value().removeAll(md5.toHex());
}
return true;
}
@ -617,9 +610,8 @@ bool Backend::removeFolder(const QString& f) {
_entries.erase(fi);
KMD5 folderMd5;
folderMd5.update(f.toUtf8());
_hashes.remove(MD5Digest(folderMd5.rawDigest()));
QByteArray folderMd5 = QCryptographicHash::hash(f.toUtf8(), QCryptographicHash::Md5);
_hashes.remove(folderMd5.toHex());
return true;
}
@ -628,20 +620,19 @@ bool Backend::removeFolder(const QString& f) {
bool Backend::folderDoesNotExist(const QString& folder) const {
KMD5 md5;
md5.update(folder.toUtf8());
return !_hashes.contains(MD5Digest(md5.rawDigest()));
QByteArray md5 = QCryptographicHash::hash(folder.toUtf8(), QCryptographicHash::Md5);
return !_hashes.contains(md5.toHex());
}
bool Backend::entryDoesNotExist(const QString& folder, const QString& entry) const {
KMD5 md5;
md5.update(folder.toUtf8());
HashMap::const_iterator i = _hashes.find(MD5Digest(md5.rawDigest()));
QCryptographicHash *md5 = new QCryptographicHash(QCryptographicHash::Md5);
md5->addData(folder.toUtf8());
HashMap::const_iterator i = _hashes.find(md5->result().toHex());
if (i != _hashes.end()) {
md5.reset();
md5.update(entry.toUtf8());
return !i.value().contains(MD5Digest(md5.rawDigest()));
md5->reset();
md5->addData(entry.toUtf8());
return !i.value().contains(md5->result().toHex());
}
return true;
}

View file

@ -22,8 +22,6 @@
#ifndef KWALLETBACKEND_H
#define KWALLETBACKEND_H
#include <kcodecs.h>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QMap>
@ -40,34 +38,6 @@
namespace KWallet {
/**
* @internal
*/
class MD5Digest : public QByteArray {
public:
MD5Digest() : QByteArray(16, 0) {}
MD5Digest(const char *data) : QByteArray(data, 16) {}
MD5Digest(const KMD5::Digest d) : QByteArray(reinterpret_cast<const char *>(d), 16) {}
virtual ~MD5Digest() {}
int operator<(const MD5Digest& r) const {
int i = 0;
char x, y;
for (; i < 16; ++i) {
x = at(i);
y = r.at(i);
if (x != y) {
break;
}
}
if (i < 16 && x < y) {
return 1;
}
return 0;
}
};
/* @internal
*/
class KDE_EXPORT Backend {
@ -179,7 +149,7 @@ class KDE_EXPORT Backend {
typedef QMap< QString, Entry* > EntryMap;
typedef QMap< QString, EntryMap > FolderMap;
FolderMap _entries;
typedef QMap<MD5Digest, QList<MD5Digest> > HashMap;
typedef QMap<QByteArray, QList<QByteArray> > HashMap;
HashMap _hashes;
QByteArray _passhash; // password hash used for saving the wallet
QByteArray _newPassHash; //Modern hash using KWALLET_HASH_PBKDF2_SHA512

View file

@ -36,7 +36,6 @@
#include <kio/netaccess.h>
#include <kactioncollection.h>
#include <klocale.h>
#include <kcodecs.h>
#include <kmessagebox.h>
#include <kmenu.h>
#include <ksqueezedtextlabel.h>
@ -1161,7 +1160,7 @@ void KWalletEditor::importXML() {
if (type == QLatin1String( "password" )) {
_w->writePassword(ename, e.text());
} else if (type == QLatin1String( "stream" )) {
_w->writeEntry(ename, KCodecs::base64Decode(e.text().toLatin1()));
_w->writeEntry(ename, QByteArray::fromBase64(e.text().toLatin1()));
} else if (type == QLatin1String( "map" )) {
QMap<QString,QString> map;
QDomNode mapNode = e.firstChild();
@ -1219,7 +1218,7 @@ void KWalletEditor::exportXML() {
if (_w->readEntry(*j, ba) == 0) {
xml.writeStartElement(QLatin1String( "stream" ));
xml.writeAttribute(QLatin1String( "name" ), *j);
xml.writeCharacters(QLatin1String( KCodecs::base64Encode(ba) ));
xml.writeCharacters(QLatin1String( ba.toBase64() ));
xml.writeEndElement();
}
break;