Created
September 9, 2016 00:57
-
-
Save rafaelchagasb/1967ccbf1ca0ff12093eceba172f51b3 to your computer and use it in GitHub Desktop.
Gerar hash arquivo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.security.MessageDigest; | |
public class Main { | |
public static void main(String[] args)throws Exception | |
{ | |
MessageDigest md = MessageDigest.getInstance("SHA-512"); | |
FileInputStream fis = new FileInputStream("/home/rafael/Downloads/ACcompactado.zip"); | |
byte[] dataBytes = new byte[1024]; | |
int nread = 0; | |
while ((nread = fis.read(dataBytes)) != -1) { | |
md.update(dataBytes, 0, nread); | |
}; | |
byte[] mdbytes = md.digest(); | |
//convert the byte to hex format method 1 | |
StringBuffer sb = new StringBuffer(); | |
for (int i = 0; i < mdbytes.length; i++) { | |
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); | |
} | |
System.out.println("Digest(in hex format):: " + sb.toString()); | |
//convert the byte to hex format method 2 | |
StringBuffer hexString = new StringBuffer(); | |
for (int i=0;i<mdbytes.length;i++) { | |
String hex=Integer.toHexString(0xff & mdbytes[i]); | |
if(hex.length()==1) hexString.append('0'); | |
hexString.append(hex); | |
} | |
System.out.println("Digest(in hex format):: " + hexString.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment