MQLArticles/Utils/File/FolderOps/FolderEncrypt.mqh

179 lines
5.7 KiB
MQL5
Raw Permalink Normal View History

2026-03-27 09:40:26 -05:00
//+------------------------------------------------------------------+
//| 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"
2026-09-02 08:46:32 -05:00
#include <TSN\\Crypto\\Fernet.mqh>
2026-03-27 09:40:26 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-06-03 18:52:51 -05:00
namespace TSN
{
2026-03-27 09:40:26 -05:00
class CFolderOpsEncrypt : public CFolderOpsBase
{
private:
2026-09-02 15:54:33 -05:00
CFernet* m_fernet;
2026-03-28 07:46:34 -05:00
bool m_clean_all;
2026-03-27 09:40:26 -05:00
public:
2026-09-02 15:54:33 -05:00
CFolderOpsEncrypt(void) : m_fernet(NULL), m_clean_all(false)
2026-03-28 07:46:34 -05:00
{}
2026-03-27 09:40:26 -05:00
~CFolderOpsEncrypt(void) {}
//---
2026-09-02 15:54:33 -05:00
void Fernet(CFernet* encryptor) { m_fernet = encryptor; }
CFernet* Fernet() { return m_fernet; }
2026-03-27 09:40:26 -05:00
//---
2026-09-02 15:54:33 -05:00
void SetInitialPass() { m_clean_all = false; }
2026-03-27 09:40:26 -05:00
2026-09-02 15:54:33 -05:00
// Decrypt
bool DecryptFolder(const string& init_dir, bool common_flag, bool delete_prev_file, int ttl, string ext_decrypt = NULL);
2026-03-27 09:40:26 -05:00
2026-09-02 15:54:33 -05:00
// 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)
2026-03-27 09:40:26 -05:00
{
//---
2026-09-02 15:54:33 -05:00
string filename = "";
::ResetLastError();
//Print(init_dir);
long handle = FileFindFirst(init_dir + "*", filename, (common_flag ? FILE_COMMON : 0));
if(handle == INVALID_HANDLE)
2026-03-27 09:40:26 -05:00
{
2026-09-02 15:54:33 -05:00
LogWarning(StringFormat("Error al comenzar la busqueda, ultimo error = %d, puede ser nomal si no hay archivos", ::GetLastError()), FUNCION_ACTUAL);
return true;
2026-03-27 09:40:26 -05:00
}
2026-09-02 15:54:33 -05:00
//---
do
2026-03-27 09:40:26 -05:00
{
2026-09-02 15:54:33 -05:00
const string full_filename = init_dir + filename;
2026-03-28 07:46:34 -05:00
//---
2026-09-02 15:54:33 -05:00
if(filename[StringLen(filename) - 1] == '\\')
2026-03-27 09:40:26 -05:00
{
2026-09-02 15:54:33 -05:00
// 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;
2026-03-28 07:46:34 -05:00
}
2026-09-02 15:54:33 -05:00
else
2026-03-28 07:46:34 -05:00
{
2026-09-02 15:54:33 -05:00
//---
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);
}
2026-03-27 09:40:26 -05:00
}
}
2026-09-02 15:54:33 -05:00
while(FileFindNext(handle, filename));
2026-03-27 09:40:26 -05:00
2026-09-02 15:54:33 -05:00
//---
FileFindClose(handle);
2026-03-28 21:46:51 -05:00
2026-09-02 15:54:33 -05:00
//--- Si se limpio
return true;
}
};
2026-03-27 09:40:26 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-09-02 15:54:33 -05:00
bool CFolderOpsEncrypt::DecryptFolder(const string& init_dir, bool common_flag, bool delete_prev_file, int ttl, string ext_decrypt = NULL)
2026-03-27 09:40:26 -05:00
{
//---
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
{
2026-09-02 15:54:33 -05:00
filename = init_dir + filename;
2026-03-27 09:40:26 -05:00
//---
if(filename[StringLen(filename) - 1] == '\\')
{
// a false dado qeu volveremos a llamar
2026-03-28 07:46:34 -05:00
//--- 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;
2026-09-02 15:54:33 -05:00
else
m_clean_all = false;
2026-03-28 07:46:34 -05:00
//---
2026-09-02 15:54:33 -05:00
if(!DecryptFolder(filename, common_flag, delete_prev_file, ttl, ext_decrypt))
2026-03-27 09:40:26 -05:00
{
2026-09-02 15:54:33 -05:00
LogError(StringFormat("Error al limpiar el folder = %s", filename), FUNCION_ACTUAL);
2026-03-28 21:46:51 -05:00
FileFindClose(handle);
2026-03-27 09:40:26 -05:00
return false;
}
}
else
{
2026-03-28 07:46:34 -05:00
//---
if(!m_clean_all)
2026-03-27 09:40:26 -05:00
{
2026-03-28 07:46:34 -05:00
if(IsFileExcluyed(filename))
continue;
if(!IsFileIncluyed(filename))
continue;
2026-03-27 09:40:26 -05:00
}
2026-03-28 07:46:34 -05:00
//---
2026-09-02 15:54:33 -05:00
if(!m_fernet.DecryptFile(filename, common_flag, delete_prev_file, ttl, ext_decrypt))
2026-03-28 07:46:34 -05:00
{
2026-09-02 15:54:33 -05:00
LogWarning(StringFormat("Fallo al eliminar el desencriptar = '%s'", filename), FUNCION_ACTUAL);
2026-03-28 07:46:34 -05:00
}
2026-03-27 09:40:26 -05:00
}
}
while(FileFindNext(handle, filename));
2026-03-28 21:46:51 -05:00
//---
FileFindClose(handle);
2026-03-27 09:40:26 -05:00
//--- Si se limpio
return true;
}
2026-06-03 18:52:51 -05:00
}
2026-03-27 09:40:26 -05:00
//+------------------------------------------------------------------+
#endif // MQLARTICLES_UTILS_FILE_FOLDEROPS_FOLDERENCRYPT_MQH