Skip to content

Instantly share code, notes, and snippets.

@erfanegtfi
Created April 13, 2018 12:37
Show Gist options
  • Select an option

  • Save erfanegtfi/a62707696aa9d10af0b187c37670385d to your computer and use it in GitHub Desktop.

Select an option

Save erfanegtfi/a62707696aa9d10af0b187c37670385d to your computer and use it in GitHub Desktop.
Hash String md5, sha1
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Hash {
public static String getHash(String txt, String hashType) {
try {
byte[] array = MessageDigest.getInstance(hashType).digest(txt.getBytes());
StringBuffer sb = new StringBuffer();
for (byte b : array) {
sb.append(Integer.toHexString((b & MotionEventCompat.ACTION_MASK) | AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY).substring(1, 3));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
return null;
}
}
public static String md5(String txt) {
return getHash(txt, "MD5");
}
public static String sha1(String txt) {
return getHash(txt, "SHA1");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment