Quantcast
Channel: Home Page http://blogs.dotnethell.it
Viewing all articles
Browse latest Browse all 24464

JAVA - GENERARE HASH MD5/SHA DI UN FILE (Codice)

$
0
0

JAVA - GENERARE HASH MD5/SHA DI UN FILE (Codice)


 

Oggi vi voglio mettere a disposizione la possibilità di calcolare Hash di un file con l'algoritmo MD5/SHA .Potete verificare se due file sono uguali e quindi hanno lo stesso Hash oppure sono diversi.. 

Come potete usare la libreria :

       CODICE JAVA - HashFile.java :
public static void main(String[] args) {
// TODO Auto-generated method stub
HashFile hashFile = new HashFile("Originale.txt","SHA");
HashFile hashFile1 = new HashFile("Originale.txt","MD5");
System.out.println(hashFile.equals(hashFile1)); //False

// Risposta della funzione equals()
// false // SHA: 44d3a83db52a0cf0f908ad0ef55c83f4771f66ad // MD5: ed0a96e83ab7b0910fcbcc131b2e6b82

System.out.println("SHA: " + hashFile.HashFileGenera());
System.out.println("MD5: " + hashFile1.HashFileGenera());
System.out.println(hashFile.toString());

}
       CODICE JAVA - HashFile.java :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class HashFile {

private File file;
private String Algoritmo;
public HashFile(String filename,String Algoritmo){
this.file = new File(filename);
this.Algoritmo = Algoritmo;
}
public String HashFileGenera() {
String output;
try{
MessageDigest digest = MessageDigest.getInstance(this.Algoritmo);
InputStream is = new FileInputStream(file);                
byte[] buffer = new byte[8192];
int read = 0;
try {
    while( (read = is.read(buffer)) > 0) {
        digest.update(buffer, 0, read);
    }       
    byte[] md5sum = digest.digest();
    BigInteger bigInt = new BigInteger(1, md5sum);
    output = bigInt.toString(16);
//   System.out.println("MD5: " + output);
    return output;
}
catch(IOException e) {
    throw new RuntimeException("Unable to process file for "+this.Algoritmo, e);
}
finally {
    try {
        is.close();
    }
    catch(IOException e) {
        throw new RuntimeException("Unable to close input stream for "+this.Algoritmo+" calculation", e);
    }
}       
 }catch(NoSuchAlgorithmException | FileNotFoundException e){
      e.printStackTrace();
     }
return Algoritmo;
}
public String toString(){
return (
"" + this.Algoritmo + " : " + this.HashFileGenera()
);
}

public boolean equals(Object o){
HashFile p = (HashFile) o;
return ( this.HashFileGenera().equals(p.HashFileGenera())
);
}

}
Potete scaricare il file d'esempio :

Password :  "ImaginSystem" 
Link File Download : Download

By ImaginSystems & Queen Gin   

Viewing all articles
Browse latest Browse all 24464

Trending Articles


“BAHAY KUBO HUGOT”


FORTUITOUS EVENT


Pokemon para colorear


Girasoles para colorear


OFW quotes : Pinoy Tagalog Quotes


“Mr bolero banat lines”


Re:Mutton Pies (lleechef)


Ka longiing longsem kaba skhem bad kaba khlain ka pynlong kein ia ka...


Vimeo Create - Video Maker & Editor 1.5.2 by Vimeo Inc


Vimeo 10.7.1 by Vimeo.com, Inc.


UPDATE SC IDOL: TWO BECOME ONE


KASAMBAHAY BILL IN THE HOUSE


Pocoyo para colorear


Sapos para colorear


Patama Quotes – Tanga love tagalog quotes


“Hanggang s apag tanda natin”


RE: Mutton Pies (frankie241)


Hato lada ym dei namar ka jingpyrshah jong U JJM Nichols Roy (Bah Joy) ngin...


Vimeo 10.7.0 by Vimeo.com, Inc.


Vimeo 11.6.0 by Vimeo.com, Inc.