How to get sha256 hash output as binary data instead of hex using powershell?

Question:

Is there an in-built method to generate SHA256 as a binary data for a given string ? Basically, I am using below bash script to first generate a hash as a binary data and then do a base64 encoding. All I want to have is the exact thing in Powershell so that both the outputs are identical:

In powershell, I can get the SHA256 in hex using the below script, but then struggling to get it as a binary data:

I can then use [Convert]::ToBase64String($Bytes) to convert to base64 but in between how do I get a binary data similar to bash output before I pass it to base64 conversion.

Answer:

I think you are over-doing this, since the ComputeHash method already returns a Byte[] array (binary data). To do what (I think) you are trying to achieve, don’t convert the bytes to string, because that will result in a hexadecimal string.

returns

n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg=

Source:

How to get sha256 hash output as binary data instead of hex using powershell? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply