forked from nique_372/MQLArticles
83 lines
2.7 KiB
MQL5
83 lines
2.7 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| TestEnc.mq5 |
| |
//| 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 version "1.00"
| |
#property strict
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "..\\FolderEncrypt.mqh"
| |
// incluimos PRNG random
| |
#include <TSN\\Crypto\\URandomPRNG.mqh>
| |
// Xoshiro256 para random
| |
#include <TSN\\Random\\Xoshiro256.mqh>
| |
| |
//+------------------------------------------------------------------+
| |
//| Script program start function |
| |
//+------------------------------------------------------------------+
| |
void OnStart()
| |
{
| |
//---
| |
const string folder_path = "MQLArticles\\File\\FolderOps\\Enc\\";
| |
::FolderDelete(folder_path, FILE_COMMON); // Elimnamos si existe
| |
| |
//---
| |
struct File
| |
{
| |
string path;
| |
string content;
| |
};
| |
const File data[] =
| |
{
| |
{folder_path + "data_importante.txt", "contraseña: 123"},
| |
{folder_path + "data_importante.csv", "contraseña: kjsdbfjksd"},
| |
{folder_path + "data_importante.ini", "key=val"},
| |
};
| |
| |
//---
| |
const int t = ArraySize(data);
| |
for(int i = 0; i < t; i++)
| |
{
| |
::ResetLastError();
| |
const int fh = FileOpen(data[i].path, FILE_TXT | FILE_WRITE | FILE_COMMON);
| |
if(fh == INVALID_HANDLE)
| |
{
| |
PrintFormat("Fallo al abrir el archivo = %s, ultimo error = %d", data[i].path, ::GetLastError());
| |
continue;
| |
}
| |
FileWrite(fh, data[i].content);
| |
FileClose(fh);
| |
}
| |
| |
//--- Random para el IV
| |
TSN::CURandomPRNG<Xoshiro256>::s_rand.Seed(1000);
| |
// key 0 para reproducibilidad
| |
uchar key[CRYPTOBYLEO_FERNET_KEYBYTES];
| |
ArrayInitialize(key, 0);
| |
| |
//---
| |
TSN::CFernet fernet;
| |
fernet.Key(key);
| |
| |
//---
| |
TSN::CFolderOpsEncrypt folder_enc;
| |
folder_enc.AddLogFlags(LOG_ALL);
| |
| |
// Seteamos el encriptoadr y su random
| |
folder_enc.Fernet(&fernet);
| |
| |
// Steamos los inc
| |
folder_enc.SizeArrInclued(2);
| |
folder_enc.SetValIncluyed(0, "*.txt");
| |
folder_enc.SetValIncluyed(1, "*.csv");
| |
// Encryptamos
| |
folder_enc.SetInitialPass();
| |
// usamos el random URandomPRNG con bakcend de xho para el IV
| |
Print(folder_enc.EncryptFolder<TSN::CURandomPRNG<Xoshiro256>>(folder_path, true, true, TimeLocal(), ".enc"));
| |
}
| |
//+------------------------------------------------------------------+
|