This commit is contained in:
Elena Subbotina
2024-07-29 21:58:34 +03:00
parent 0eb8c93cf9
commit af01306997
3 changed files with 68 additions and 34 deletions

View File

@ -305,8 +305,15 @@ _buf GenerateOdfKey(_buf & pSalt, _buf & pPassword, int keySize, int spin, CRYPT
{ {
_buf pKey (keySize); _buf pKey (keySize);
_buf empty (NULL, 0, false); _buf empty (NULL, 0, false);
_buf pPassword_hash;
_buf pPassword_hash = HashAppend(pPassword, empty, algorithm); if (algorithm == CRYPT_METHOD::None)
{
pPassword_hash = pPassword; }
else
{
pPassword_hash = HashAppend(pPassword, empty, algorithm);
}
PKCS5_PBKDF2_HMAC<SHA1> pbkdf; PKCS5_PBKDF2_HMAC<SHA1> pbkdf;
@ -819,36 +826,40 @@ void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*& d
} }
} }
//----------------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------------
void odfWriteProtect::SetPassword (const std::wstring &password_) void ODFWriteProtect::SetPassword (const std::wstring &password_)
{ {
password = password_; password = password_;
} }
void odfWriteProtect::SetProtectKey (const std::string &key)
{
protect_key = key;
}
void odfWriteProtect::SetProtectAlgorithm (const CRYPT_METHOD::_hashAlgorithm &alg)
{
hash = alg;
}
void odfWriteProtect::Generate()
{
_buf pPassword (password);
_buf empty (NULL, 0, false);
_buf pHashTest = HashAppend(empty, pPassword, hash); void ODFWriteProtect::SetCryptData(_odfWriteProtectData &_data)
protect_key = std::string((char*)pHashTest.ptr, pHashTest.size);
}
bool odfWriteProtect::Verify()
{ {
_buf pPassword (password); data = _data;
_buf empty (NULL, 0, false); }
_buf pHash (protect_key); void ODFWriteProtect::GetCryptData(_odfWriteProtectData &_data)
{
_data = data;
}
_buf pHashTest = HashAppend(empty, pPassword, hash); void ODFWriteProtect::Generate()
{
return (pHashTest == pHash); std::string passw_ansi = std::string(password.begin(), password.end());
_buf pPassword (passw_ansi);
_buf pSalt (data.saltValue);
_buf pHash = GenerateOdfKey(pSalt, pPassword, 16, data.spinCount, CRYPT_METHOD::None);
data.hashValue = std::string((char*)pHash.ptr, pHash.size);
}
bool ODFWriteProtect::Verify()
{
std::string passw_ansi = std::string(password.begin(), password.end());
_buf pPassword (passw_ansi);
_buf pSalt (data.saltValue);
_buf pHash (data.hashValue);
_buf pHashTest = GenerateOdfKey(pSalt, pPassword, 16, data.spinCount, CRYPT_METHOD::None);
return (pHashTest == pHash);
} }
//---------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------
void ECMAWriteProtect::SetPassword (const std::wstring &password_) void ECMAWriteProtect::SetPassword (const std::wstring &password_)

View File

@ -40,12 +40,14 @@ namespace CRYPT_METHOD
{ {
enum _hashAlgorithm enum _hashAlgorithm
{ {
None,
MD5, MD5,
SHA1, SHA1,
SHA224, SHA224,
SHA256, SHA256,
SHA384, SHA384,
SHA512 SHA512,
PBKDF2
}; };
enum _cipherAlgorithm enum _cipherAlgorithm
@ -81,6 +83,7 @@ struct _ecmaWriteProtectData
std::string saltValue; std::string saltValue;
std::string hashValue; std::string hashValue;
}; };
typedef _ecmaWriteProtectData _odfWriteProtectData;
struct _ecmaCryptData struct _ecmaCryptData
{ {
//default ms2010 //default ms2010
@ -162,21 +165,22 @@ private:
}; };
//--------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------
class odfWriteProtect class ODFWriteProtect
{ {
public: public:
odfWriteProtect() : hash(CRYPT_METHOD::SHA1) {} ODFWriteProtect(){}
virtual ~ODFWriteProtect(){}
void SetPassword (const std::wstring & password); void SetPassword (const std::wstring &password);
void SetProtectKey (const std::string & protect_key);
void SetProtectAlgorithm (const CRYPT_METHOD::_hashAlgorithm & hash); void SetCryptData(_odfWriteProtectData &data);
void GetCryptData(_odfWriteProtectData &data);
void Generate(); void Generate();
bool Verify(); bool Verify();
private: private:
std::wstring password; std::wstring password;
CRYPT_METHOD::_hashAlgorithm hash; _odfWriteProtectData data;
std::string protect_key;
}; };
//--------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------
class ECMAEncryptor class ECMAEncryptor

View File

@ -1093,3 +1093,22 @@ bool ECMACryptFile::WriteAdditional(const std::wstring &file_name, const std::ws
return true; return true;
} }
bool TestOdfProtect(const std::wstring &passward, const std::string &salt, const std::string &hash, int count)
{
CRYPT::ODFWriteProtect test;
CRYPT::_odfWriteProtectData ver1;
ver1.hashAlgorithm = CRYPT_METHOD::PBKDF2;
ver1.hashValue = DecodeBase64(hash);
ver1.saltValue = DecodeBase64(salt);
ver1.spinCount = count;
test.SetCryptData(ver1);
test.SetPassword(passward);
bool result_ver1 = test.Verify();
return result_ver1;
}