Class: CoBreak::OpenSSL::SHA3_512

Inherits:
Object
  • Object
show all
Defined in:
ext/cobreak/cobreak_openssl.c

Class Method Summary collapse

Class Method Details

.hexdigest(input) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'ext/cobreak/cobreak_openssl.c', line 348

VALUE sha3_512_hexdigest(VALUE self, VALUE input) {

    // Asegúrate de que el input es una cadena

    Check_Type(input, T_STRING);


    // Crear un contexto para el hash

    EVP_MD_CTX *ctx = EVP_MD_CTX_new();

    const EVP_MD *md = EVP_sha3_512();

    

    // Inicializar el contexto y calcular el hash

    EVP_DigestInit_ex(ctx, md, NULL);

    EVP_DigestUpdate(ctx, (unsigned char *)StringValueCStr(input), RSTRING_LEN(input));

    

    unsigned char output[EVP_MAX_MD_SIZE];

    unsigned int output_len;


    EVP_DigestFinal_ex(ctx, output, &output_len);

    EVP_MD_CTX_free(ctx);


    // Convertir el hash a una cadena hexadecimal

    VALUE hex_string = rb_str_new("", 0);

    for (unsigned int i = 0; i < output_len; i++) {

        rb_str_catf(hex_string, "%02x", output[i]);

    }


    return hex_string;

}