2

I'll do my best to describe my predicament.

I'm writing a python script that will take an input file and perform a SHA-256 hash on it.

If I take the raw binary image and run it through the command prompt (on my mac: shasum -a 256 somefile.bin), I get the 'correct' digest: 'bda1ca...'

I've run this binary through an online calculator and get the same result.

If I open this hash in a hex editor, extract all of the bytes into my clipboard and paste them into a SHA256 calculator I get a different result: '689035...'

What am I missing here? I've tried upper/lower cash, spaces between each byte. I'm not sure what else to try.

This problem started because I'm implementing this in Python, but before I even worry about the code I'm trying to understand why this is different.

Andrew Corsini
  • 1,548
  • 3
  • 11
  • 11
  • I am going to choose dvxam's answer as correct. Actually, my python script was correct, but I was reading files out of order based on someone's incorrect instructions! However, dvxam explained it thoroughly enough that I get it now. – Andrew Corsini Apr 12 '16 at 19:38
  • If you're using `echo` be careful of the newline! `echo "hi" | shasum --algorithm=256 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 -` versus `echo -n "hi" | shasum --algorithm=256 8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4 -` – Joshua Pinter Nov 30 '21 at 21:01

1 Answers1

2

When you copy on your keyboard the "bytes" from your hex editor, you are most likely just copying a string representation of it.

maybe those commands will help you to get it :

$ echo "Hello, world" > my_bin

$ shasum -a 256 my_bin
37980c33951de6b0e450c3701b219bfeee930544705f637cd1158b63827bb390  -


$ cat my_bin | shasum -a 256
37980c33951de6b0e450c3701b219bfeee930544705f637cd1158b63827bb390  -

$ cat my_bin | xxd -p | shasum -a 256
0e535a1a279db9753bcbc226b9305e6e6a39ba2d866c01a44cb9aac8d5fc9833  -

(xxd is a hex editor i used to display content of the file)

dvxam
  • 604
  • 4
  • 12
  • Thank you, I understand that much. What is the way around this? For instance - the binary I am performing a SHA256 on is a smaller piece of a larger binary. If I SHA256 the smaller binary I get one result, but if I just "read' the values into my python script I get another. – Andrew Corsini Apr 12 '16 at 18:37