SHA256 difference between Windows+Ubuntu

44 views
Skip to first unread message

Murray

unread,
May 9, 2013, 11:13:34 PM5/9/13
to cryptop...@googlegroups.com
Hi,
I'm quite new to both C++ and crypto++, and have been trying to work out this problem.
I've successfully installed crypto++ 5.6.2 to Windows 7 64-bit and Ubuntu 12.04 64-bit.

Under Ubuntu, my SHA256() function occasionally returns nothing. I can reproduce it usually by entering a few large values and then a couple small. It works 100% of the time under windows.
g++ version 4.6.3.

Any hints would be appreciated,
Thanks.

  1. I compile with:
    g++ -m32 -O3 -fpic --c main.cpp -lcryptopp
  2. g++ -m32 -shared -o test.exe main.o -lc --static -lcryptopp


    1. #include "cryptopp/sha.h"
    2. #include "cryptopp/base64.h"
    3. #include "cryptopp/hex.h"
    4. #include <iostream>
    5. #include <string>
    6.  
    7. const char * SHA256(const char * argv)
    8. {
    9.     CryptoPP::SHA256 hash;
    10.     byte digest[ CryptoPP::SHA256::DIGESTSIZE ];
    11.     std::string input = std::string(argv);
    12.  
    13.     hash.CalculateDigest( digest, (const byte *)input.c_str(), input.length() );
    14.  
    15.     CryptoPP::HexEncoder encoder;
    16.     std::string output;
    17.     encoder.Attach(new CryptoPP::StringSink( output ));
    18.     encoder.Put( digest, sizeof(digest) );
    19.     encoder.MessageEnd();
    20.     return output.c_str();
    21. }
    22. int main()
    23. {
    24.         std::string str;
    25.         while(1)
    26.         {
    27.                 str.clear();
    28.                 std::getline(std::cin, str);
    29.                 const char * result = SHA256(str.c_str());
    30.                 std::cout << "Hash: " << result << std::endl;
    31.         }
    32.         return 0;
    33. }

Christopher Head

unread,
May 10, 2013, 2:11:46 PM5/10/13
to cryptop...@googlegroups.com, Murray
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

See below.

Chris

On Thu, 9 May 2013 20:13:34 -0700 (PDT)
Murray <in...@murrawhip.net> wrote:

> 7. const char * SHA256(const char * argv)
> 8. {
> 9. CryptoPP::SHA256 hash;
> 10. byte digest[ CryptoPP::SHA256::DIGESTSIZE ];
> 11. std::string input = std::string(argv);
> 12.
> 13. hash.CalculateDigest( digest, (const byte
> *)input.c_str(), input.length() );
> 14.
> 15. CryptoPP::HexEncoder encoder;
> 16. std::string output;
> 17. encoder.Attach(new CryptoPP::StringSink( output ));
> 18. encoder.Put( digest, sizeof(digest) );
> 19. encoder.MessageEnd();
> 20. return output.c_str();

On this line, you are returning a pointer to a member of a local
variable. The variable output is a local and will therefore be
destroyed on return, which also deallocates the internal storage used
to hold the c_str. You then use the c_str after it has been freed. You
should return an std::string instead.

> 21. }
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iF4EAREIAAYFAlGNOGUACgkQnfE3lq0v9IzkPAD/a5oKJOUwVqWuWKTUEcJstJXb
I9ujNtRlML76OP2tjm8A/ivx+xaKLyeKj7Wiw9iwHgkUy8O0dEg67LhOBZ8kUxA7
=a8Fl
-----END PGP SIGNATURE-----
Reply all
Reply to author
Forward
0 new messages