0.前言
该程序要用到eclipse中的swt插件来实现可视化编程
插件的安装及使用操作可在我这一篇博客中获取
https://blog.csdn.net/leon9dragon/article/details/91864590
(非常简便,2分钟就能安装完了,而且可视化编程十分好用,强烈推荐)
下图是最终实现的效果
1.代码实现
package com.blog.sha;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
public class testSHA extends JFrame {
private JPanel contentPane;
private JTextField pathField;
private JTextField shaField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
testSHA frame = new testSHA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public testSHA() {
setTitle("\u6D88\u606F\u6458\u8981\u754C\u9762");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
pathField = new JTextField();
pathField.setBounds(69, 67, 211, 21);
contentPane.add(pathField);
pathField.setColumns(10);
JButton chooseBton = new JButton("\u9009\u62E9\u6587\u4EF6");
chooseBton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
chooseaction(arg0);
}
});
chooseBton.setBounds(290, 66, 93, 23);
contentPane.add(chooseBton);
shaField = new JTextField();
shaField.setEditable(false);
shaField.setBounds(113, 138, 311, 21);
contentPane.add(shaField);
shaField.setColumns(10);
JButton comfirmBton = new JButton("\u786E\u8BA4");
comfirmBton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
confirmaction(arg0);
}
});
comfirmBton.setBounds(10, 137, 93, 23);
contentPane.add(comfirmBton);
JLabel label = new JLabel("\u8F93\u51FA\u6587\u4EF6\u7684\u6D88\u606F\u6458\u8981");
label.setForeground(Color.RED);
label.setFont(new Font("黑体", Font.BOLD, 15));
label.setBounds(186, 113, 167, 15);
contentPane.add(label);
}
//选择文件事件
public void chooseaction(ActionEvent arg0){
JFileChooser fileChooser=new JFileChooser("C:/Users/Administrator/Desktop");//默认搜寻路径,可根据自己的需要进行修改
if(fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
pathField.setText(fileChooser.getSelectedFile().getPath());//获取文本框给出的文件路径
}
}
//确认事件,输出文件消息摘要
public void confirmaction(ActionEvent arg0){
//首先获取加密文件的路径
String filePath = null;
filePath = pathField.getText();//取的要进行加密的文件的路径
File shaFile = null;//定义要获取摘要的文件
try {
shaFile = new File(filePath);
if(!shaFile.exists()) {
JOptionPane.showMessageDialog(null, "请先选择文件");
throw new NullPointerException("SHA file is empty");
}
shaField.setText(testSHA.encrypt(shaFile));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//摘要算法名称, SHA-1, SHA-256, SHA-512
private final static String ALGORITHM = "SHA-1";
// 提取字节数组的SHA,data是待提取字节数组
public static String encrypt(byte[] data) throws Exception {
//获取SHA摘要算法实例
MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM);
//计算消息摘要
byte[] bytes = messageDigest.digest(data);
//返回16进制格式字符串
return byte2Hex(bytes);
}
//提取字符的SHA,data是待提取字符串
public static String encrypt(String data) throws Exception {
return encrypt(data.getBytes());
}
//提取数据流的SHA,inputStream是待提取数据流
public static String encrypt(InputStream inputStream) throws Exception {
//获取SHA摘要算法实例
MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM);
//计算消息摘要
byte[] bytes = new byte[1024];
while (inputStream.read(bytes) != -1) {
messageDigest.update(bytes);
}
//返回16进制格式字符串
return byte2Hex(messageDigest.digest());
}
//提取文件的SHA,file是待提取文件
public static String encrypt(File file) throws Exception {
return encrypt(new FileInputStream(file));
}
//将byte转为16进制
private static String byte2Hex(byte[] bytes) {
StringBuilder data = new StringBuilder(bytes.length);
for (byte b : bytes) {
data.append(Integer.toHexString((b >> 4) & 0xf)); //高4位
data.append(Integer.toHexString(b & 0xf)); //低4位
}
return data.toString();
}
}
2.运行效果
1)选择文件
2)点击确认后,输出消息摘要