问题描述
我有一个密码存储在ldap中作为md5哈希: {MD5} 3CydFlqyl / 4AB5cY5ZmdEA ==
从它的外观来看,它是base64编码的。
如何将从ldap接收的字节数组转换为可读的md5-hash-style字符串,如下所示: 1bc29b36f623ba82aaf6724fd3b16718
?
是 {MD5}
哈希或ldap的一部分是否添加它并且应该在解码之前将其删除?
String b = Base64.decodeBase64的(a)的ToString();
它返回 - [B @ 24bf1f20
。可能这是一个错误的编码,但当我将其转换为UTF-8时,我看到不可读的字符。
那么,我该怎么做才能解决这个问题?
看来上面的答案是针对C#的,因为那里对于Java中的StringBuilder类,没有这样的AppendFormat方法。
这是正确的解决方案:
public static String getMd5Hash(String str)throws NoSuchAlgorithmException,UnsupportedEncodingException
{
MessageDigest md = MessageDigest.getInstance(MD5);
byte [] thedigest = md.digest(str.getBytes(UTF-8));
StringBuilder hexString = new StringBuilder();
for(int i = 0; i< thedigest.length; i ++)
{
String hex = Integer.toHexString(0xFF& thedigest [i]);
if(hex.length()== 1)
hexString.append('0');
hexString.append(hex);
}
返回hexString.toString()。toUpperCase();
}
I have a password stored in ldap as md5 hash: {MD5}3CydFlqyl/4AB5cY5ZmdEA==
By the looks of it, it's base64 encoded.How can i convert byte array received from ldap to a nice readable md5-hash-style string like this: 1bc29b36f623ba82aaf6724fd3b16718
?Is {MD5}
a part of hash or ldap adds it and it should be deleted before decoding?
I've tried to use commons base64 lib, but when i call it like this:
String b = Base64.decodeBase64(a).toString();
It returns this - [B@24bf1f20
. Probably it's a wrong encoding, but when i convert it to UTF-8 i see unreadable chars.So, what can i do to solve this?
It appears the above answer was for C#, as there is no such AppendFormat method for the StringBuilder class in Java.
Here is the correct solution:
public static String getMd5Hash(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException
{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(str.getBytes("UTF-8"));
StringBuilder hexString = new StringBuilder();
for (int i = 0; i < thedigest.length; i++)
{
String hex = Integer.toHexString(0xFF & thedigest[i]);
if (hex.length() == 1)
hexString.append('0');
hexString.append(hex);
}
return hexString.toString().toUpperCase();
}
这篇关于将Base64编码的md5转换为可读的String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!