Reputation: 4799
I've trying to calculate the base64 encoded sha256 hash of some JavaScript at the command line using OpenSSL and via PHP.
cat test.js | openssl dgst -sha256 -binary | openssl enc -base64
(p5CrWBV6O1Xp7BlWwdVXTeGeSx3uo/IsNaYOIOblAZk=)
echo base64_encode(hash("sha256", "alert('Test');", true));
(2D7QyY5W4ziVZhF/vzeGy8oHgnz4TjqaoifV41mHi3c=)
The resulting base64 encoded values are not the same, but the input to each method is the same. Stepping back, the sha256 hash digests do not match before base64 encoding.
cat test.js | openssl dgst -sha256
(a790ab58157a3b55e9ec1956c1d5574de19e4b1deea3f22c35a60e20e6e50199)
echo hash("sha256", "alert('Test');");
(d83ed0c98e56e3389566117fbf3786cbca07827cf84e3a9aa227d5e359878b77)
I'm not sure where the problem is.
Upvotes: 1
Views: 1371
Reputation: 4799
I found the issue in the end thanks to some support on Twitter.
When editing the JS file with nano, it was appending a newline character on the end. Editing the file using nano -L test.js
resolved the problem.
Upvotes: 1
Reputation: 458
cat
is adding an extra newline character, which is going along for the ride in the digest. Try running the digest on the file directly:
openssl dgst -sha256 -binary test.js | ...
Upvotes: 0