How to generate a unique hash code for string input in android...?
JavaAndroidHashHashmapJava Problem Overview
I wanted to generate a unique hash code for a string in put in android. Is there any predefined library is there or we have to generate manually. Please any body if knows please present a link or a code stuff.
Java Solutions
Solution 1 - Java
It depends on what you mean:
-
As mentioned
String.hashCode()
gives you a 32 bit hash code. -
If you want (say) a 64-bit hashcode you can easily implement it yourself.
-
If you want a cryptographic hash of a String, the Java crypto libraries include implementations of MD5, SHA-1 and so on. You'll typically need to turn the String into a byte array, and then feed that to the hash generator / digest generator. For example, see @Bryan Kemp's answer.
-
If you want a guaranteed unique hash code, you are out of luck. Hashes and hash codes are non-unique.
A Java String of length N has 65536 ^ N
possible states, and requires an integer with 16 * N
bits to represent all possible values. If you write a hash function that produces integer with a smaller range (e.g. less than 16 * N
bits), you will eventually find cases where more than one String hashes to the same integer; i.e. the hash codes cannot be unique. This is called the Pigeonhole Principle, and there is a straight forward mathematical proof. (You can't fight math and win!)
But if "probably unique" with a very small chance of non-uniqueness is acceptable, then crypto hashes are a good answer. The math will tell you how big (i.e. how many bits) the hash has to be to achieve a given (low enough) probability of non-uniqueness.
Solution 2 - Java
This is a class I use to create Message Digest hashes
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Sha1Hex {
public String makeSHA1Hash(String input)
throws NoSuchAlgorithmException, UnsupportedEncodingException
{
MessageDigest md = MessageDigest.getInstance("SHA1");
md.reset();
byte[] buffer = input.getBytes("UTF-8");
md.update(buffer);
byte[] digest = md.digest();
String hexStr = "";
for (int i = 0; i < digest.length; i++) {
hexStr += Integer.toString( ( digest[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return hexStr;
}
}
Solution 3 - Java
String input = "some input string";
int hashCode = input.hashCode();
System.out.println("input hash code = " + hashCode);
Solution 4 - Java
I use this i tested it as key from my EhCacheManager
Memory map ....
Its cleaner i suppose
/**
* Return Hash256 of String value
*
* @param text
* @return
*/
public static String getHash256(String text) {
try {
return org.apache.commons.codec.digest.DigestUtils.sha256Hex(text);
} catch (Exception ex) {
Logger.getLogger(HashUtil.class.getName()).log(Level.SEVERE, null, ex);
return "";
}
}
am using maven but this is the jar commons-codec-1.9.jar
Solution 5 - Java
You can use this code for generating has code for a given string.
int hash = 7;
for (int i = 0; i < strlen; i++) {
hash = hash*31 + charAt(i);
}
Solution 6 - Java
For me it worked
public static long getUniqueLongFromString (String value){
return UUID.nameUUIDFromBytes(value.getBytes()).getMostSignificantBits();
}
Solution 7 - Java
A few line of java code.
public static void main(String args[]) throws Exception{
String str="test string";
MessageDigest messageDigest=MessageDigest.getInstance("MD5");
messageDigest.update(str.getBytes(),0,str.length());
System.out.println("MD5: "+new BigInteger(1,messageDigest.digest()).toString(16));
}
Solution 8 - Java
Let's take a look at the stock hashCode() method:
public int hashCode() {
int h = hash;
if (h == 0 && count > 0) {
for (int i = 0; i < count; i++) {
h = 31 * h + charAt(i);
}
hash = h;
}
return h;
}
The block of code above comes from the java.lang.String class. As you can see it is a 32 bit hash code which fair enough if you are using it on a small scale of data. If you are looking for hash code with more than 32 bit, you might wanna checkout this link: http://www.javamex.com/tutorials/collections/strong_hash_code_implementation.shtml