Skip to content
Stanislav Koshutsky edited this page Feb 15, 2017 · 4 revisions

The Hash class is intended to wrap hash (MD5 or SHA1) calculating methods. Hash could be calculated for string/text, byte-array, file or a stream. Also application SHA1 could be retrieved using getAppKeyHash() method. Methods checkMD5() and checkSHA() could be used to check if no file modifications has been made since the time you stored its hash. Methods isValidSHA1()/isValidMD5() could be used to check if given string is a valid hash string.

public static String getMD5(final byte[] bytes)  

Calculates MD5 hash of a given byte array.

public static String getMD5(final String s)  

Calculates MD5 hash of a given String. Aware of passing big strings to avoid OOM.

public static boolean checkMD5(final String md5Hash, final File file)  

Checks if given hash is the same for given file - helps to detect file changes and returns true if MD5 hash for given file is the same to given hash or false otherwise.

public static String getMD5(final File file)

Calculates MD5 hash of a given File

public static String getMD5(final InputStream inputStream)  

Calculates MD5 hash for a given stream. Warning - pointer will be moved to the end of stream so you have to reset stream or whatever you need to do after running this method.
Given stream will not be reset moving its pointer to a begin before calculation so you could calc hash of a part of a stream if you want, just pass stream pointed to a desired position before calling this method.
Given stream will not be closed after calculations.

public static String getMD5AndResetStream(final InputStream inputStream)  

Calculates MD5 hash for a given stream. Warning: stream will be reset after hash calc.
Given stream will not be reset moving its pointer to a begin before calculation so you could calc hash of a part of a stream if you want, just pass stream pointed to a desired position before calling this method.
Given stream will not be closed after calculations.

public static String getSHA(final byte[] bytes)  

Calculates SHA hash of a given byte array.

public static String getSHA(final String s)  

Calculates SHA hash of a given String. Aware of passing big strings to avoid OOM.

public static boolean checkSHA(final String shaHash, final File file)  

Checks if given hash is the same for given file - helps to detect file changes, returns true if SHA hash for given file is the same to given hash or false otherwise

public static String getSHA(final File file)  

Calculates SHA hash of a given File

public static String getSHA(final InputStream inputStream)  

Calculates SHA hash for a given stream. Warning - pointer will be moved to the end of stream so you have to reset stream or whatever you need to do after running this method.
Given stream will not be reset moving its pointer to a begin before calculation so you could calc hash of a part of a stream if you want, just pass stream pointed to a desired position before calling this method.
Given stream will not be closed after calculations.

public static String getSHAAndResetStream(final InputStream inputStream)  

Calculates SHA hash for a given stream. Warning: stream will be reset after hash calc.
Given stream will not be reset moving its pointer to a begin before calculation so you could calc hash of a part of a stream if you want, just pass stream pointed to a desired position before calling this method.
Given stream will not be closed after calculations.

public static String getAppKeyHash(final Context context, final String packageName)  

Returns application SHA hash - the cert fingerprint used to sign the app

public static boolean isValidSHA1(String s)  

Checks if given string is a valid SHA1 hash string using pattern

public static boolean isValidMD5(String s)   

Checks if given string is a valid MD5 hash string using pattern

Clone this wiki locally