×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: SHA 256 problem

  1. #1
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    SHA 256 problem

    hi im trying to find a hash or message digest of a string using the following link:

    http://www.newlc.com/en/SHA-256-hashing-algorithm.html



    but problem is that whenever i try to write the sha256sum (see the link code)to a file, it always writes a encryted text rather then writing a hash value.
    so anybody having idea about SHA 256 hashing

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,737

    Re: SHA 256 problem

    That "encrypted text" is most likely the hash value, just in binary format. If you expect a string of hexadecimal digits, it is just a matter of formatting. Use the format string "%02x" for getting the 2-digit hexadecimal representation for the individual bytes.

  3. #3
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    Re: SHA 256 problem

    hi im using the folliwng code to write each byte in hex format:

    _LIT8(KHex123,"%02X");
    for(TInt i=0;i<len123;i++)
    {
    converter.AppendFormat(KHex123,(buffer)[i]);
    }

    buffer is TBuf<256>buffer,inside which the SHA 256 byte data is written in encrypted form, len123 is the length of buffer

    Now when i tried to convert that into the hex format as stated above , it results in a buffer of 512 bytes of hex string, rather then getting 256 bits

    i used the following code too:
    TUint8 number(0);
    HBufC8* buf = HBufC8::NewLC(len123/2);
    TPtr8 hexptr=buf->Des();
    for(TInt i=0; i <len123;i = i+2)
    {
    TLex8 lex(buffer.Mid(i,2));
    lex.Val(number,EHex);
    hexptr.Append(number);
    }

    on writing the hexptr to a file, a 128 byte file is made which if i open then nothing is visible.I copied the same to pc & opened it then it showed as number of square blocks characters
    Last edited by vineet.jain; 2009-11-24 at 12:43.

  4. #4
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    Re: SHA 256 problem

    hi all, i solved the stated problem.But now im getting a 33 byte string when i apply SHA 256 algo.Now after that i convert that string into the hex format as a result of which i got a 66 byte string.I used the following which is getting me a 66 byte hex code:

    _LIT8(KHex123,"%X");
    for(TInt i=0;i<len123;i++)
    {

    converter.AppendFormat(KHex123,(buf6)[i]);
    }

    so converter buffer contains the hex code.Now i have the following queries:

    a)why is it giving me a 66 byte hex string after conversion as it is a SHA 256 algo,so it must return a 32 byte string after conversion too.

    b) if i remove the 02 from %02X then i get a 64 byte string.A 64 byte string is also a result in following link:
    http://www.xorbin.com/tools/sha256-hash-calculator

    but to my surprise the hash value which im getting is different from what i get from above link .For e.g. the hash value which i get of "a" is :

    BA5EC51D7A4ACE95168704431D59A2B21A4E951ACC10505A8DC47C51EE40

    while what i get from link is:
    ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb

  5. #5
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    Re: SHA 256 problem

    anybody having any idea that why a different SHA 256 value is written to buffer?

    i mean different then what this link gives"

    http://www.xorbin.com/tools/sha256-hash-calculator

    im using the following link to calculate hash:

    http://www.newlc.com/en/SHA-256-hashing-algorithm.html

    & following is the code im using to calulate hex string from resulting hash:

    TBuf8<256> checksum8;
    TBuf8<256> tempHexDigit;
    _LIT8(KHex123,"%02X");

    for(TInt i=0;i<32;i++)
    {


    tempHexDigit.Format(KHex123,(const TUint*)sha256sum[i]);
    checksum8.Append(tempHexDigit);



    }

    iFile.Write(checksum8);

    Now data written to the file is a 64 byte hex string for alphabet "a", but its different from what we get from:
    http://www.xorbin.com/tools/sha256-hash-calculator
    Last edited by vineet.jain; 2009-11-24 at 19:21.

  6. #6
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    Re: SHA 256 problem

    Any responses..?

  7. #7
    Registered User
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122

    Re: SHA 256 problem

    There are issues with the code snippets you've posted, but most likely there are even more severe problems earlier in the calculation. Please show the code where you calculate sha256sum of "a" in the first place.

    (The sample usage snippet on the newlc page you linked in the original post also has its issues. For example, it does unnecessary copying and even then calculates the hash of "buf" which has not been declared nor initialized.)

  8. #8
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    Re: SHA 256 problem

    hi thanks for the resopnse.Actually the if you see that NewLC() code i have implemented the same in my code as well, so the calulation of SHA256sum is done inside that readymade class on NewLC().By the way following is just the calling of methods of that class:

    SHA256::sha256_context ctx;
    TUint8 sha256sum[32]; // for hash storage


    TBuf8<256> tMyBuffer8(_L("a"));

    TUint8* ptrMyBuffer = new (ELeave) TUint8[tMyBuffer8.Length()];
    CleanupArrayDeletePushL(ptrMyBuffer);

    Mem::Copy(ptrMyBuffer, tMyBuffer8.Ptr(), tMyBuffer8.Length() );

    sha256_starts( &ctx );

    sha256_update( &ctx, &buf, tMyBuffer8.Length() );

    sha256_finish( &ctx, sha256sum );// this is the value im converting into a hex string

  9. #9
    Registered User
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122

    Re: SHA 256 problem

    That won't even compile: You're initializing 8-bit descriptor tMyBuffer8 with a 16-bit descriptor _L("a"). When you call sha256_update(), there's the buf variable that has not been declared nor initialized. Where does it come from?

  10. #10
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    Re: SHA 256 problem

    oh that _L8 is ok,actually i made some changes in the code.Regarding that buf variable, i have declared it within the function as TUint8 buf;

    as the argument types in update function are:
    void SHA256::sha256_update( sha256_context *ctx, TUint8 *input, TUint32 length )

  11. #11
    Registered User
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122

    Re: SHA 256 problem

    Still there's no relation between tMyBuffer8 where the "a" went into and buf which you pass to sha256_update().

  12. #12
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    Re: SHA 256 problem

    well this is what they have done in the given class inside the update function:


    void SHA256::sha256_update( sha256_context *ctx, TUint8 *input, TUint32 length )
    {
    TUint32 left, fill;

    if( ! length ) return;

    left = ctx->total[0] & 0x3F;
    fill = 64 - left;

    ctx->total[0] += length;
    ctx->total[0] &= 0xFFFFFFFF;

    if( ctx->total[0] < length )
    ctx->total[1]++;

    if( left && length >= fill )
    {
    Mem::Copy( (void *) (ctx->buffer + left),
    (void *) input, fill );
    sha256_process( ctx, ctx->buffer );
    length -= fill;
    input += fill;
    left = 0;
    }

    while( length >= 64 )
    {
    sha256_process( ctx, input );
    length -= 64;
    input += 64;
    }

    if( length )
    {
    Mem::Copy( (void *) (ctx->buffer + left),
    (void *) input, length );
    }
    }

  13. #13
    Registered User
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122

    Re: SHA 256 problem

    That's not the point. The point is what *you* pass in as arguments to that function.

  14. #14
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,078

    Re: SHA 256 problem

    so what am i supposed to pass in as the argument, i just followed the way the NewLC has given

  15. #15
    Nokia Developer Expert
    Join Date
    Jun 2006
    Posts
    28

    Re: SHA 256 problem

    I think you are not passing the correct argument as pointed by laa-laa.

    sha256_update( &ctx, &buf, tMyBuffer8.Length() ); // Here instead of buf you should pass tMyBuffer8.

    as shown below:
    sha256_update( &ctx, tMyBuffer8.Ptr(), tMyBuffer8.Length() );



    Also for what version of OS do you need SHA256 functionality? Because the latest version of OS has SHA256 implmented.

    For details check: http://developer.symbian.org/xref/os...spi/inc/hash.h

Similar Threads

  1. Problem with CAknDoubleStyleListBox with a CAknSearchField
    By jjig in forum Symbian User Interface
    Replies: 2
    Last Post: 2009-09-17, 00:58
  2. STLPort namespace issue
    By yickhong in forum Open C/C++
    Replies: 0
    Last Post: 2009-04-09, 08:54
  3. SyncML link problem
    By samuel_ in forum Symbian
    Replies: 4
    Last Post: 2009-03-27, 08:02
  4. netcards - Problem with opening adapter
    By kernj in forum Symbian Tools & SDKs
    Replies: 5
    Last Post: 2007-01-10, 08:56
  5. Problem: S60 SDK for CW in PC with .NET
    By anttij in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2005-02-28, 11:36

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×