SHA256 and data "sp0"

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

SHA256 and data "sp0"

FenderQ
Using SHA256 in OpenSSL 0.9.8 ...
Does anyone get no result with data "sp0"?
Please note: sp0 is lowercase above and without the "'s nor the ? char

I have tried these two OpenSSL versions:
openssl-0.9.8a.tar.gz
openssl-0.9.8-stable-SNAP-20060207.tar.gz

const unsigned char *hash_data(void *data, size_t size)
{
  static unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha;

  memset(hash, 0, sizeof(hash));
  memset(&sha, 0, sizeof(sha));

  if (data && size > 0)
  {
    SHA256_Init(&sha);
    SHA256_Update(&sha, data, size);
    SHA256_Final(hash, &sha);
  }

  return hash;
}

Does anyone else get this lack of data?
 
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: SHA256 and data "sp0"

Victor Duchovni
On Tue, Feb 07, 2006 at 09:34:49PM -0800, FenderQ wrote:

> Using SHA256 in OpenSSL 0.9.8 ...
> Does anyone get no result with data "sp0"?
> Please note: sp0 is lowercase above and without the "'s nor the ? char
>
> I have tried these two OpenSSL versions:
> openssl-0.9.8a.tar.gz
> openssl-0.9.8-stable-SNAP-20060207.tar.gz
>
> const unsigned char *hash_data(void *data, size_t size)
> {
>   static unsigned char hash[SHA256_DIGEST_LENGTH];
>   SHA256_CTX sha;
>
>   memset(hash, 0, sizeof(hash));
>   memset(&sha, 0, sizeof(sha));
>
>   if (data && size > 0)
>   {
>     SHA256_Init(&sha);
>     SHA256_Update(&sha, data, size);
>     SHA256_Final(hash, &sha);
>   }
>
>   return hash;
> }
>
> Does anyone else get this lack of data?
>  

The hash byte array is not C string. It can and will contain
ASCII NUL ('\0'), possibly in the first byte. For example
with sha1 this is the case for the input string "sp59" (as
a 4 byte quantity).

    $ echo -n sp59 | openssl sha1 -binary | od -t x1
    0000000 00 0c a0 f2 82 b9 fc 06 63 e8 54 38 10 05 e5 9a
    0000020 54 92 9d f0
    0000024

Your hash is the raw binary string. If you want hex or base64
you need to encode the binary data.

--
        Viktor.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: SHA256 and data "sp0"

FenderQ
Thanks very much Victor ...

I see exactly what you mean.  The first character in that hash result being a '\0' char is masking the rest of the data when it's looked at like a C char array.  Looking at my memory directly I now see the other bytes of data.  So it always did contain the hash value.  I just couldn't see it by viewing it as the char array.

Thanks again for the help ;-)

On Wed, Feb 08, 2006 at 01:05:09AM -0500, Victor Duchovni wrote:

> On Tue, Feb 07, 2006 at 09:34:49PM -0800, FenderQ wrote:
>
> > Using SHA256 in OpenSSL 0.9.8 ...
> > Does anyone get no result with data "sp0"?
> > Please note: sp0 is lowercase above and without the "'s nor the ? char
> >
> > I have tried these two OpenSSL versions:
> > openssl-0.9.8a.tar.gz
> > openssl-0.9.8-stable-SNAP-20060207.tar.gz
> >
> > const unsigned char *hash_data(void *data, size_t size)
> > {
> >   static unsigned char hash[SHA256_DIGEST_LENGTH];
> >   SHA256_CTX sha;
> >
> >   memset(hash, 0, sizeof(hash));
> >   memset(&sha, 0, sizeof(sha));
> >
> >   if (data && size > 0)
> >   {
> >     SHA256_Init(&sha);
> >     SHA256_Update(&sha, data, size);
> >     SHA256_Final(hash, &sha);
> >   }
> >
> >   return hash;
> > }
> >
> > Does anyone else get this lack of data?
> >  
>
> The hash byte array is not C string. It can and will contain
> ASCII NUL ('\0'), possibly in the first byte. For example
> with sha1 this is the case for the input string "sp59" (as
> a 4 byte quantity).
>
>     $ echo -n sp59 | openssl sha1 -binary | od -t x1
>     0000000 00 0c a0 f2 82 b9 fc 06 63 e8 54 38 10 05 e5 9a
>     0000020 54 92 9d f0
>     0000024
>
> Your hash is the raw binary string. If you want hex or base64
> you need to encode the binary data.
>
> --
> Viktor.
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]