Coder1
Coder1

Reputation: 13321

What's a good hash to use between PHP and Python?

I have the luxury of starting from scratch, so I'm wondering what would be a good hash to use between PHP and Python.

I just need to be able to generate the same hash from the same text in each language.

From what I read, PHP's md5() isn't going to work nicely.

Upvotes: 1

Views: 158

Answers (3)

lig
lig

Reputation: 3890

I suggest to use sha1 as it is implemented out of the box in both but has no collision valnurabilities like md5. See: http://en.wikipedia.org/wiki/MD5#Collision_vulnerabilities

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88647

md5() always plays nicely - it always does the same thing because it is a standard hashing format.

The only tripping hazard is that some languages default return format for an MD5 hash is a 32 byte ascii string containing hexadecimal characters, and some use a 16 byte string containing a literal binary representation of the hash.

PHP's md5() by default returns a 32-byte string, but if you pass true to the second argument, it will return the 16 byte form instead. So as long as you know which version your other language uses (in you case Python), you just need to make sure that you get the correct format from PHP.

You may be better using the 32-byte form anyway, depending on how your applications communicate. If you use a communication protocol based on plain-text (such as HTTP) it is usually safer to use plain-text versions of anything - binary, in this case, is smaller, but liable to get corrupted in transmission by badly written servers/clients.

The binary vs. ascii problem applys to just about any hashing algorithm you can think of.

Upvotes: 5

symcbean
symcbean

Reputation: 48357

What is it you want from the hash? (portability, security, performance....)

From what I read, PHP's md5() isn't going to work nicely.

What did you read? Why won't it work?

I just need to be able to generate the same hash from the same text in each language

Since PHP only provides crc32 (very insecure), md5 and sha1 out of the box, it's not exactly a huge amount of testing you need to do. Of course if portability is not an issue then there's the mcrypt and openssl apis available. And more recently the hash PECL gives you a huge choice.

Upvotes: 4

Related Questions