Different result HMAC SHA-256 in C++ and PHP -


i encode string base64 , generate hmac sha256 base64 string. encode result of hmac(char bytes) base64.

i use c++: openssl library.

and got different results in php , c++:

c++:

json: {"req_hash":"someuniqcodehash","answer":true}

base64: eyjyzxffagfzaci6innvbwvvbmlxq29kzuhhc2gilcjhbnn3zxiionrydwv9

hmac: p/p2ylxl8xdhmn+qialvlfds50ai4je/l1pmmrkzkre=

php:

json: {"req_hash":"someuniqcodehash","answer":true}

base64: eyjyzxffagfzaci6innvbwvvbmlxq29kzuhhc2gilcjhbnn3zxiionrydwv9

hmac: p/p2ylxl8xdhmn+qialvlfds50ai4je/l1pmmrkzkre=

and if can see, hmac same result!

c++: p/p2ylxl8xdhmn+qialvlfds50ai4je/l1pmmrkzkre=

php: p/p2ylxl8xdhmn+qialvlfds50ai4je/l1pmmrkzkre=

but, when change json (changed true false):

json: {"req_hash":"someuniqcodehash","answer":false}

i got this:

c++:

json: {"req_hash":"someuniqcodehash","answer":false}

base64: eyjyzxffagfzaci6innvbwvvbmlxq29kzuhhc2gilcjhbnn3zxiiomzhbhnlfq==

hmac: znuows2mmlpjibspq2gfsnival8iudczxzs24d0=

php:

json: {"req_hash":"someuniqcodehash","answer":false}

base64: eyjyzxffagfzaci6innvbwvvbmlxq29kzuhhc2gilcjhbnn3zxiiomzhbhnlfq==

hmac: znuows2mmlpjibspq2gfsnival8iudczxzs24d0ahza=

why hmac result different?

you can see:

c++: znuows2mmlpjibspq2gfsnival8iudczxzs24d0=

php: znuows2mmlpjibspq2gfsnival8iudczxzs24d0ahza=

in php hmac string added chars: ...ahza=. what this?

and php code:

 <?php  $b = base64_encode('{"req_hash":"someuniqcodehash","answer":false}');  $hmac =$b.".".base64_encode(hash_hmac('sha256',$b,'eyjhzgryzxnzx3rvijp7jzenoidjbgll',true)); 

my c++ code:

std::string sfjson = "{\"req_hash\":\"someuniqcodehash\",\"answer\":false}"; std::cout << "json: " << sfjson << "\n";  std::string fencoded_data = base64_encode_str(sfjson); std::cout << "base64: " << fencoded_data << "\n";  unsigned char* digest; std::string key = "eyjhzgryzxnzx3rvijp7jzenoidjbgll";  digest = hmac(evp_sha256(), reinterpret_cast<const unsigned char*>(key.c_str()), key.length(), reinterpret_cast<const unsigned char*>(fencoded_data.c_str()), fencoded_data.length(), null, null);  std::string sname(reinterpret_cast<char*>(digest));  std::string hmac_data = base64_encode_str(sname);  std::cout << "hmac: " << hmac_data << "\n"; 

base64 there: https://gist.github.com/rustem-art/5f6b510c65bbbfd279386225b978f960

i found solution! ;)

never use null as:

digest = hmac(evp_sha256(),               reinterpret_cast<const unsigned char*>(key.c_str()), key.length(),               reinterpret_cast<const unsigned char*>(data.c_str()), data.length(),               null, null); 

use special variable (in code is: diglen , result) return function.

unsigned int diglen;  unsigned char result[evp_max_md_size];  digest = hmac(evp_sha256(),               reinterpret_cast<const unsigned char*>(key.c_str()), key.length(),               reinterpret_cast<const unsigned char*>(data.c_str()), data.length(),               result, &diglen); 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -