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

前端 未结 8 507
迷失自我
迷失自我 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:18

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

    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

    0 讨论(0)
提交回复
热议问题