shantanuo
shantanuo

Reputation: 32316

shortest possible hash value

I have gone through this page...

http://www.php.net/manual/en/function.hash.php

MD5 is 32 characters long while sha1 is 40. For e.g.

$str = 'apple';

sha1 string d0be2dc421be4fcd0172e5afceea3970e2f3d940
md5 string 1f3870be274f6c49b3e31a0c6728957f

Even if the optional raw_output is set to TRUE, then the md5 digest is instead returned in raw binary format with a length of 16.

I am looking for a function that will create hash that will be equal or less than 8 characters.

Update: I need smaller strings for 3 reasons:

1) MySQL Archive table type does not seem to allow an index on a column that has more than 8 chars

2) I am planning to use key-value utility like redis that likes smaller keys

3) Security is not an issue here. I am hashing columns like "country + telco + operator"

Upvotes: 5

Views: 5799

Answers (3)

Whisperity
Whisperity

Reputation: 3042

You can use the crc32 method to create the hash, it creates an 8 character long hash.

$hash = hash('crc32', $input, FALSE);

Be extremely cautious with this method, as it is totally exposed to cryptographic attacks. Do NOT use this for any sort of security cheking.

Upvotes: 13

Chibueze Opata
Chibueze Opata

Reputation: 10054

Left for me, I would suggest you create your own custom 'encryption' algorithm that generates 8 characters for you. You can even decide for instance to do an md5, SHA1 and SHA512, and combine in the ratio of 2:3:3 to get your own custom code.

Upvotes: 0

VeeWee
VeeWee

Reputation: 580

function short_hash($value, $length=8) {
    return substr(md5($value), 0, $length);
}

Upvotes: -3

Related Questions