Convert MD5 into String in java

后端 未结 4 1249
猫巷女王i
猫巷女王i 2021-02-04 17:46

Can anyone know how to convert the MD5 to String. In my case I have saved the password in MD5 in database. I am trying to retr

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 18:48

    I think that is the most elegant way to do that:

    public String getMD5(String data) {
            String result = null;
            MessageDigest md;
            try {
                md = MessageDigest.getInstance("MD5");
                md.update(data.getBytes(Charset.forName("UTF-8")));
                result = String.format(Locale.ROOT, "%032x", new BigInteger(1, md.digest()));
            } catch (NoSuchAlgorithmException e) {
                throw new IllegalStateException(e);
            }
            return result;
    }
    

    `

提交回复
热议问题