Skip to content

Commit

Permalink
Fixes (#5) (#6)
Browse files Browse the repository at this point in the history
* fixed the change password and added function to check password
  • Loading branch information
EddyTheCo authored Feb 5, 2024
1 parent f73c420 commit cb242f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/vault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class QVAULT_EXPORT Vault: public QObject
);
Q_INVOKABLE QString getDataString(QString password)const;
QByteArray getData(QByteArray password) const;
void setFile(QString file){if(file!=m_file){m_file=file;restart();emit fileChanged();}}
Q_INVOKABLE bool setDataString(QString plainText,QString password);
Q_INVOKABLE bool changePassword(QString oldPass,QString newPass);
Q_INVOKABLE bool checkPassword(QString password)const;
bool setData(QByteArray plainText,QByteArray password);
bool isEmpty()const{return m_isEmpty;}

Expand Down
27 changes: 25 additions & 2 deletions src/vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Vault::Vault(QObject *parent,const QString filename):QObject(parent),m_ctx(EVP_C
m_isEmpty(true)
{
readFromFile();
connect(this,&Vault::fileChanged,this,&Vault::restart);
connect(this,&QObject::destroyed,this,[=](){EVP_CIPHER_CTX_free(m_ctx);});
}
void Vault::restart()
Expand All @@ -43,8 +42,26 @@ void Vault::restart()
setIsEmpty(true);
readFromFile();
}
bool Vault::checkPassword(QString password)const
{
if(m_isEmpty||password.size()<8) return false;

auto passHash=QPasswordDigestor::deriveKeyPbkdf2(
QCryptographicHash::Sha512,
password.toUtf8(),
m_iv,
NITERATIONS,
64);
if(passHash==m_passHash)
{
return true;
}
return false;

}
bool Vault::changePassword(QString oldPass,QString newPass)
{

if(m_isEmpty||newPass.size()<8||oldPass.size()<8) return false;

auto oldPassHash=QPasswordDigestor::deriveKeyPbkdf2(
Expand All @@ -62,6 +79,7 @@ bool Vault::changePassword(QString oldPass,QString newPass)
m_iv,
NITERATIONS,
32);

const auto plainText=getContent(oldkey);
setRandomIV();
auto newPassHash=QPasswordDigestor::deriveKeyPbkdf2(
Expand All @@ -70,14 +88,17 @@ bool Vault::changePassword(QString oldPass,QString newPass)
m_iv,
NITERATIONS,
64);

auto newkey=QPasswordDigestor::deriveKeyPbkdf2(
QCryptographicHash::Sha256,
newPass.toUtf8(),
m_iv,
NITERATIONS,
32);
setContent(plainText,newkey);

m_passHash=newPassHash;
setContent(plainText,newkey);

return true;
}
return false;
Expand Down Expand Up @@ -151,6 +172,7 @@ if(file.exists()&&file.open(QIODevice::ReadOnly))
}
void Vault::setRandomIV()
{
m_iv=QByteArray();
auto buffer=QDataStream(&m_iv,QIODevice::WriteOnly | QIODevice::Append);
for(auto i=0;i<4;i++)
{
Expand Down Expand Up @@ -239,6 +261,7 @@ bool Vault::setData(QByteArray plainText,QByteArray password)
m_iv,
NITERATIONS,
32);

return setContent(plainText,key);
}
return false;
Expand Down

0 comments on commit cb242f9

Please sign in to comment.