How to generate a unique hash code for string input in android…?

前端 未结 8 506
迷失自我
迷失自我 2020-12-13 03:41

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 pl

相关标签:
8条回答
  • 2020-12-13 04:03

    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.

    0 讨论(0)
  • 2020-12-13 04:03

    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;
            }
    }
    
    0 讨论(0)
  • 2020-12-13 04:10
    String input = "some input string";
    int hashCode = input.hashCode();
    System.out.println("input hash code = " + hashCode);
    
    0 讨论(0)
  • 2020-12-13 04:10

    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));
    }
    
    0 讨论(0)
  • 2020-12-13 04:13

    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

    0 讨论(0)
  • 2020-12-13 04:15

    For me it worked

       public static long getUniqueLongFromString (String value){
           return  UUID.nameUUIDFromBytes(value.getBytes()).getMostSignificantBits();
        }
    
    0 讨论(0)
提交回复
热议问题