Small unique hash for Strings?

I have a list of Strings that can have maximum length 25.

I want to map each String to another unique String that is 25 character long.

One way i try was to use md5 (Java Security API) but the md5 hash(in hex) generated for

each String was around 40 character long! but i only have 25 character length

(yes i can not increase it due to some reason).

Is there any way to map a String on another hash or String that is unique?

regards,

TH

[485 byte] By [T-H.a] at [2007-10-2 3:28:52]
# 1

well, the md5 hash is 16 bytes. You can encode this as 32 characters using ASCII hex encoding. You can then keep the first 25 bytes and throw the rest away. That is one solution.

A better solution is to base64 encode the md5 hash. The base64 encoding of 16 bytes takes only 22 characters, so this is better.

When using md5, or any other cryptographic hash function, there is very small probability of having two strings hash to the same value at random. Using the methods outlined above, this probability is neglible. However, there are recent cryptanalytic results on md5 and even sha1 that show that, in certain scenarios, it may be much easier to choose a string that hashes to the same value as another string.

Perhaps the best option is to investigate algorithms for constructing what are called perfect hash functions. Just google "perfect hash function".

ghstarka at 2007-7-15 22:38:30 > top of Java-index,Security,Cryptography...