Расскажем об очень полезной библиотеке Apache Commons Codec ( https://commons.apache.org/proper/commons-codec/ ) - а точнее о классе утилит DigestUtils, который позволяет считать хеши от текста, файлов и т д. Поддерживает хеши md2, md5, sha1, sha256, sha384, sha512. Зависимость maven:
<dependency><groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
Подсчет хешей для текста:
import javax.swing.JOptionPane;
import org.apache.commons.codec.digest.DigestUtils;
public class HashTextTest {
public static String GetTextHash(String buf, String Tip) {
if (!buf.isEmpty()) {
switch (Tip) {
case "md2": return DigestUtils.md2Hex(buf);
case "md5": return DigestUtils.md5Hex(buf);
case "sha1": return DigestUtils.sha1Hex(buf);
case "sha256": return DigestUtils.sha256Hex(buf);
case "sha384": return DigestUtils.sha384Hex(buf);
case "sha512": return DigestUtils.sha512Hex(buf);
default: return "wrong hash type";
}
} else {
JOptionPane.showMessageDialog(null, "'Input Text' is empty !");
}
return "some error";
}
public static void main(String[] args) {
String tip = JOptionPane.showInputDialog("input Hash Type:");
String str = JOptionPane.showInputDialog("input string for hash:");
JOptionPane.showMessageDialog(null, GetTextHash(str, tip));
}
}
исходники - https://github.com/harp077/HashTextDemo
подсчет хеша строки для кодировки UTF-8:
import static org.apache.commons.codec.binary.StringUtils.getBytesUtf8;
String str = "bla-bla" ;
DigestUtils.md5Hex(getBytesUtf8(str));
Подсчет хешей для файлов:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.commons.codec.digest.DigestUtils;
public class HashFileTest {
public static String hash_of_File(File buffile, String tip) {
if (!buffile.isFile()) {
JOptionPane.showMessageDialog(null, "not a file !");
return "";
}
try (FileInputStream fis = new FileInputStream(buffile);
BufferedInputStream bis = new BufferedInputStream(fis);) {
switch (tip) {
case "md2": return DigestUtils.md2Hex(bis);
case "md5": return DigestUtils.md5Hex(bis);
case "sha1": return DigestUtils.sha1Hex(bis);
case "sha256": return DigestUtils.sha256Hex(bis);
case "sha384": return DigestUtils.sha384Hex(bis);
case "sha512": return DigestUtils.sha512Hex(bis);
default: return "wrong hash type";
}
} catch (FileNotFoundException fex) {
} catch (IOException ex) { }
return "";
}
public static void main(String[] args) {
String tip = JOptionPane.showInputDialog("input Hash Type:");
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Открыть файл");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
JOptionPane.showMessageDialog(null, hash_of_File(file, tip));
}
}
}
исходники - https://github.com/harp077/HashFileDemo
В commons-codec еще много что хорошего.import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.commons.codec.digest.DigestUtils;
public class HashFileTest {
public static String hash_of_File(File buffile, String tip) {
if (!buffile.isFile()) {
JOptionPane.showMessageDialog(null, "not a file !");
return "";
}
try (FileInputStream fis = new FileInputStream(buffile);
BufferedInputStream bis = new BufferedInputStream(fis);) {
switch (tip) {
case "md2": return DigestUtils.md2Hex(bis);
case "md5": return DigestUtils.md5Hex(bis);
case "sha1": return DigestUtils.sha1Hex(bis);
case "sha256": return DigestUtils.sha256Hex(bis);
case "sha384": return DigestUtils.sha384Hex(bis);
case "sha512": return DigestUtils.sha512Hex(bis);
default: return "wrong hash type";
}
} catch (FileNotFoundException fex) {
} catch (IOException ex) { }
return "";
}
public static void main(String[] args) {
String tip = JOptionPane.showInputDialog("input Hash Type:");
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Открыть файл");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
JOptionPane.showMessageDialog(null, hash_of_File(file, tip));
}
}
}
исходники - https://github.com/harp077/HashFileDemo
Комментариев нет:
Отправить комментарий