Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Hash - Hash.java

Hash.java

Caricato da: Netarrow
Scarica il programma completo

  1. import java.security.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class Hash {
  6.  
  7. public static byte[] calcolaHash(String file) throws Exception {
  8. System.out.print("Inizio calcolo hash");
  9. System.out.print(".");
  10. MessageDigest md = MessageDigest.getInstance("MD5");
  11. System.out.print(".");
  12. DigestInputStream digestIn = new DigestInputStream(bis, md);
  13. System.out.print(".");
  14. while(digestIn.read() != -1);
  15. byte[] digest = md.digest();
  16. System.out.println(". Calcolo terminato!!");
  17. return digest;
  18. }
  19.  
  20. public static void verificaHash(String fileIn, String fileHash) throws Exception {
  21. byte[] digest = calcolaHash(fileIn);
  22. FileInputStream fis = new FileInputStream(fileHash);
  23. int b = 0;
  24. while((b = fis.read()) != -1) {
  25. baos.write(b);
  26. }
  27. byte[] hashSaved = baos.toByteArray();
  28. if(Arrays.equals(digest, hashSaved)) {
  29. System.out.println("L'impronta combacia");
  30. } else {
  31. System.out.println("L'impronta non combacia");
  32. }
  33. }
  34.  
  35. public static void main(String args[]) throws Exception {
  36. if(args.length < 2 || (args[0].equals("-v") && args.length != 3)) {
  37. System.out.println("Usage: Hash -c || -v fileInput [se -v] fileImpronta\n" +
  38.                     "opzioni: -c calcola l'hash e lo salva in fileOutput\n" +
  39.                     "opzioni: -v verifica l'hash di fileOutput con fileImpronta\n" +
  40.                     "ad esempio: Hash -c file.txt\n" +
  41.                     "            Hash -v file.txt hashFile.txt\n");
  42. System.exit(1);
  43. }
  44. if(args[0].equals("-c")) {
  45. FileOutputStream fos = new FileOutputStream("hash" + args[1]);
  46. fos.write(calcolaHash(args[1]));
  47. fos.close();
  48. } else if(args[0].equals("-v")) {
  49. verificaHash(args[1], args[2]);
  50. }
  51. }
  52.  
  53. }