Jump to content

SHA1 Hashing Problem ~ Unable to generate the right output


Xdwardo

Recommended Posts

Hi everyone  8)

 

I'm new around here but thought it's about time I joined a good PHP forum! I'll introduce myself properly on the right section, but for now, I'll my post my coding problem on here. I wonder if any has any knowledge or can help.

 

I'm setting up a connection from my web server to a potential data supplier web server, which involves a load of encryption. One of the stages is generating a SHA1 hash of an encrypted string.

 

Now I've got some old example code, however the "mhash" function used in this old code appears to obsolete. Thus is doesn't work.

 

I've tried using the available "sha1" and "hash" functions but cannot replicate the hashed output they provide.

 

Here's the original code:

$encrypted_string = "B0436CBFBC5CAAFB7339AF4A1DF845974D53B9D369146E2E4F1451929D9EBE254363E983F4F94517EB9585FDB112E7B1CCE11A33C5BBA23F8D5DE9D3415BA526489AC796A36FBA76D4293C8DFB673708CED10C9732EEC472D9E43D2626AA104121666E79DD8F2FF6BAC0143BD62E0EE826AF6459779C162613508D48BFE2FC8DD558A1834D7205F96EA8D446E9B371E78E990A3995B1052DCBA9CA0AF99CC77ED2A8B55B2B882BA29D4BB4B07FA91AB4D2F10FBB93732B077335A7E6D96FE813AEDC3711A85CD0C13AE22B28C14FCCE3AF4C1F5D2C0F7697DEC7487CCFC0ED4E77B1B65F39BAD5236E3D3C69D33FC484";

$hashBinaryValue = mhash(MHASH_SHA1, $encrypted_string);
$hashValue = bin2hex($hashBinaryValue);
echo 'hashValue='.$hashValue.'<br>';

 

The example hashed output should be:

31f6d26b18d3c04895cdc2cc05cbd9ad003f2d3e

 

I cannot seem to replicate this output using the available functions? I've tried the following:

$hashBinaryValue = hash('sha1', $encrypted_string);
$hashValue = bin2hex($hashBinaryValue);

 

And also:

$hashBinaryValue = sha1($encrypted_string);
$hashValue = bin2hex($hashBinaryValue);

 

Both generate:

37333736363862393037313732326265346438396433633236383936363430376434613665363231

 

I've found a webpage that can generate the SHA1 hash, but do not know what language they've done it in.

http://www.fileformat.info/tool/hash.htm?hex=B0436CBFBC5CAAFB7339AF4A1DF845974D53B9D369146E2E4F1451929D9EBE254363E983F4F94517EB9585FDB112E7B1CCE11A33C5BBA23F8D5DE9D3415BA526489AC796A36FBA76D4293C8DFB673708CED10C9732EEC472D9E43D2626AA104121666E79DD8F2FF6BAC0143BD62E0EE826AF6459779C162613508D48BFE2FC8DD558A1834D7205F96EA8D446E9B371E78E990A3995B1052DCBA9CA0AF99CC77ED2A8B55B2B882BA29D4BB4B07FA91AB4D2F10FBB93732B077335A7E6D96FE813AEDC3711A85CD0C13AE22B28C14FCCE3AF4C1F5D2C0F7697DEC7487CCFC0ED4E77B1B65F39BAD5236E3D3C69D33FC484

 

Any help or input would be greatly appreciated =)

 

Link to comment
Share on other sites

Just to give a more simpler way of putting it...

 

Using the word "hello" as an example...

 

I've tried this for:

echo sha1(pack("H*","hello")); 

 

The output is:

0f28dfede025e7786ec18a62943a80f17bde47fc 

 

I need the output of "hello" to be:

320355ced694aa69924f6bb82e7b74f420303fd9

 

Can anyone replicate this using a PHP SHA1 hashing function?

 

Link to comment
Share on other sites

That hash you're supposed to get for "hello",

320355ced694aa69924f6bb82e7b74f420303fd9

is the hash of character 14. Which is a control character. If that's actually the correct result then something is reducing "hello" to 0xE. I think you just got this one wrong.

 

As for the original, the encrypted value needs to be bytes, not the hex representation. Written in PHP code:

/* what you have */ "B0436CBFBC5CAAFB7339AF..."
/* what you need */ "\xB0\x43\x6C\xBF\xBC\x5C\xAA\xFB\x73\x39\xAF..."

Adjust whatever encryption thing you have to keep the output bytes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.