01: /*
02: * SalomeTMF is a Test Management Framework
03: * Copyright (C) 2005 France Telecom R&D
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * @author Aurore PENAULT
20: *
21: * Contact: mikael.marche@rd.francetelecom.com
22: */
23: package salomeTMF_plug.docXML.importxml;
24:
25: import java.io.File;
26: import java.io.FileInputStream;
27: import java.io.FileNotFoundException;
28: import java.security.MessageDigest;
29:
30: /**
31: *
32: * @author Aurore PENAULT
33: * @since JDK5.0
34: */
35: public class Digest {
36:
37: private static final int SIZE = 16384;
38: private static final String base = "0123456789abcdef";
39:
40: /**
41: * Compute the MD5 digest of a file from a FileInputStream
42: * @param fis a FileInputStream
43: * @return MD5 as a String
44: */
45: public static String runMD5(FileInputStream fis) {
46: try {
47: MessageDigest sha1 = MessageDigest.getInstance("MD5");
48: byte buffer[] = new byte[SIZE];
49: while (true) {
50: int l = fis.read(buffer);
51: if (l == -1)
52: break;
53: sha1.update(buffer, 0, l);
54: }
55: fis.close();
56: byte digest[] = sha1.digest();
57: StringBuffer buf = new StringBuffer();
58: for (int i = 0; i < digest.length; i++) {
59: buf.append(base.charAt((digest[i] >> 4) & 0xf));
60: buf.append(base.charAt(digest[i] & 0xf));
61: }
62: String md5 = buf.toString();
63: ;
64: return md5;
65: } catch (Exception e) {
66: e.printStackTrace();
67: return null;
68: }
69: }
70:
71: /**
72: * Compute the MD5 digest of a file from a FileInputStream
73: * @param fis a FileInputStream
74: * @return MD5 as a String
75: * @throws FileNotFoundException if the file does not exist,
76: * is a directory rather than a regular file, or for some other
77: * reason cannot be opened for reading.
78: */
79: public static String runMD5(File f) throws FileNotFoundException {
80: FileInputStream fis = new FileInputStream(f);
81: return Digest.runMD5(fis);
82: }
83:
84: }
|