springboot md5
时间: 2024-06-23 20:01:44 浏览: 9
Spring Boot 是一个流行的 Java 框架,它简化了构建企业级应用的初始设置。MD5(Message-Digest Algorithm 5)是哈希函数的一种,用于数据完整性校验和密码散列。在 Spring Boot 中,你可以使用内置的 Java 密码工具类 `java.security.MessageDigest` 或者第三方库(如 Apache Commons Codec)来计算 MD5 值。
以下是一个简单的例子,展示如何在 Spring Boot 中使用 MD5 加密:
```java
import org.springframework.core.convert.converter.StringTo Md5Converter;
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;
public class Md5Example {
public static String calculateMD5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
return toHex(bytes);
} catch (Exception e) {
throw new RuntimeException("Failed to calculate MD5", e);
}
}
private static String toHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
public static void main(String[] args) {
String input = "Hello, Spring Boot!";
String md5Hash = calculateMD5(input);
System.out.println("MD5 hash of " + input + " is: " + md5Hash);
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)