$ echo -e 'blob 14\0Hello, World!' | shasum
produces: 8ab686eafeb1f44702738c8b0f24f2567c36da6d
running this in js/node:
var sha1 = require('sha1');
const fileContents = "Hello, World!";
const length = fileContents.length + 1;
const blobString = `blob ${length}\0${fileContents}`;
const hash = sha1(blobString);
console.log(blobString);
console.log(hash);
produces:
blob 14Hello, World!
d4fcfe4d339d4e59d168fdf3c0ad445fa980a7d6
why are the hashes not equal ? (8ab686eafeb1f44702738c8b0f24f2567c36da6d != d4fcfe4d339d4e59d168fdf3c0ad445fa980a7d6
)
The hashes are not equal because of the difference of a newline character in the input.
echo
adds a newline. Use printf
instead:
printf 'blob 14\0Hello, World!' | shasum
# prints: d4fcfe4d339d4e59d168fdf3c0ad445fa980a7d6
This works too, but not as portable, because the flags of echo
are not supported predictably in all systems:
echo -ne 'blob 14\0Hello, World!' | shasum