M
M
madPO2014-09-10 12:41:51
OpenSSL
madPO, 2014-09-10 12:41:51

How to get a string of 20 characters in SHA1 (c++ and openssl)?

Good afternoon.
PHP has a SHA1 function , its second argument allows you to "shorten" the final hash.

echo SHA1('Hello World', true).'</br>';
echo SHA1('Hello World');

The first line will have a hash of 20 characters, the second line will have a hash of 40.
How can I achieve the same in c++ using openssl? How to convert 40 hex characters to 20 binary bytes?
Now there is this code:
...
        unsigned char obuf[20];
        SHA1((const unsigned char *)st, strlen(st), obuf);
  char mdString[SHA_DIGEST_LENGTH*2+1];
  for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
           sprintf(&mdString[i*2], "%02x", (unsigned int)obuf[i]);
...

Where st is the string to be encoded.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2014-09-10
@OLS

In my opinion, the parameter true says that 20 binary bytes will be returned ,
and false - that 40 hexadecimal characters will be returned .
We are not talking about any shortening here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question