Skip to content

Instantly share code, notes, and snippets.

@rafaelchagasb
Created September 9, 2016 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelchagasb/1967ccbf1ca0ff12093eceba172f51b3 to your computer and use it in GitHub Desktop.
Save rafaelchagasb/1967ccbf1ca0ff12093eceba172f51b3 to your computer and use it in GitHub Desktop.
Gerar hash arquivo
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