SHA256 签名加密工具类
时间: 2023-08-24 11:04:51 浏览: 54
以下是一个Java实现的SHA256签名加密工具类:
```
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA256Util {
public static String encrypt(String input) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(input.getBytes());
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
```
使用示例:
```
String input = "hello world";
String encrypted = SHA256Util.encrypt(input);
System.out.println("Input: " + input);
System.out.println("Encrypted: " + encrypted);
```
输出:
```
Input: hello world
Encrypted: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
```