5

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"

4
  • 2
    Well either you need the many bits (for many information) or you don't. If the latter is the case, then why not truncate the hash string to the required length? As long as you always do it the same way, the result should also be the same (for equal inputs). But bear in mind that less bits lead to higher collision probability. Commented Jun 15, 2012 at 8:11
  • For what you need this? I hardly assume you want to generate temporary passwords, but remember, that hashes only consists of 16 different characters. Thats to less for passwords. Commented Jun 15, 2012 at 8:11
  • You should add some more explanation why you wish to create a hash with only 8 characters. It is very vulnearable. I fear that you might do a big mistake which will follow you on your wake and curse your project from the very insides. Commented Jun 15, 2012 at 8:19
  • @shantanuo: Did you also see this Commented Jun 15, 2012 at 8:29

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed it is. Thanks for pointing out that I forgot to add a notice to the answer. I hope the questioner will modify his/her question with more details where he/she wishes to use the method.
0

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.

2 Comments

While this might seem like a good idea, it is better not try to be smart. By combining multiple partial hashes you might generate an insecure one.
Hmm didn't post this in the context of 'secure' hashes though @fotanus but thinking back now, I realize, this will be a silly waste of hardware resources. In recent times murmurHash in particular will be a good alternative to crc, possibly xxHash as well.
-3
function short_hash($value, $length=8) { return substr(md5($value), 0, $length); } 

1 Comment

beware of collisions! don't use this for any security related hasing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.