zoukankan      html  css  js  c++  java
  • java生成字符串md5函数类(javaSE)

    //实现生成MD5值
    import java.io.BufferedInputStream;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    public class Digest {
    	public Digest() {
    		// TODO Auto-generated constructor stub
    	}
    	/**
    	 * @param args
    	 */
    	public static StringBuilder check(String path) {
    		// TODO Auto-generated method stub
    		StringBuilder sb = new StringBuilder();
    		byte[] size = null;
    		StringBuilder noAlgorithm=new StringBuilder("无法使用MD5算法,这可能是你的JAVA虚拟机版本太低");
    		StringBuilder fileNotFound=new StringBuilder("未能找到文件,请重新定位文件路径");
    		StringBuilder IOerror=new StringBuilder("文件输入流错误");
    		try {
    			MessageDigest md5=MessageDigest.getInstance("MD5");//生成MD5类的实例
    			File file = new File(path); //创建文件实例,设置路径为方法参数
    			FileInputStream fs = new FileInputStream(file); 
    			BufferedInputStream bi = new BufferedInputStream(fs); 
    			ByteArrayOutputStream bo = new ByteArrayOutputStream(); 
    			byte[] b = new byte[bi.available()]; //定义字节数组b大小为文件的不受阻塞的可访问字节数
    			int i; 
    			//将文件以字节方式读到数组b中
    			while ((i = bi.read(b, 0, b.length)) != -1) 
    			{ 
    			} 
    			md5.update(b);//执行MD5算法
    			for (byte by : md5.digest()) 
    			{
    			sb.append(String.format("%02X", by));//将生成的字节MD5值转换成字符串
    			}
    			bo.close();
    			bi.close();
    		} catch (NoSuchAlgorithmException e) {
    			// TODO Auto-generated catch block
    			return noAlgorithm;
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			return fileNotFound;
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			return IOerror;
    		}
            return sb;//返回MD5值
    	}
    }
    

      

    //生成窗体类为主类
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    import javax.swing.plaf.metal.MetalLookAndFeel;
    public class MainFrame extends JFrame implements ActionListener,MouseListener{
     JTextField fileSource=new JTextField(36);
     JTextField produceMD5=new JTextField(36);
     JTextField showEqual=new JTextField("请在此处输入源MD5值",36);
     JButton choiceFile=new JButton("选择文件");
     JButton createMD5=new JButton("生成MD5");
     JButton judgement=new JButton("对比");
     JPanel panel;
     JFileChooser fileChooser=new JFileChooser();
    	public MainFrame() {
    		// TODO Auto-generated constructor stub
    		super("MD5工具");
    		//设置主窗体的观感为金属外观
    			try {
    				UIManager.setLookAndFeel(new MetalLookAndFeel());
    			} catch (UnsupportedLookAndFeelException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		//窗体布局使用GridBagLayout
    		GridBagLayout gbl=new GridBagLayout();
    		GridBagConstraints gbc=new GridBagConstraints();
    		panel=new JPanel(gbl);
    		panel.setBorder(BorderFactory.createTitledBorder("xiaohb's MD5 check tool"));
    		gbc.fill=GridBagConstraints.BOTH;
    		gbc.weightx=1.0;
    		gbc.weighty=1.0;
    		gbl.setConstraints(fileSource, gbc);
    		panel.add(fileSource);
    		gbc.weightx=0.0;
    		gbc.gridwidth=GridBagConstraints.REMAINDER;
    		gbl.setConstraints(choiceFile, gbc);
    		panel.add(choiceFile);
    		gbc.gridwidth=1;
    		gbl.setConstraints(produceMD5, gbc);
    		panel.add(produceMD5);
    		gbc.weightx=0.0;
    		gbc.gridwidth=GridBagConstraints.REMAINDER;
    		gbl.setConstraints(createMD5, gbc);
    		panel.add(createMD5);
    		gbc.gridwidth=1;
    		gbl.setConstraints(showEqual, gbc);
    		panel.add(showEqual);
    		gbc.weightx=0.0;
    		gbc.gridwidth=GridBagConstraints.REMAINDER;
    		gbl.setConstraints(judgement, gbc);
    		panel.add(judgement);
    		add(panel);
    		//给按钮添加注册器
    		showEqual.addMouseListener(this);
    		choiceFile.addActionListener(this);
    		createMD5.addActionListener(this);
    		judgement.addActionListener(this);
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    MainFrame frame=new MainFrame();
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setSize(350, 200);
    frame.setResizable(false);
    frame.setVisible(true);
    	}
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		Digest digest=new Digest();
    		JOptionPane prompt=new JOptionPane();
    		if(e.getSource()==choiceFile){
    			fileChooser.showOpenDialog(this);
    				fileSource.setText(fileChooser.getSelectedFile().toString());//显示选择的文件名
    		}else if(e.getSource()==createMD5){
    			produceMD5.setText((digest.check(fileSource.getText())).toString());//生成的MD5值显示在文本区域内
    		}else if(e.getSource()==judgement){
    			//判断MD5值是否相同
    			if(produceMD5.getText().equalsIgnoreCase(showEqual.getText())){
    				prompt.showMessageDialog(this, "两个MD5值相同,文件安全!");
    			}else{
    				prompt.showMessageDialog(this, "两个MD5值不同,文件可能被篡改,请检查!");
    			}
    		}
    	}
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		if(e.getSource()==showEqual){
    			showEqual.setText("");
    		}
    	}
    	public void mouseEntered(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mousePressed(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	public void mouseReleased(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    

      

  • 相关阅读:
    【iOS】打印方法名
    【iOS】设备系统版本
    【iOS】receiver type *** for instance message is a forward declaration
    【iOS】获取应用程序本地路径
    hash算法
    redis文档
    Couchbase
    nodejs多核处理
    基于nodejs的消息中心
    nodejs两个例子
  • 原文地址:https://www.cnblogs.com/koal/p/4394035.html
Copyright © 2011-2022 走看看