0

I am looking for an algorithm which creates a unique key for a string. The generated key for the string should be the same for every instance of execution of the code. I want to serialize the key and the string into a file and deserialize (binary file)it on some other platform like an android application , set top box, etc...

Can some one help me in this regards?

user1453251
  • 13
  • 1
  • 4
  • 2
    Simplest way is to get hash of it (_using default hashCode() method_). – Santosh Apr 14 '14 at 14:36
  • Be careful using the hashCode for generating unique key, you can have some collisions. Ex: `"Cc".hashCode() == "DD".hashCode()`. – Alexis C. Apr 14 '14 at 14:42
  • How unique do you want it to be? – Kayaman Apr 14 '14 at 14:45
  • you have this tagged [java] and [ruby]. why? – ddavison Apr 14 '14 at 14:58
  • The value generated for the same string should be remain constant for different executions. Example: For a string "I am Satya I wanted to be an architect". If the value generated for the above string is some "12345e54". The value should be the same of i run the code later again. I wanna know if i can use any libraries written in ruby / java and any other third party library. – user1453251 Apr 14 '14 at 15:10

2 Answers2

3

You can use the hashCode() function.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#hashCode()

Eddie Curtis
  • 1,207
  • 8
  • 20
  • 2
    I think it's worth pointing out that while it's generally accepted that String.hashCode() will be consistent across implementations, it's not guaranteed. See here: http://stackoverflow.com/questions/785091/consistency-of-hashcode-on-a-java-string – krispy Apr 14 '14 at 14:50
  • I am looking for an implementation which generates a constant value for several executions like md5 value for an unmodified file. – user1453251 Apr 14 '14 at 15:17
  • As mentioned in the link, String.hashCode() will usually do this, but could be risky when used with, for example, a set-top-box VM which happens to use a different string hashing function. MD5 works on strings too, so you might as well use it. – krispy Apr 14 '14 at 15:29
2

You could use the String.hashCode() method. However the hashCode() method is implementation specific, which means that (in theory) it cannot be used across platforms. Another common way of doing this is to use an MD5 hash.

MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes("UTF-8"));
return new BigInteger(1, md.digest()).intValue();

That should generate a unique integer for every string. It should be possible to get the same integer from the same string using MD5 algorithms on other Java platforms and even from other programming languages.

krispy
  • 1,244
  • 14
  • 19
  • Does it output the same value for every instance of the same string. I mean will the vlaue be the same if i execute the code some time later(twice ,thrice, ....)? – user1453251 Apr 14 '14 at 15:14
  • Yes. Definitely. As long as the string has the same characters it will always output the same integer. – krispy Apr 14 '14 at 15:17