MD5 hashing in Android

前端 未结 16 1995
醉梦人生
醉梦人生 2020-11-29 18:06

I have a simple android client which needs to \'talk\' to a simple C# HTTP listener. I want to provide a basic level of authentication by passing username/password in POST r

16条回答
  •  自闭症患者
    2020-11-29 18:57

    The androidsnippets.com code does not work reliably because 0's seem to be cut out of the resulting hash.

    A better implementation is here.

    public static String MD5_Hash(String s) {
        MessageDigest m = null;
    
        try {
                m = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
        }
    
        m.update(s.getBytes(),0,s.length());
        String hash = new BigInteger(1, m.digest()).toString(16);
        return hash;
    }
    

提交回复
热议问题