Thank you. I'm still a little off. Anyone see where I am still not
right?
with the same inputs,
php output: RB7psloFMPq2Z4jg7MUo35DtY+32atMUImfIfTWlHxM=
c++ output:
646434695767556b7a494e593875424d4f6230573547775a7a6f5467787a6252
code:
const char *secretId=msg_to_sign.c_str();
int msg_len = strlen( secretId );
CryptoPP::SHA256 sha256hash;
int dwLen = sha256hash.DIGESTSIZE;
// hmac
unsigned char hmac[ dwLen ];
memset( hmac, 0x00, dwLen );
CryptoPP::HMAC<CryptoPP::SHA256>((byte*)privateId,
pri_len).CalculateDigest(hmac, (byte*)secretId, msg_len);
// base64
CryptoPP::Base64Encoder hexEncoder;
hexEncoder.Put(hmac,dwLen);
//hexEncoder.Close(); // Shaun Wilde in 2000 uses, but it no
longer exists?
hexEncoder.Get(hmac,dwLen*2);
char *out = (char *) malloc( sizeof(char) * ((dwLen*2)+1) );
char *p = out;
for( int i = 0; i < dwLen; i++, p += 2 ) {
snprintf ( p, 3, "%02x", hmac[i] );
}
On Dec 14, 9:10 am, Da Co <[email protected]> wrote:
> Also , thanks to Andrey
> V.http://andreyvitdev.blogspot.com/2005/10/crypto-usage-sample.html
>
> On Tue, Dec 14, 2010 at 10:02 AM, Da Co <[email protected]> wrote:
> >http://www.codeproject.com/KB/security/hexencdec.aspx
>
> > On Tue, Dec 14, 2010 at 12:03 AM, Paul Crown <[email protected]>wrote:
>
> >> My next attempt is getting a little better. Now how to base64 encode?
>
> >> const char *secretId=msg_to_sign.c_str();
> >> int msg_len = strlen( secretId );
>
> >> CryptoPP::SHA256 sha256hash;
> >> unsigned char hmac[ sha256hash.DIGESTSIZE ];
> >> memset( hmac, 0x00, sha256hash.DIGESTSIZE );
> >> CryptoPP::HMAC<CryptoPP::SHA256>((byte*)privateId,
> >> pri_len).CalculateDigest(hmac, (byte*)secretId, msg_len);
>
> >> Thanks.
>
> >> Paul
>
> >> On Dec 13, 7:20 pm, Paul Crown <[email protected]> wrote:
> >> > Greetings,
>
> >> > I would like to incorporate the equivalent of the following php code
> >> > snippet in a c++ app. I have been experimenting and have learned how
> >> > to use a number of crypto++ functions. I have yet, however, to fully
> >> > wrap my mind around this snippet.
>
> >> > I think that I have just figured out that the term "SHA256" is used in
> >> > different respects, one as a hash method, and one as a message
> >> > encoding method. Which means that my c++ snippet is useless.
>
> >> > In summary, I want to calculate HMAC with SHA256 and base64-encoding
> >> > in c++, just like demonstrated in the php snippet below. Can you
> >> > please point me in the right direction?
>
> >> > Thank you.
>
> >> > Paul
>
> >> > Php code snippet:
>
> >> > // calculate HMAC with SHA256 and base64-encoding
>
> >> > $signature = base64_encode(hash_hmac("sha256", $string_to_sign,
> >> > $private_key, True));
>
> >> > c++ snippet:
>
> >> > const char *secretId=msg_to_sign.c_str();
> >> > int msg_len = strlen( secretId );
>
> >> > // sha256
> >> > CryptoPP::SHA256 sha256hash;
> >> > unsigned char hash[ sha256hash.DIGESTSIZE ];
> >> > memset( hash, 0x00, sha256hash.DIGESTSIZE );
> >> > sha256hash.CalculateDigest(hash, (byte*)secretId, msg_len); // use
> >> > libcrypt0++ library
>
> >> > // hmac
> >> > unsigned char hmac[ sha256hash.DIGESTSIZE ];
> >> > memset( hmac, 0x00, sha256hash.DIGESTSIZE );
> >> > CryptoPP::HMAC<CryptoPP::SHA256>((byte*)secretId,
> >> > msg_len).CalculateDigest(hmac, hash, sha256hash.DIGESTSIZE);
>
> >> --
--
You received this message because you are subscribed to the "Crypto++ Users"
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at
http://www.cryptopp.com.