public static void main(String[] args) throws Exception { String str = "resource"; System.out.println(Arrays.toString(getHash(str,"MD5").getBytes()));//使用默认解码后输出 } public static String getHash(String str, String hashType) { try { MessageDigest digest = MessageDigest.getInstance(hashType); digest.reset(); byte[] b = digest.digest(str.getBytes()); System.out.println(Arrays.toString(b)); //编码前输出 return new String(b); //使用默认编码 } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return str; }
Output:
[-106, -85, 78, 22, 63, 78, -32, 58, -86, 77, 16, 81, -86, 81, -46, 4]
[-17, -65, -67, -17, -65, -67, 78, 22, 63, 78, -17, -65, -67, 58, -17, -65, -67, 77, 16, 81, -17, -65, -67, 81, -17, -65, -67, 4]
Why are the outputs before encoding and decoding after encoding different?
You might think
(new String(b)).getBytes().equals(b)
,实际上并非如此。(尽管new String(s.getBytes()).equals(s)
it must be. )Because
byte[]
转换成String
时,有些字节是未必能转换成字符的,比如第一个-106
、第二个-85
就是,所以转换成String
时前两个就变成了未知字符(表面上会显示?
,但实际上是一个Unicode字符),再转成byte[]
(你这边defaultCharset
应该是UTF-8
), each unknown character becomes 3 bytes.If you use GBK, the situation is not bad, but still slightly different:
So the conclusion is:If you use String to represent a Hash value, you cannot force-convert byte[] to String, but convert it to hexadecimal representation as usual.