Mailing List Archive

Problem when hashing
Hi,

i have one problem trying to hash some data. This is the code:

unsigned char hash[20];
gcry_md_hash_buffer(GCRY_MD_SHA1, hash, sessionKey, 16);
gcry_mpi_scan(&mpiHash, GCRYMPI_FMT_USG, hash, 20, NULL);
gcry_sexp_build(&data, &erroff, "(data (flags pkcs1) (hash sha1 %m))", mpiHash);
err = gcry_pk_sign(&signedKey, data, privateKey);
if (err) printf("%s", gcry_strerror(err));

The error it gives to me is Conflicting use.

I've debugging it and the cause of this is the hash in the S-Expression has a length of
21 instead of 20, here is a print of it:

(data
(flags pkcs1)
(hash sha1 #00EAE3734D9EA7394FCBEE92ADB05994B5083827FC#)
)

So, I suppose it is caused by the leading zero byte in the hash, but where does
this byte come from? How can I solve this problem?

Thanks.
Re: Problem when hashing [ In reply to ]
On Thu, 24 Feb 2005 13:41:00 +0100, Madelman said:

> So, I suppose it is caused by the leading zero byte in the hash, but where does
> this byte come from? How can I solve this problem?

That is to mark the number as positive. What you should do is to
use

unsigned char md[20];

...
rc = gcry_sexp_build (&hash, NULL,
"(data (flags pkcs1) (hash sha1 %b))",
(size_t)20, md);

That's far easier, isn't it? Make sure that the first argument of %b
has the type size_t, the second one is a pointer of course.


Shalom-Salam,

Werner