给我生成一个用java语言写的hash256加密的代码
时间: 2023-05-31 11:05:05 浏览: 125
SHA-256 java加密代码
以下是用Java语言实现的Hash256加密代码:
```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Hash256 {
public static void main(String[] args) {
String input = "Hello World";
String output = hash256(input);
System.out.println("Hash256 output: " + output);
}
public static String hash256(String input) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] hash = messageDigest.digest(input.getBytes());
StringBuilder stringBuilder = new StringBuilder();
for (byte b : hash) {
stringBuilder.append(String.format("%02x", b));
}
return stringBuilder.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
```
输入字符串"Hello World",输出Hash256加密后的字符串:"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"。
阅读全文