Android file MD5/SHA1/SHA256 checksum

    Generally, file integrity and correctness verification will be attached to the file download requirements to avoid the downloaded file being incomplete or being hijacked and injected by a third party. It seems that many of them use MD5 verification, but some also use sha1 or sha256 verification. In the two recent projects, I just encountered md5 and sha256 verification, followed the MessageDigest source code, extracted the tool class for future use, and contributed it here. If there is any incorrect place, please correct me.

    1. Use enumeration to define the types that need to be verified, shield directly incoming unsupported verification methods, and increase the fault tolerance rate:

public enum TypeEnum {
        MD5, SHA1, SHA256
    }

    2. Get the md5/sha1/sha256 of the specified file

private static String getFileSignature(File file, TypeEnum typeEnum) {
        MessageDigest digest;
        String type = "";
        switch (typeEnum) {
            case MD5:
                type = "MD5";
                break;
            case SHA1:
                type = "SHA-1";
                break;
            case SHA256:
                type = "SHA-256";
                break;
        }
        if (TextUtils.isEmpty(type)) {
            LogUtil.e(Constants.LOG_TAG, "type undefined");
            return null;
        }
        try {
            digest = MessageDigest.getInstance(type);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace ();
            return null;
        }

        InputStream is;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
            LogUtil.e(TAG, "Exception while getting FileInputStream");
            return null;
        }

        byte[] buffer = new byte[8192];
        int read;
        try {
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            String output = bigInt.toString(16);
            output = String.format("%32s", output).replace(' ', '0');
            return output;
        } catch (IOException e) {
            e.printStackTrace ();
            LogUtil.e(TAG,"Unable to process file for ");
            return null;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace ();
                LogUtil.e(TAG, "Exception on closing inputstream:" );
            }
        }
    }

3. Compare with the file md5/sha1/sha256 given by the server

public static boolean validateFile(TypeEnum typeEnum, String standardStr, File fileToCheck) {
        if (TextUtils.isEmpty(standardStr) || fileToCheck == null) {
            LogUtil.e(TAG, "MD5 string empty or updateFile null");
            return false;
        }

        String calculatedDigest = getFileSignature(fileToCheck, typeEnum);

        if (TextUtils.isEmpty(calculatedDigest)) {
            LogUtil.d(TAG, "calculatedDigest null");
            return false;
        }
        return calculatedDigest.equalsIgnoreCase(standardStr);
    }

The full code is as follows:

public class FileValidateUtil {

    private static final String TAG = Constants.LOG_TAG;

    public enum TypeEnum {
        MD5, SHA1, SHA256
    }

    /**
     * @param typeEnum
     * @param standardStr
     * @param fileToCheck
     * @return
     */
    public static boolean validateFile(TypeEnum typeEnum, String standardStr, File fileToCheck) {
        if (TextUtils.isEmpty(standardStr) || fileToCheck == null) {
            LogUtil.e(TAG, "MD5 string empty or updateFile null");
            return false;
        }

        String calculatedDigest = getFileSignature(fileToCheck, typeEnum);

        if (TextUtils.isEmpty(calculatedDigest)) {
            LogUtil.d(TAG, "calculatedDigest null");
            return false;
        }
        return calculatedDigest.equalsIgnoreCase(standardStr);
    }

    private static String getFileSignature(File file, TypeEnum typeEnum) {
        MessageDigest digest;
        String type = "";
        switch (typeEnum) {
            case MD5:
                type = "MD5";
                break;
            case SHA1:
                type = "SHA-1";
                break;
            case SHA256:
                type = "SHA-256";
                break;
        }
        if (TextUtils.isEmpty(type)) {
            LogUtil.e(Constants.LOG_TAG, "type undefined");
            return null;
        }
        try {
            digest = MessageDigest.getInstance(type);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace ();
            return null;
        }

        InputStream is;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
            LogUtil.e(TAG, "Exception while getting FileInputStream");
            return null;
        }

        byte[] buffer = new byte[8192];
        int read;
        try {
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            String output = bigInt.toString(16);
            output = String.format("%32s", output).replace(' ', '0');
            return output;
        } catch (IOException e) {
            e.printStackTrace ();
            LogUtil.e(TAG,"Unable to process file for ");
            return null;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace ();
                LogUtil.e(TAG, "Exception on closing inputstream:" );
            }
        }
    }

}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855032&siteId=291194637
Recommended