1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
public class Checksum extends java.applet.Applet {
public void init (){
Frame parent = new Frame();
FileDialog fd = new FileDialog(parent, "Please choose a file",FileDialog.LOAD);
fd.show();
String selectedItem = fd.getFile();
if (selectedItem == null) {
// no file selected
} else {
File ffile = new File( fd.getDirectory() + File.separator +
fd.getFile());
try {
RandomAccessFile fic = new RandomAccessFile(ffile, "r");
StringBuffer chaine = new StringBuffer();
String temp = "";
String contents = loadFile(ffile);
while ((temp = fic.readLine())!= null)
chaine.append(temp);
fic.close();
System.out.println(Checksum.md5(contents));
} catch (IOException fnf) {
System.out.println("Fichier non trouvé");
fnf.printStackTrace();
}
}
}
public static String loadFile(File f) {
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
StringWriter out = new StringWriter();
int b;
while ((b=in.read()) != -1)
out.write(b);
out.flush();
out.close();
in.close();
return out.toString();
}
catch (IOException ie)
{
ie.printStackTrace();
return null;
}
}
public static String hex(byte[] array) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1,3));
}
return sb.toString();
}
public static String md5 (String message) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return hex (md.digest(message.getBytes("CP1252")));
} catch (NoSuchAlgorithmException e) {
} catch (UnsupportedEncodingException e) {
}
return null;
}
} |
Partager