import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; // // This is Java Swing program to select one file and then // compute a 160 bit hash code (SHA1) of the contents. // // Product of PlumByte.com - you are welcome to rewrite this // source and then make it your own. If you are just going to make // a few changes please retain this comment and add your own. // public class FindSHA1 extends JFrame { Box box = new Box(BoxLayout.Y_AXIS); private JTextField jt_hash = new JTextField(40); private JFileChoice jfc; private SHA1 sha1 = new SHA1(); public FindSHA1() { box.add(new JLabel(" ")); box.add(new JLabel("SHA1 Hash Value of File:")); box.add(new JLabel(" ")); box.add(jt_hash); box.add(new JLabel(" ")); box.add(new JLabel(" ")); jfc = new JFileChoice(this); box.add(jfc); getContentPane().add(box); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); show(); } public void run(File f) { jt_hash.setText(sha1.compute(f)); } public static void main(String[] args) { FindSHA1 sw = new FindSHA1(); } } ////////////////////////////////////////////////////////////////// class JFileChoice extends JFileChooser { private FindSHA1 findsha1; public JFileChoice(FindSHA1 fs) { findsha1 = fs; } public void approveSelection() { findsha1.run(getSelectedFile()); } public void cancelSelection() { System.exit(0); } } ////////////////////////////////////////////////////////////////// class SHA1 { private int w[] = new int[80]; private BufferedInputStream bin; private int h0,h1,h2,h3,h4; private int a,b,c,d,e; private int k_table[] = new int[80]; private boolean end_of_file = false; private boolean more_blocks = true; private byte buffer[] = new byte[64]; private long nbytes = 0; public SHA1() { int i = 0; h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE; h3 = 0x10325476; h4 = 0xC3D2E1F0; while(i<20) k_table[i++] = 0x5A827999; while(i<40) k_table[i++] = 0x6ED9EBA1; while(i<60) k_table[i++] = 0x8F1BBCDC; while(i<80) k_table[i++] = 0xCA62C1D6; } public long length() { return nbytes; } public String compute(File f) { nbytes = 0; end_of_file = false; more_blocks = true; try { this.bin = new BufferedInputStream(new FileInputStream(f)); while(this.more_blocks) { this.get_block(); this.step(); } this.bin.close(); return this.string(); } catch (IOException e) { return "00000000"+"00000000"+"00000000"+"00000000"; } } private void get_block() throws IOException { int n; if(!end_of_file) { n = bin.read(buffer); if(n<0) n = 0; nbytes += n; if(n<64) { // Usually n is 64. end_of_file = true; buffer[n++] = (byte)0x80; while(n<56) buffer[n++] = 0; if(n==56) { place_last_8_bytes(); more_blocks = false; } else { while(n<64) buffer[n++] = 0; } }; } else { more_blocks = false; for(n=0;n<56;n++) buffer[n] = 0; place_last_8_bytes(); } } private void place_last_8_bytes() { long nbits = nbytes * 8; int n = 56; int pos = 56; while(n<64) { buffer[n++] = (byte)((nbits >> pos) & 0xFF); pos -= 8; } } private void step() { int t, temp; a = h0; b = h1; c = h2; d = h3; e = h4; fill_w(); for(t=0;t<80;t++) { temp = (a<<5) + (a>>>27) + f(t,b,c,d) + e + w[t] + k_table[t]; e = d; d = c; c = (b<<30) + (b>>>2); b = a; a = temp; } h0 = h0 + a; h1 = h1 + b; h2 = h2 + c; h3 = h3 + d; h4 = h4 + e; } private void fill_w() { int i,k; int bix = 0; // buffer index. for(i=0;i<16;i++) { k = 0; k = (k<<8) ^ (buffer[bix++] & 0xFF); // 4 reps to fill 32 bits. k = (k<<8) ^ (buffer[bix++] & 0xFF); k = (k<<8) ^ (buffer[bix++] & 0xFF); k = (k<<8) ^ (buffer[bix++] & 0xFF); w[i] = k; } for(i=16;i<80;i++) { k = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]; w[i] = (k>>>31) ^ (k<<1); } } private int f(int t,int b,int c,int d) { if(t<40) { if(t<20) { return (b&c) | (~b&d); } else { return b ^ c ^ d; } } else { if(t<60) { return (b&c) | (b&d) | (c&d); } else { return b ^ c ^ d; } } } // // Convert the 160 bits of h0,..,h4 to base 32. // private String string() { char c[] = new char[32]; int h[] = new int[4]; long register = (((long)h0)<<32) ^ (h1 & 0x00000000FFFFFFFFL); int rpos = 59; int hpos = 0; int i; // Note we don't care what is in h[3], but it will get read // once at the very end. h[0] = h2; h[1] = h3; h[2] = h4; for(i=0;i<32;i++) { c[i] = (char)((register >> rpos) & 0x1F); rpos -= 5; if(rpos<0) { rpos += 32; register = (register<<32) ^ (h[hpos++] & 0x00000000FFFFFFFFL); } c[i] += (c[i]<26)? 'A' : ('2'-26); } return new String(c); } }