在Java中是否有任何方法可以生成一个字符串的MD5哈希?
当前回答
private String hashuj(String dane) throws ServletException{
try {
MessageDigest m = MessageDigest.getInstance("MD5");
byte[] bufor = dane.getBytes();
m.update(bufor,0,bufor.length);
BigInteger hash = new BigInteger(1,m.dige`enter code here`st());
return String.format("%1$032X", hash);
} catch (NoSuchAlgorithmException nsae) {
throw new ServletException("Algorytm szyfrowania nie jest obsługiwany!");
}
}
其他回答
你可以试着跟着走。查看详细信息和下载代码:http://jkssweetlife.com/java-hashgenerator-md5-sha-1/
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Example {
public static void main(String[] args) throws Exception {
final String inputString = "Hello MD5";
System.out.println("MD5 hex for '" + inputString + "' :");
System.out.println(getMD5Hex(inputString));
}
public static String getMD5Hex(final String inputString) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(inputString.getBytes());
byte[] digest = md.digest();
return convertByteToHex(digest);
}
private static String convertByteToHex(byte[] byteData) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}
Spring中也有一个DigestUtils类:
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/util/DigestUtils.html
这个类包含md5DigestAsHex()方法来完成这项工作。
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5 encryption
*
* @author Hongten
*
*/
public class MD5 {
public static void main(String[] args) {
System.out.println(MD5.getMD5("123456"));
}
/**
* Use md5 encoded code value
*
* @param sInput
* clearly
* @ return md5 encrypted password
*/
public static String getMD5(String sInput) {
String algorithm = "";
if (sInput == null) {
return "null";
}
try {
algorithm = System.getProperty("MD5.algorithm", "MD5");
} catch (SecurityException se) {
}
MessageDigest md = null;
try {
md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte buffer[] = sInput.getBytes();
for (int count = 0; count < sInput.length(); count++) {
md.update(buffer, 0, count);
}
byte bDigest[] = md.digest();
BigInteger bi = new BigInteger(bDigest);
return (bi.toString(16));
}
}
Codingkit上有一篇关于这个的文章。查看:http://codingkit.com/a/JAVA/2013/1020/2216.html
另一种实现:Java中的快速MD5实现
String hash = MD5.asHex(MD5.getHash(new File(filename)));
使用java.security.MessageDigest库的简单函数
public String stringMD5Hash(String text) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] bytes = messageDigest.digest(text.getBytes());
StringJoiner stringJoiner = new StringJoiner("");
for (byte b : bytes) {
stringJoiner.add(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
}
return stringJoiner.toString();
}
对于文件使用这个函数
public static String FileMD5Hash(File file) throws NoSuchAlgorithmException, IOException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] bytes = messageDigest.digest(getFileBytes(file));
StringJoiner stringJoiner = new StringJoiner("");
for (byte b : bytes) {
stringJoiner.add(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
}
return stringJoiner.toString();
}
public static byte[] getFileBytes(File file) throws IOException{
InputStream inputStream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, bytesRead);
}
return byteArrayOutputStream.toByteArray();
}
推荐文章
- 到底是什么导致了堆栈溢出错误?
- 为什么Android工作室说“等待调试器”如果我不调试?
- Java:路径vs文件
- ExecutorService,如何等待所有任务完成
- Maven依赖Servlet 3.0 API?
- 如何在IntelliJ IDEA中添加目录到应用程序运行概要文件中的类路径?
- getter和setter是糟糕的设计吗?相互矛盾的建议
- Android room persistent: AppDatabase_Impl不存在
- Java的String[]在Kotlin中等价于什么?
- Intellij IDEA上的System.out.println()快捷方式
- 使用Spring RestTemplate获取JSON对象列表
- Spring JPA选择特定的列
- URLEncoder不能翻译空格字符
- Java中的super()
- 如何转换JSON字符串映射<字符串,字符串>与杰克逊JSON