【发布时间】:2019-07-24 22:04:24
【问题描述】:
所以我编写了这段代码来转换为我的密码的哈希值,我现在想要获取哈希值并将其转换回字符串。怎么可能做到这一点?
package security;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashPassword {
public static String hashPassword(String password) throws NoSuchAlgorithmException
{
MessageDigest sha= MessageDigest.getInstance("SHA");
sha.update(password.getBytes());
byte [] b=sha.digest();
StringBuffer sb= new StringBuffer();
for(byte b1:b)
{
sb.append(Integer.toHexString(b1 & 0xff).toString());
}
return sb.toString();
}
public static void main(String[] args)
{
String password="1234";
System.out.println(password);
try
{
System.out.println(hashPassword(password));
}catch(NoSuchAlgorithmException e)
{}
}
}
【问题讨论】:
-
你没有。这就是哈希的意义所在。为什么你认为你需要原始字符串,你想要完成什么?
标签: java string hash passwords