Created
November 15, 2012 22:13
-
-
Save airborn/4081730 to your computer and use it in GitHub Desktop.
File Digest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package filedigest; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.math.BigInteger; | |
import java.security.DigestInputStream; | |
import java.security.MessageDigest; | |
import javax.swing.JFileChooser; | |
public class Main extends javax.swing.JFrame { | |
public Main() { | |
initComponents(); | |
} | |
@SuppressWarnings("unchecked") | |
// <editor-fold defaultstate="collapsed" desc="Generated Code"> | |
private void initComponents() { | |
fileChooser = new javax.swing.JFileChooser(); | |
jButton1 = new javax.swing.JButton(); | |
filename = new javax.swing.JLabel(); | |
sum = new javax.swing.JLabel(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
getContentPane().setLayout(new java.awt.GridLayout(0, 1)); | |
jButton1.setText("Otwórz"); | |
jButton1.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
jButton1ActionPerformed(evt); | |
} | |
}); | |
getContentPane().add(jButton1); | |
getContentPane().add(filename); | |
getContentPane().add(sum); | |
pack(); | |
}// </editor-fold> | |
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { | |
if (JFileChooser.APPROVE_OPTION == fileChooser.showOpenDialog(this)) { | |
File file = fileChooser.getSelectedFile(); | |
try { | |
MessageDigest digest = MessageDigest.getInstance("MD5"); | |
InputStream is = new DigestInputStream(new FileInputStream(file), digest); | |
byte[] b = new byte[128]; | |
while (is.read(b) != -1) { | |
} | |
is.close(); | |
BigInteger bigInt = new BigInteger(1, digest.digest()); | |
String hashtext = bigInt.toString(16); | |
filename.setText(file.getName()); | |
sum.setText(hashtext); | |
} catch (Exception ex) { | |
System.out.println(ex); | |
} | |
} | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String args[]) { | |
/* Set the Nimbus look and feel */ | |
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> | |
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. | |
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html | |
*/ | |
try { | |
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { | |
if ("Nimbus".equals(info.getName())) { | |
javax.swing.UIManager.setLookAndFeel(info.getClassName()); | |
break; | |
} | |
} | |
} catch (ClassNotFoundException ex) { | |
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (InstantiationException ex) { | |
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (IllegalAccessException ex) { | |
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} catch (javax.swing.UnsupportedLookAndFeelException ex) { | |
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); | |
} | |
//</editor-fold> | |
/* Create and display the form */ | |
java.awt.EventQueue.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
new Main().setVisible(true); | |
} | |
}); | |
} | |
// Variables declaration - do not modify | |
private javax.swing.JFileChooser fileChooser; | |
private javax.swing.JLabel filename; | |
private javax.swing.JButton jButton1; | |
private javax.swing.JLabel sum; | |
// End of variables declaration | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment