CryptoByLeo/Src/MGF1/Main.mqh
Nique_372 18e20ce3b1
2026-08-20 17:10:56 -05:00

97 lines
3.3 KiB
MQL5

//+------------------------------------------------------------------+
//| Main.mqh |
//| Copyright 2026, Niquel Mendoza |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza"
#property link "https://www.mql5.com"
#property strict
#ifndef CRYPTOBYLEO_SRC_MGF1_MAIN_MQH
#define CRYPTOBYLEO_SRC_MGF1_MAIN_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "Def.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// seed: semilla
// r: indic de lectura
// seeds: tamaño de la semilla
// outl: tamaño del out en bytes
// out: array de salida
// w: indice de escritura final para out
// f: funcion de hashing
//---
void MGF1(const uchar& seed[], int r, int seeds, uchar& outm[], int w, int outl,
CCryptoHashMeta::ENUM_TSN_CRYPTO_HASH f)
{
//----
const int hlen = TSN::CCryptoHashMeta::g_crytpo_hash_meta[f].outl; // tamaño de salida
const CCryptoHashMeta::TsnCryptoByLeoHashFuncFixed fhash = TSN::CCryptoHashMeta::g_crypto_fhash_fixed[f];
//--- buffer base
uchar buf[];
const int bufs = ArrayResize(buf, seeds + 4);
const int rl = r;
const int fin = seeds + r;
//--- empiza a leer desde r en seed y copia desde r-rl (i)
for(; r < fin; r++)
buf[r - rl] = seed[r]; // copiamos
//--- buffer del reuslto del hasher
// uchar buf_hash_ou[CRYPTOBYLEO_HASHFUNC_FIXED_MAX_OL]; // tamaño=maximo
//ArrayResize(buf_hash_ou, hlen);
//--- numero de bloques a procesar
const uint nblocks = uint((outl + (hlen - 1)) / hlen); // ceil..
//--- necesitmoas seguridad para hlen (evitar escrituras fuera de rango)
const int sob = outl & (CRYPTOBYLEO_HASHFUNC_FIXED_MAX_OL - 1); // % 64
if(sob > 0)
{
// En caso haya sobrante para llegar a ser multiplo del max
// w=donde se empzia a escribir
// outl=numero de datos
// sob=sobrante que neceismtoas para qeu sea multiplo
const int sum = w + outl + sob;
if(sum > ArraySize(outm))
{
// Agrandamos el buffer a lo uqe falta para ser multiplo del hash maximo
ArrayResize(outm, sum);
}
}
//--- aumentamos
outl += w;
//--
for(uint32_t counter = 0; counter < nblocks; counter++)
{
// añadimos el contador
buf[seeds] = uchar(counter >> 24);
buf[seeds + 1] = uchar(counter >> 16);
buf[seeds + 2] = uchar(counter >> 8);
buf[seeds + 3] = uchar(counter);
// aplicamos hasher
fhash(buf, bufs, outm, w);
// la salida se escribe en outm apartir de w
// aumentamos w en hlen bytes que escribimos
w += hlen;
}
}
}
//+------------------------------------------------------------------+
#endif // CRYPTOBYLEO_SRC_MGF1_MAIN_MQH