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

Hash: SHA256




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 ?


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.

  • I updated my question, could you please tell me, how to make "gpg --clearsign" command to generate output with specific "Hash" value? – social_loser May 12 '17 at 7:16
  • 2
    I found it, just append "--personal-digest-preferences sha512 " to change the hash to sha512! – social_loser May 12 '17 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 May 13 '17 at 7:52

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.

  • there is no encryption, only the "gpg --clearsign" command, I updated my post to make my question more clear. – social_loser May 12 '17 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 May 12 '17 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. – social_loser May 12 '17 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 May 12 '17 at 11:58

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.