Java tutorial
//package com.java2s; import android.util.Log; import java.util.Arrays; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.SecretKey; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; public class Main { private static final String LOG_TAG = "MarvinMessaging"; private static final String KEY_FACTORY = "PBEWithSHA256And128BitAES-CBC-BC"; private static final int ITERATION_COUNT = 50; public static boolean checkPassword(char[] plaintextPassword, String passwordHash, String passwordSalt) { byte[] calculatedHash = null; byte[] savedSalt = hexStringToBytes(passwordSalt); byte[] savedHash = hexStringToBytes(passwordHash); if (savedHash.length == 0) return true; //if they are both empty this will fall through (first time, no set pass) try { calculatedHash = generateHash(plaintextPassword, savedSalt); } catch (Exception e) { Log.d(LOG_TAG, "checkPassword", e); return false; } if (calculatedHash != null) { if (Arrays.equals(calculatedHash, savedHash)) return true; else return false; } return false; } public static byte[] hexStringToBytes(String hex) { byte[] bytes = new byte[hex.length() / 2]; int j = 0; for (int i = 0; i < hex.length(); i += 2) { try { String hexByte = hex.substring(i, i + 2); Integer I = new Integer(0); I = Integer.decode("0x" + hexByte); int k = I.intValue(); bytes[j++] = new Integer(k).byteValue(); } catch (NumberFormatException e) { Log.d(LOG_TAG, "hexStringToBytes", e); return bytes; } catch (StringIndexOutOfBoundsException e) { Log.d(LOG_TAG, "hexStringToBytes", e); return bytes; } } return bytes; } public static byte[] generateHash(char[] pass, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { PBEKeySpec keySpec = new PBEKeySpec(pass, salt, ITERATION_COUNT, 128); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_FACTORY); SecretKey key = keyFactory.generateSecret(keySpec); return key.getEncoded(); } }