6

When using GnuPG to clear sign a text, there is a hash part in signed message. Take the example:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

abc
-----BEGIN PGP SIGNATURE-----

iQEzBAEBCAAdFiEEZZvqE5j3koIxs9Xim0+E4a5Vo2cFAlkRdvQACgkQm0+E4a5V
o2ew2QgAzHuvZ7Hlb6+3qRLjc9Yhdi+6tBmNWXbWpKoAQxpzx6jKQp/FSpQeGWuj
RxcYnqU3pk4ycMLtaCFcfnHEW5N0B95eXGcurgMGz7A6xhy0hy25x8WBdeKVAQ+2
PLA2ytJLUn2L1S3ueqJWcdVUBRaiczOOsYvvO
...
...

-----END PGP SIGNATURE-----

But the hash algorithm is different for different keys (or servers), sometimes SHA1, sometimes SHA256, SHA512.

What determines this, the key or GnuPG client? I can't find info on this, when you use gpg create new keys , there is no option to specify the hash algorithm.

update: to clearify my question, I added more info below. the command used to generate the example output above, is :

gpg --clearsign

( The gpg version is gpg2 on my system. )

then , I typed some random text and there comes the result above. I wish to know, how to generate output with specific "Hash:" values ? say, SHA1 ?

3
  • 2
    --digest-algo Commented May 12, 2017 at 7:44
  • and "--personal-digest-preferences" works too. Commented May 12, 2017 at 11:13
  • @dave_thompson_085: This was the only solution out there that worked for me. Commented Aug 22, 2018 at 15:16

3 Answers 3

1

The hashing algorithm is chosen by the implementation of OpenPGP, in your case GnuPG. Which one gets selected

For encrypting messages, additionally the recipient's preferences stored in the public key are considered.

3
  • I updated my question, could you please tell me, how to make "gpg --clearsign" command to generate output with specific "Hash" value? Commented May 12, 2017 at 7:16
  • 2
    I found it, just append "--personal-digest-preferences sha512 " to change the hash to sha512! Commented May 12, 2017 at 11:12
  • Better provide multiple options, as the different preferences will be combined. If you only provide sha512 and the other side indicates not supporting it, you will be left the the bare minimum of SHA1, or even MD5.
    – Jens Erat
    Commented May 13, 2017 at 7:52
4

I wish to know, how to generate output with specific "Hash:" values ? say, SHA1 ?

To answer your question, use the --digest-algo SHA1 option.

As an example, here is a detached signature using SHA-256. I don't use --clearsign, so I'm not going to try to cobble it together:

gpg -a -u 1F8E37BD --digest-algo SHA256 --output test.txt.sig --detach-sig test.txt

-a produces the ASCII armour output. -u selects the signing key among different keys. --digest-algo selects the hash. --output is the output filename. The input filename must be last option.

The list of hashes and their values are available in RFC 4880, Section 9.4. SHA-1 is 2, and SHA-256 is 8.

You can audit the signature with:

$ cat test.txt.sig | gpg --list-packets | grep "digest algo"
    digest algo 8, begin of digest 05 94
1

What should be happening in that case is that you generate a hash for the message and use a key to encrypt it. The other side uses the opposite key to decrypt the hash, builds the hash of the message and compares it to the hash your attached.

It's important for the counterpart to know which kind of hash you used but there is no direct correlation between keys and the hash.

4
  • there is no encryption, only the "gpg --clearsign" command, I updated my post to make my question more clear. Commented May 12, 2017 at 7:15
  • Yes, there is encryption because that's how it work. Also see this and especially this graphic for a more general overview. Have a look at this for some more in-depth information.
    – Seth
    Commented May 12, 2017 at 7:57
  • the links you provided is very useful, I understand now. your answer explains the theory, and Jens Erat's answer explains it at another angle, it's my fault , my question is vague and my english is bad, both of you is good but I have to choose one. I decide to pick Erat answer, thank you very much. sorry for my bad english. Commented May 12, 2017 at 11:10
  • That's totally fine. It's your decision which answer actually answers you question. Additional proficiency in English isn't a requirement to either ask or answer a question. Though usually it helps. ;-) If you find an answer useful you could also consider upvoting. There is no reason to tell anyone why you chose an answer. Also check the answer for some more information on what do to if someone answers a question. Upvoting should be one of the recent privileges you earned.
    – Seth
    Commented May 12, 2017 at 11:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .