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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
public class vxchecksum {
public static void main(String[] args) throws FileNotFoundException {
String[] opts = args.clone();
if (opts.length == 0) {
System.err.println("usage: cheksum.jar [-w] <filename>\n");
System.err.println("-w:\twrite the output to \"SHA256\"");
System.exit(-1);
}
else {
File f = new File("D:\\lib.sha256"); //fichier contenant les hash
Scanner sc = new Scanner(f);
//List<String> files = new ArrayList<String>();
while(sc.hasNextLine()) {
String line = sc.nextLine();
String[] content = line.split("\\*");
String hash = content[0];
String file = content[1];
//System.out.println(hash);
//System.out.println(file);
try {
MessageDigest md;
md = MessageDigest.getInstance("SHA-256");
//Create checksum for this file
File file1 = new File("D:\\dev\\checksum\\lib\\" + file);
//Get checksum
String checksum = getFileChecksum(md, file1);
//affichage du checksum calculé
//System.out.println(file + ":" + checksum);
//compare checksum
if (checksum.equals(hash)) {
System.out.println("No problem detected for file " + file);
}else {
System.out.println("problem detected for file " + file + ". Checksum mismatch");
System.out.println("hash attendu: " +hash);
System.out.println("hash calculé: " +checksum) ;
System.out.println("\n") ;
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static String getFileChecksum(MessageDigest digest, File file) throws FileNotFoundException {
// TODO Auto-generated method stub
//fichier
FileInputStream fis = new FileInputStream(file);
byte[] byteArray = new byte[1024];
int bytesCount = 0;
//lecture du fichier
try {
while ((bytesCount = fis.read(byteArray)) != -1) {
digest.update(byteArray, 0, bytesCount);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
//fermeture du stream
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bytes = digest.digest();
StringBuilder sb = new StringBuilder();
for(int i=0; i< bytes.length ;i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
//return du hash
return sb.toString();
}
} |