1. File
public String getFileMD5(File file)
{
//Create checksum for this file
//File file = new File("c:/temp/testOut.txt");
//Use MD5 algorithm
MessageDigest md5Digest = null;
try
{
md5Digest = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
//Get the checksum
String checksum = null;
try
{
checksum = this.getFileChecksum(md5Digest, file);
}
catch (IOException e)
{
e.printStackTrace();
}
//see checksum
//System.out.println(checksum);
return checksum;
}
private static String getFileChecksum(MessageDigest digest, File file) throws IOException
{
//Get file input stream for reading the file content
FileInputStream fis = new FileInputStream(file);
//Create byte array to read data in chunks
byte[] byteArray = new byte[1024];
int bytesCount = 0;
//Read file data and update in message digest
while ((bytesCount = fis.read(byteArray)) != -1)
{
digest.update(byteArray, 0, bytesCount);
}
//close the stream; We don't need it now.
fis.close();
//Get the hash's bytes
byte[] bytes = digest.digest();
//This bytes[] has bytes in decimal format;
//Convert it to hexadecimal format
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
//return complete hash
return sb.toString();
}
2.String
public String getStringMD5(String str)
{
//Use MD5 algorithm
MessageDigest md5Digest = null;
try
{
md5Digest = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
//Get the checksum
String checksum = null;
try
{
checksum = this.getStringChecksum(md5Digest, str);
}
catch (IOException e)
{
e.printStackTrace();
}
//see checksum
//System.out.println(checksum);
return checksum;
}
private static String getStringChecksum(MessageDigest digest, String str) throws IOException
{
//Create byte array to read data in chunks
byte[] byteArray = str.getBytes("utf-8");
int bytesCount = byteArray.length ;
//Read file data and update in message digest
digest.update(byteArray, 0, bytesCount);
//Get the hash's bytes
byte[] bytes = digest.digest();
//This bytes[] has bytes in decimal format;
//Convert it to hexadecimal format
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
//return complete hash
return sb.toString();
}
3.InputStream
public String getInputStreamMD5(InputStream inputStream)
{
//Create checksum for this file
// File file = new File("c:/temp/testOut.txt");
// try
// {
// InputStream inputStream = new FileInputStream(file);
// }
// catch (FileNotFoundException e)
// {
// e.printStackTrace();
// }
//Use MD5 algorithm
MessageDigest md5Digest = null;
try
{
md5Digest = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
//Get the checksum
String checksum = null;
try
{
checksum = this.getInputStreamChecksum(md5Digest, inputStream);
}
catch (IOException e)
{
e.printStackTrace();
}
//see checksum
//System.out.println(checksum);
return checksum;
}
private static String getInputStreamChecksum(MessageDigest digest, InputStream inputStream) throws IOException
{
InputStream fis = inputStream;
//Create byte array to read data in chunks
byte[] byteArray = new byte[1024];
int bytesCount = 0;
//Read file data and update in message digest
while ((bytesCount = fis.read(byteArray)) != -1)
{
digest.update(byteArray, 0, bytesCount);
}
//close the stream; We don't need it now.
fis.close();
//Get the hash's bytes
byte[] bytes = digest.digest();
//This bytes[] has bytes in decimal format;
//Convert it to hexadecimal format
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
//return complete hash
return sb.toString();
}
4.Resource
public String getResourceMD5(Resource resource)
{
//Create checksum for this file
// File file = new File("c:/temp/testOut.txt");
// Resource resource = new FileSystemResource(file);
//Use MD5 algorithm
MessageDigest md5Digest = null;
try
{
md5Digest = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
//Get the checksum
String checksum = null;
try
{
checksum = this.getResourceChecksum(md5Digest, resource);
}
catch (IOException e)
{
e.printStackTrace();
}
//see checksum
//System.out.println(checksum);
return checksum;
}
private static String getResourceChecksum(MessageDigest digest, Resource resource) throws IOException
{
//Get inputstream from resource
InputStream fis = resource.getInputStream();
//Create byte array to read data in chunks
byte[] byteArray = new byte[1024];
int bytesCount = 0;
//Read file data and update in message digest
while ((bytesCount = fis.read(byteArray)) != -1)
{
digest.update(byteArray, 0, bytesCount);
}
//close the stream; We don't need it now.
fis.close();
//Get the hash's bytes
byte[] bytes = digest.digest();
//This bytes[] has bytes in decimal format;
//Convert it to hexadecimal format
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++)
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
//return complete hash
return sb.toString();
}