Android MD5 password encryption

less than 1 minute read

Today I will show you how to generate the MD5 hash String for a given String in Android

/**
 * function md5 encryption for passwords
 * 
 * @param password
 * @return passwordEncrypted
 */
private static final String md5(final String password) {
	try {

		MessageDigest digest = java.security.MessageDigest
				.getInstance("MD5");
		digest.update(password.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 "";
}

Comments