Get MD5 String from Message Digest

前端 未结 11 1250
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 20:46

I understand how it works but if I want to print out the MD5 as String how would I do that?

public static void getMD5(String fileName) throws Exception{
            


        
11条回答
  •  孤城傲影
    2020-12-23 20:56

    FYI...

    In certain situations this did not work for me

    md5 = new java.math.BigInteger(1, digest.digest()).toString(16);
    

    but this did

    StringBuilder sb = new StringBuilder();
    
    for (int i = 0; i < digest.length; i++) {
        if ((0xff & digest[i]) < 0x10) {
            sb.append("0").append(Integer.toHexString((0xFF & digest[i])));
        } else {
            sb.append(Integer.toHexString(0xFF & digest[i]));
        }
    }
    
    String result = sb.toString();
    

提交回复
热议问题