I have a table with a column of unique string values. The max length of the string value is 255 char. I want to generate a unique id with the string value as input. In other words I am looking for a compact representation for a string. The unique id generated can be alpha-numeric. A useful feature to have would be to be able to regenerate the string value from the unique id.
Is there an efficient function to generate such an unique id. Some ways could be using checksum or hash functions. I want to know if there is a standard way to do this.
I am using MySql database and java.
Thanks!
–edit: I am looking for a more compact representation rather than just using the string itself.
How unique is “unique”? Using any good hashing function (MD5 is decent for most uses, and easily implemented via java.security.MessageDigest.getInstance(“MD5”) can get you to a 128-bit number that’s very very likely to be unique. Using a subset of the hash gets you a smaller ID, with a higher chance of collision.
Using an auto_increment field in the DB, if it fits your design, might be easier to implement, will truly guarantee uniqueness, and will use smaller IDs than the 16 bytes of MD5. You can also then meet your requirement of finding the string by the key, which you can’t do for a hash.