我需要在 MD5 哈希中转换字符串。
我正在使用
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex( byte[] bytes )
{
char[] hexChars = new char[ bytes.length * 2 ];
for( int j = 0; j < bytes.length; j++ )
{
int v = bytes[ j ] & 0xFF;
hexChars[ j * 2 ] = hexArray[ v >>> 4 ];
hexChars[ j * 2 + 1 ] = hexArray[ v & 0x0F ];
}
return new String( hexChars );
}
它给出的输出就像这个网站 http://www.md5.cz/
但我需要生成散列作为这个 http://webcodertools.com/hashstring 给出的输出。
请在两个站点中使用测试。
通过使用上述功能,我得到了第一个站点的 o/p,但我需要第二个站点提供的信息。
是否有任何不同的功能,或者我是否遗漏了什么?
谢谢。
最佳答案
第二个网站只是使用 base64 而不是 hex 将二进制数据表示为文本。所以你可以完全摆脱你的 bytesToHex
方法,只使用 Base64
:
String base64Digest = Base64.encodeToString(thedigest, Base64.DEFAULT);
(顺便说一句,我会避免在变量名中使用 the
作为前缀——它没有任何好处,而且很笨拙。)
关于java - android 或 java 中的 MD5 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24929675/