Преобразование строки в SHA-256:
public class SHA {
public static String hashSha256(String data) {
try {
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedHash = digest.digest(data.getBytes(StandardCharsets.UTF_8));
return bytesToHex(encodedHash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Cannot find SHA-256 algorithm", e);
}
}
private static String bytesToHex(byte[] hash) {
final StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
Мета информация
Область:: 00 Снипеты для Java, 00 Криптография
Родитель:: SHA-256
Источник::
Создана:: 2024-09-14
Автор::