mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
more
This commit is contained in:
parent
bee852a28c
commit
9df2b49d07
41
core.cpp
41
core.cpp
@ -1415,15 +1415,46 @@ QList<CString> Core::splitMessage(const QString &message)
|
|||||||
|
|
||||||
void Core::setPassword(QString& password)
|
void Core::setPassword(QString& password)
|
||||||
{
|
{
|
||||||
|
if (password.isEmpty())
|
||||||
|
{
|
||||||
|
clearPassword();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!pwhash)
|
if (!pwhash)
|
||||||
pwhash = new uint8_t[TOX_HASH_LENGTH];
|
pwhash = new uint8_t[TOX_HASH_LENGTH];
|
||||||
|
|
||||||
CString str(password);
|
CString str(password);
|
||||||
tox_hash(pwhash, str.data(), str.size());
|
tox_hash(pwhash, str.data(), str.size());
|
||||||
password.clear();
|
password.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Core::clearPassword()
|
||||||
|
{
|
||||||
|
if (pwhash)
|
||||||
|
{
|
||||||
|
delete[] pwhash;
|
||||||
|
pwhash = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray Core::encryptData(const QByteArray& data)
|
||||||
|
{
|
||||||
|
if (!pwhash)
|
||||||
|
return;
|
||||||
|
uint8_t encrypted[data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
|
||||||
|
if (tox_pass_encrypt(data.data(), data.size(), pwhash, TOX_HASH_LENGTH, encrypted) == -1)
|
||||||
|
{
|
||||||
|
qWarning() << "Core::encryptData: encryption failed";
|
||||||
|
return QByteArray();
|
||||||
|
}
|
||||||
|
return QByteArray(reinterpret_cast<char*>encrypted, data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]);
|
||||||
|
}
|
||||||
|
|
||||||
bool Core::encryptFile(const QString& path)
|
bool Core::encryptFile(const QString& path)
|
||||||
{
|
{
|
||||||
|
if (!pwhash)
|
||||||
|
return false;
|
||||||
QFile file(path);
|
QFile file(path);
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
qWarning() << "Core::encryptFile: file" << path << "DNE";
|
qWarning() << "Core::encryptFile: file" << path << "DNE";
|
||||||
@ -1436,19 +1467,15 @@ bool Core::encryptFile(const QString& path)
|
|||||||
QByteArray data = file.readAll();
|
QByteArray data = file.readAll();
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
uint8_t[] out = new uint8_t[data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
|
data = encryptData(data);
|
||||||
if (tox_pass_encrypt(data.data(), data.size(), pwhash, TOX_HASH_LENGTH, out)
|
if (data.isEmpty())
|
||||||
== -1)
|
|
||||||
{
|
|
||||||
qWarning() << "Core::encryptFile:" << "encryption of" << path << "failed";
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
if (!file.open(QIODevice::WriteOnly)) {
|
if (!file.open(QIODevice::WriteOnly)) {
|
||||||
qCritical() << "Core::encryptFile:" << path << " cannot be opened for writing";
|
qCritical() << "Core::encryptFile:" << path << " cannot be opened for writing";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
file.write(reinterpret_cast<char*>out, data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
|
file.write(data.data(), data.size());
|
||||||
file.close();
|
file.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
1
core.h
1
core.h
@ -96,6 +96,7 @@ public slots:
|
|||||||
|
|
||||||
void setPassword(QString& password);
|
void setPassword(QString& password);
|
||||||
bool encryptFile(const QString& path);
|
bool encryptFile(const QString& path);
|
||||||
|
QByteArray encryptData(const QByteArray& data);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void connected();
|
void connected();
|
||||||
|
@ -219,7 +219,7 @@ QString Widget::getUsername()
|
|||||||
void Widget::onAvatarClicked()
|
void Widget::onAvatarClicked()
|
||||||
{
|
{
|
||||||
QString filename = QFileDialog::getOpenFileName(this, tr("Choose a profile picture"), QDir::homePath());
|
QString filename = QFileDialog::getOpenFileName(this, tr("Choose a profile picture"), QDir::homePath());
|
||||||
if (filename == "")
|
if (filename.isEmpty())
|
||||||
return;
|
return;
|
||||||
QFile file(filename);
|
QFile file(filename);
|
||||||
file.open(QIODevice::ReadOnly);
|
file.open(QIODevice::ReadOnly);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user