Menu

[r110]: / Openbase / src / at / it_open / openbase / security / FileHash.java  Maximize  Restore  History

Download this file

45 lines (36 with data), 1.2 kB

 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package at.it_open.openbase.security;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* @author roland
*/
public class FileHash {
public static String getHash(String file) throws IOException {
StringBuffer hexString = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
fis.close();
byte[] mdbytes = md.digest();
hexString = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
hexString.append(Integer.toHexString((0xFF & mdbytes[i])));
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
}
return "";
}
}