Getting SHA256 value from a file

I am trying to get the SHA256 hash from a selected filename. Most file downloads from trusted sites today list the SHA# signatures which you can use to verify the accuracy of the download. I wish to compare these.

I have got the MD5 hash by reading the file in chunks and processing the chunks. SHA does not appear to work this way. I have tried using Crypto.Hash with the encryption set to Crypto.Agorithm.SH256. I have tried reading the file into a memoryBlock as a Binary stream. Both methods suggested in the documentation. Neither returns any kind of expected results.

Is there a way to accomplish this without resorting to a plugin?

What is the output you are getting vs expected? Maybe the Xojo output needs to be converted to hex? Just a guess.

As an example:

EncodeHex(Crypto.SHA512("hello world"))
1 Like

Thanks! This looks promising. I did a quick test to verify the returned hash looked correct, but that was with a text string, not an entire file.

Now all I need to do is figure out how to get the contents of the file into one long Memory block I can test. With MD5 I use MD5.Process, to process each individual chunk of the binary stream as I read it in. There is no method to do this with the SHA# methods. I assume I have to load the entire file in order to check it.

My M_Crypto project includes this.

If you need speed, check the SHA256MBS class in MBS Xojo Encryption Plugin.

You can run SHA256MBS.HashFile function on a thread to have it hash the whole file and do that on a preemptive thread.