tags:

views:

708

answers:

5

I don't think I was specific enough last time. Here we go:

I have a hex string:

742713478fb3c36e014d004100440041004 e0041004e00000060f347d15798c9010060 6b899c5a98c9014d007900470072006f007 500700000002f0000001f7691944b9a3306 295fb5f1f57ca52090d35b50060606060606

The last 20 bytes should (theoretically) contain a SHA1 Hash of the first part (complete string - 20 bytes). But it doesn't match for me.

Trying to do this with PHP, but no luck. Can you get a match?

Ticket:

742713478fb3c36e014d004100 440041004e0041004e00000060 f347d15798c90100606b899c5a 98c9014d007900470072006f00 7500700000002f0000001f7691944b9a

sha1 hash of ticket appended to original:

3306295fb5f1f57ca52090d35b50060606060606

My sha1 hash of ticket:

b6ecd613698ac3533b5f853bf22f6eb4afb94239

Here's what is in the ticket and how it's being stored. FWIW, I can pull out username, etc, and spot the various delimiters. http://www.codeproject.com/KB/aspnet/Forms_Auth_Internals/AuthTicket2.JPG

Edited: I have discovered that the string is padded on the end by the decryption function it goes through before this point. I removed the last 6 bytes and adjusted by ticket and hash accordingly. Still doesn't work, but I'm closer.

+2  A: 

Try trimming the string first, its suprisingly easy to have a newline or space on the end that changes the hash completely.

ck
I can correctly extract other information and on examination of the string it is formatted appropriately, delimiters where expected, etc. I wish this was the problem!
lynn
Actually, that is indeed part of the problem. The string is decrypted, and the function used pads the end of the string.
lynn
+5  A: 

Your ticket is being calculated on the hex string itself. Maybe the appended hash is calculated on another representation of the same data?

Welbog
To clarify, I tried trimming the last 20 bytes from the raw data, no luck.
lynn
What format do you get the value in? What is the value itself representing? Where are you getting it? Hashes are chaotic by design: different formats will make a huge difference. Can you not ask how the original hash is being calculated?
Welbog
The ticket: http://www.codeproject.com/KB/aspnet/Forms_Auth_Internals/AuthTicket2.JPG This is hashed, the hash is then appended.
lynn
Yeah, but what is being hashed? Is it being hashed byte by byte? Is its hex, octal, decimal, etc representation being hashed? Is this something you can find out?
Welbog
So you accepted my answer? What was the actual problem? I'm curious.
Welbog
It's a hunch. I'm still working on it, but this must be the problem as my .net cohort can replicate the hash on his end. I'll update with what I discover when I finish.
lynn
OK, thanks. I really appreciate being kept in the loop.
Welbog
I'm still struggling with this, but it's narrowed down to something to do with the way I'm representing my hex data. http://stackoverflow.com/questions/615157/how-do-i-sha1-hash-hex-values-correctly-in-php
lynn
+3  A: 

I think you are getting confused about bytes vs characters.

Internally, php stores every character in a string as a byte. The sha1 hash that PHP generates is a 40 character (40 byte) hexademical representation of the 20-byte binary data, since each binary value needs to be represented by 2 hex characters.

I'm not sure if this is the actual source of your discrepancy, but seeing this misunderstanding makes me wonder if it's related.

Peter Bailey
$ticket = substr($decrypthex, 0, -40);$hash = substr($decrypthex, -40);echo sha1($ticket);
lynn
Still characters... What you want to do is for each 2 characters, do chr(hexdec('0b')); (0b is an example, ill let you create the loop logic)
Andrew Moore
Ah, thanks. I'll try that.
lynn
Also, keep in mind that if the original code is in ASP.net, you may also have a character encoding difference (UTF-8 vs ASCII)
Andrew Moore
+1  A: 

According to this Online SHA1 tool the hash of the given text (after removing new lines and spaces) is

b6ecd613698ac3533b5f853bf22f6eb4afb94239

Idea: Make sure your inputing characters not a hex number to the PHP version.

Chris Nava
That's what I'm getting, see above where I show My sha1 hash of ticket.
lynn
A: 

The problem was that the original was a keyed hash. I had to use hash_hmac() with a validation key rather than sha1() without.

lynn