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

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

    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;
            }
    }
    

提交回复
热议问题