How to generate an MD5 checksum for a file in Android?
Convert the file content into string & use the below method:
public static String getMD5EncryptedString(String encTarget){
MessageDigest mdEnc = null;
try {
mdEnc = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
System.out.println("Exception while encrypting to md5");
e.printStackTrace();
} // Encryption algorithm
mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
while ( md5.length() < 32 ) {
md5 = "0"+md5;
}
return md5;
}
Note that this simple approach is suitable for smallish strings, but will not be efficient for large files. For the latter, see dentex's answer.
How to get a file directory's MD5 checksum in Android/Java
As far as I know there isn't an efficient way to get the checksum of a directory.
md5 checksum on file in android doesn't match the md5 after i email it to myself
Found this answer here: How to generate an MD5 checksum for a file in Android?
public static String fileToMD5(String filePath) {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
MessageDigest digest = MessageDigest.getInstance("MD5");
int numRead = 0;
while (numRead != -1) {
numRead = inputStream.read(buffer);
if (numRead > 0)
digest.update(buffer, 0, numRead);
}
byte [] md5Bytes = digest.digest();
return convertHashToString(md5Bytes);
} catch (Exception e) {
return null;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) { }
}
}
}
private static String convertHashToString(byte[] md5Bytes) {
String returnVal = "";
for (int i = 0; i < md5Bytes.length; i++) {
returnVal += Integer.toString(( md5Bytes[i] & 0xff ) + 0x100, 16).substring(1);
}
return returnVal;
}
Getting a File's MD5 Checksum in Java
There's an input stream decorator, java.security.DigestInputStream
, so that you can compute the digest while using the input stream as you normally would, instead of having to make an extra pass over the data.
MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = Files.newInputStream(Paths.get("file.txt"));
DigestInputStream dis = new DigestInputStream(is, md))
{
/* Read decorated stream (dis) to EOF as normal... */
}
byte[] digest = md.digest();
A Simple MD5 Checker Android Apps
MD5 Hash function:
public static final String md5(final String s)
{
try
{
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++)
{
String h = Integer.toHexString(0xFF & messageDigest[i]);
while (h.length() < 2)
h = "0" + h;
hexString.append(h);
}
return hexString.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return "";
}
Getting a different MD5 check sum for the same file which is saved in different folder
Yes MD5 checkSum
of a file depends on the location
where you saved it
.
Related Topics
Drawerlayout Double Drawer (Left and Right Drawers Simultaneously)
How to Make My Android App Appear in the Share List of Another Specific App
Android Setonclicklistener Method - How Does It Work
Create and Share a File from Internal Storage
Android Get Current Locale, Not Default
Programmatically Collapse or Expand Collapsingtoolbarlayout
Sha1 Key for Debug & Release Android Studio MAC , How to Generate Sha1 Release Keys in MAC
Android Studio Not Deploying Changes to App
Android Maps API V2 with Custom Markers
Visual Studio (2015) Emulator for Android Not Working - Xde.Exe - Exit Code 3
How to Save Image in Android Gallery
How to Downgrade Android Emulator
Best Practice for Nested Fragments in Android 4.0, 4.1 (<4.2) Without Using the Support Library
How to Make a View with Rounded Corners
Download Files and Store Them Locally with Phonegap/Jquery Mobile Android and iOS Apps