Manuel
Manuel

Reputation: 1

Base64 + sha256 not giving expected result

I need to figure out why I get the wrong results, here is the thing:

Expected result

Mu0FBjARVsNyDiixnKqyLCCqVunTSPQFCMnOwGQsIWliY2Jh

Current Result

MzJlZDA1MDYzMDExNTZjMzcyMGUyOGIxOWNhYWIyMmMyMGFhNTZlOWQzNDhmNDA1MDhjOWNlYzA2NDJjMjE2OQ==

The code I'm using (php)

echo base64_encode(hash("sha256", $pass.$salt'));

This site http://www.insidepro.com/hashes.php giveme a preety closer result

sha256($pass.$salt) => Mu0FBjARVsNyDiixnKqyLCCqVunTSPQFCMnOwGQsIWk=

you can find (in the site, near the result field) a [1], that means "Hash in Base64"

the thing is... I can't even get the sites result


EDIT (Thanks Jon)

now the code goes like this

echo base64_encode(hash("sha256", $pass.$salt', true));

I'm getting the same result as in the site


the problem now is the difference between the two results

actual result   : Mu0FBjARVsNyDiixnKqyLCCqVunTSPQFCMnOwGQsIWk=
expected result : Mu0FBjARVsNyDiixnKqyLCCqVunTSPQFCMnOwGQsIWliY2Jh

Upvotes: 0

Views: 1581

Answers (1)

Jon
Jon

Reputation: 437734

hash returns a hex-encoded string of its output by default, while you are expecting it to return raw bytes. Luckily it accepts a third argument; set it to true like this:

echo base64_encode(hash("sha256", $pass.$salt', true));

Upvotes: 2

Related Questions