Archived Forum Post

Index of archived forum posts

Question:

Matching SHA-1 Output with PHP SHA1 Function

Dec 07 '12 at 11:32

I am using VB.net 2010 and I’m trying to get SHA1 output that matches the output from the PHP SHA1 function with the raw_output parameter set to true.

Would you know if this is possible with Chilkat Crypt and if so would you have any example code. I am using the trial version right now and am unable to get the same results as the PHP function.


Answer

SHA-1 produces 20 bytes of output. I knew nothing about PHP SHA1 / raw_output, so I did a Google search on "PHP SHA1 raw_output" -- easy enough to find out. The raw_output is the exact 20 bytes of output. (makes sense) However, don't try to display these bytes as characters -- because they're not characters. (How would you display a 0 byte, or control-characters??)

With Chilkat, you can get the output as a byte array, or the encoded representation of the raw bytes using hex, base64, or many other encodings. Assuming you are hashing the exact same input data, the result should be the same. You can verify you're getting what you expect by using a test vector. For example, see this: http://en.wikipedia.org/wiki/SHA-1

SHA1("The quick brown fox jumps over the lazy dog")
= 2fd4e1c6 7a2d28fc ed849ee1 bb76e739 1b93eb12
This is the result, displaying the bytes in hex format, that both Chilkat and PHP should produce for the above string.