Output multiple hashes

Werner Koch wk at gnupg.org
Tue Apr 9 08:57:22 CEST 2024


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: openpgp-digital-signature.asc
Type: application/pgp-signature
Size: 247 bytes
Desc: not available
URL: <https://lists.gnupg.org/pipermail/gnupg-devel/attachments/20240409/fdcbedfd/attachment.sig>


More information about the Gnupg-devel mailing list