Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Creative commons Attribution-NonCommercial license. NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
 package base.text;
Utility functions related to string management.
public class Password {

Return a salted SHA-256 hashed version of a password, in the format salt:passwordhash.

		String salt = StringHelper.randomString(16);
		MessageDigest md = MessageDigest.getInstance("SHA-256");
		md.update((salt + password).getBytes("UTF-8"));
		return salt + ":" + Base64Encode.encode(md.digest());

Verify a password matches its password hash. A colon in the password indicates the password hash has a salt.

	public static boolean verifyPassword(String passwordHashString passwordthrows NoSuchAlgorithmExceptionUnsupportedEncodingException {
		if(passwordHash.contains(":")) {
			String[] part = passwordHash.split(":");
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			md.update((part[0] + password).getBytes("UTF-8"));
			return Base64Encode.encode(md.digest()).equals(part[1]);
else {
			return StringHelper.shaHash(password).equals(passwordHash);
New to GrepCode? Check out our FAQ X