Mailing List Archive

Output multiple hashes
Hello,
I wanted libgcrypt to print hashes of a buffer in various algorithms.
for example if I have a buffer:

char* buffer = "hello\n";

How do I get multiple hashes? For example, MD5 and SHA1?

I've enabled both using `gcry_md_enable`, but I can only read using one
algorithm using `gcry_md_read` Am I supposed to make 2 reads? What if I
have many algorithms I want to use?

Any help provided would be appreciated.

Yours Faithfully,
megamind6155.
Re: Output multiple hashes [ In reply to ]
On Sun, 7 Apr 2024 18:46, megamind6155 said:

> I've enabled both using `gcry_md_enable`, but I can only read using
> one algorithm using `gcry_md_read` Am I supposed to make 2 reads? What
> if I have many algorithms I want to use?

You need to specify the algorithm with gcry_md_read. Something like this:


err = gcry_md_open (&md, 0, 0);
if (err)
{
log_error ("%s: %s\n", fname?fname:"[stdin]", strerror(errno));
return;
}

if (!gcry_md_test_algo (GCRY_MD_MD5))
gcry_md_enable (md, GCRY_MD_MD5);
gcry_md_enable (md, GCRY_MD_SHA1);
if (!gcry_md_test_algo (GCRY_MD_RMD160))
gcry_md_enable (md, GCRY_MD_RMD160);
if (!gcry_md_test_algo (GCRY_MD_SHA224))
gcry_md_enable (md, GCRY_MD_SHA224);
if (!gcry_md_test_algo (GCRY_MD_SHA256))
gcry_md_enable (md, GCRY_MD_SHA256);
if (!gcry_md_test_algo (GCRY_MD_SHA384))
gcry_md_enable (md, GCRY_MD_SHA384);
if (!gcry_md_test_algo (GCRY_MD_SHA512))
gcry_md_enable (md, GCRY_MD_SHA512);

while ((n=es_fread (buf, 1, DIM(buf), fp)))
gcry_md_write (md, buf, n);

if (es_ferror(fp))
log_error ("%s: %s\n", fname?fname:"[stdin]", strerror(errno));
else
{
gcry_md_final (md);
if (!gcry_md_test_algo (GCRY_MD_MD5))
print_hex (md, GCRY_MD_MD5, fname);
print_hex (md, GCRY_MD_SHA1, fname );
if (!gcry_md_test_algo (GCRY_MD_RMD160))
print_hex (md, GCRY_MD_RMD160, fname );
if (!gcry_md_test_algo (GCRY_MD_SHA224))
print_hex (md, GCRY_MD_SHA224, fname);
if (!gcry_md_test_algo (GCRY_MD_SHA256))
print_hex (md, GCRY_MD_SHA256, fname );
if (!gcry_md_test_algo (GCRY_MD_SHA384))
print_hex (md, GCRY_MD_SHA384, fname );
if (!gcry_md_test_algo (GCRY_MD_SHA512))
print_hex (md, GCRY_MD_SHA512, fname );
}
gcry_md_close (md);


With print_hex() doing this:

p = gcry_md_read (md, algo);
n = gcry_md_get_algo_dlen (algo);




Shalom-Salam,

Werner

--
The pioneers of a warless world are the youth that
refuse military service. - A. Einstein