Class: CoBreak::OpenSSL::SHA3_256

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

Class Method Summary collapse

Class Method Details

.hexdigest(input) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'ext/cobreak/cobreak_openssl.c', line 260

VALUE sha3_256_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_256();

    

    // 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;
}