forked from nique_372/MQLArticles
179 lines
5.7 KiB
MQL5
179 lines
5.7 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| FolderEncrypt.mqh |
|
|
//| Copyright 2026, Niquel Mendoza. |
|
|
//| https://www.mql5.com/es/users/nique_372 |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2026, Niquel Mendoza."
|
|
#property link "https://www.mql5.com/es/users/nique_372"
|
|
#property strict
|
|
|
|
#ifndef MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERENCRYPT_MQH
|
|
#define MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERENCRYPT_MQH
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
#include "Base.mqh"
|
|
#include <TSN\\Crypto\\Fernet.mqh>
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
namespace TSN
|
|
{
|
|
class CFolderOpsEncrypt : public CFolderOpsBase
|
|
{
|
|
private:
|
|
CFernet* m_fernet;
|
|
bool m_clean_all;
|
|
|
|
|
|
public:
|
|
CFolderOpsEncrypt(void) : m_fernet(NULL), m_clean_all(false)
|
|
{}
|
|
~CFolderOpsEncrypt(void) {}
|
|
|
|
//---
|
|
void Fernet(CFernet* encryptor) { m_fernet = encryptor; }
|
|
CFernet* Fernet() { return m_fernet; }
|
|
|
|
//---
|
|
void SetInitialPass() { m_clean_all = false; }
|
|
|
|
// Decrypt
|
|
bool DecryptFolder(const string& init_dir, bool common_flag, bool delete_prev_file, int ttl, string ext_decrypt = NULL);
|
|
|
|
// Encrypt inline por bugs del compilador
|
|
template <typename TCryptoRandomFunc>
|
|
bool EncryptFolder(const string& init_dir, bool common_flag, bool delete_prev_file, datetime time, string ext_encrypt = NULL)
|
|
{
|
|
//---
|
|
string filename = "";
|
|
::ResetLastError();
|
|
//Print(init_dir);
|
|
long handle = FileFindFirst(init_dir + "*", filename, (common_flag ? FILE_COMMON : 0));
|
|
if(handle == INVALID_HANDLE)
|
|
{
|
|
LogWarning(StringFormat("Error al comenzar la busqueda, ultimo error = %d, puede ser nomal si no hay archivos", ::GetLastError()), FUNCION_ACTUAL);
|
|
return true;
|
|
}
|
|
|
|
//---
|
|
do
|
|
{
|
|
const string full_filename = init_dir + filename;
|
|
|
|
//---
|
|
if(filename[StringLen(filename) - 1] == '\\')
|
|
{
|
|
// a false dado qeu volveremos a llamar
|
|
//--- Buscamos si este folder esta en los arc hivos incluidos si lo esta entonces limpiamos todo sin exepcion
|
|
if(IsFileIncluyed(filename))
|
|
m_clean_all = true;
|
|
|
|
//---
|
|
if(!EncryptFolder<TCryptoRandomFunc>(full_filename, common_flag, delete_prev_file, time, ext_encrypt))
|
|
{
|
|
LogError(StringFormat("Error al encryptar el folder = %s", full_filename), FUNCION_ACTUAL);
|
|
FileFindClose(handle);
|
|
return false;
|
|
}
|
|
m_clean_all = false;
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
if(!m_clean_all)
|
|
{
|
|
if(IsFileExcluyed(filename))
|
|
continue;
|
|
if(!IsFileIncluyed(filename))
|
|
continue;
|
|
}
|
|
|
|
//---
|
|
//Print(filename);
|
|
if(!m_fernet.EncryptFile<TCryptoRandomFunc>(full_filename, common_flag, delete_prev_file, time, ext_encrypt))
|
|
{
|
|
LogWarning(StringFormat("Fallo al encryptar el archivo = '%s'", filename), FUNCION_ACTUAL);
|
|
}
|
|
}
|
|
}
|
|
while(FileFindNext(handle, filename));
|
|
|
|
//---
|
|
FileFindClose(handle);
|
|
|
|
//--- Si se limpio
|
|
return true;
|
|
}
|
|
};
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
bool CFolderOpsEncrypt::DecryptFolder(const string& init_dir, bool common_flag, bool delete_prev_file, int ttl, string ext_decrypt = NULL)
|
|
{
|
|
|
|
//---
|
|
string filename = "";
|
|
::ResetLastError();
|
|
long handle = FileFindFirst(init_dir + "*", filename, (common_flag ? FILE_COMMON : 0));
|
|
if(handle == INVALID_HANDLE)
|
|
{
|
|
LogWarning(StringFormat("Error al comenzar la busqueda, ultimo error = %d, puede ser nomal si no hay archivos", ::GetLastError()), FUNCION_ACTUAL);
|
|
return true;
|
|
}
|
|
|
|
//---
|
|
do
|
|
{
|
|
filename = init_dir + filename;
|
|
|
|
//---
|
|
if(filename[StringLen(filename) - 1] == '\\')
|
|
{
|
|
// a false dado qeu volveremos a llamar
|
|
//--- Buscamos si este folder esta en los arc hivos incluidos si lo esta entonces limpiamos todo sin exepcion
|
|
if(IsFileIncluyed(filename))
|
|
m_clean_all = true;
|
|
else
|
|
m_clean_all = false;
|
|
|
|
//---
|
|
if(!DecryptFolder(filename, common_flag, delete_prev_file, ttl, ext_decrypt))
|
|
{
|
|
LogError(StringFormat("Error al limpiar el folder = %s", filename), FUNCION_ACTUAL);
|
|
FileFindClose(handle);
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//---
|
|
if(!m_clean_all)
|
|
{
|
|
if(IsFileExcluyed(filename))
|
|
continue;
|
|
if(!IsFileIncluyed(filename))
|
|
continue;
|
|
}
|
|
|
|
//---
|
|
if(!m_fernet.DecryptFile(filename, common_flag, delete_prev_file, ttl, ext_decrypt))
|
|
{
|
|
LogWarning(StringFormat("Fallo al eliminar el desencriptar = '%s'", filename), FUNCION_ACTUAL);
|
|
}
|
|
}
|
|
}
|
|
while(FileFindNext(handle, filename));
|
|
|
|
//---
|
|
FileFindClose(handle);
|
|
|
|
//--- Si se limpio
|
|
return true;
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
#endif // MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERENCRYPT_MQH
|