private static boolean validateToken ( String originalToken , String storedToken ) throws NoSuchAlgorithmException , InvalidKeySpecException { String [ ] parts = storedToken . split ( ":" ) ; int iterations = Integer . parseInt ( parts [ 0 ] ) ; byte [ ] salt = fromHex ( parts [ 1 ] ) ; byte [ ] hash = fromHex ( parts [ 2 ] ) ; PBEKeySpec spec = new PBEKeySpec ( originalToken . toCharArray ( ) , salt , iterations , hash . length * 8 ) ; SecretKeyFactory skf = SecretKeyFactory . getInstance ( "PBKDF2WithHmacSHA1" ) ; byte [ ] testHash = skf . generateSecret ( spec ) . getEncoded ( ) ; int diff = hash . length ^ testHash . length ; for ( int i = 0 ; i < hash . length && i < testHash . length ; i ++ ) { diff |= hash [ i ] ^ testHash [ i ] ; } return diff == 0 ; }
Implementation of PBKDF2WithHmacSHA1