Java Code Examples for org.apache.commons.codec.digest.Md5Crypt#apr1Crypt()

The following examples show how to use org.apache.commons.codec.digest.Md5Crypt#apr1Crypt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: APR1.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
@Override
public String hash(HashString hashString, String password) {
  final String apr1Salt = "$apr1$" + hashString.salt();
  String res = Md5Crypt.apr1Crypt(password, apr1Salt);
  // we need to exclude the salt part
  return res.substring(apr1Salt.length() + 1);
}
 
Example 2
Source File: PasswordEncrypt.java    From sftpserver with Apache License 2.0 5 votes vote down vote up
public PasswordEncrypt(final String key) {
	final byte[] keyBytes = key.getBytes(US_ASCII);
	this.md5 = Md5Crypt.md5Crypt(keyBytes.clone());
	this.apr1 = Md5Crypt.apr1Crypt(keyBytes.clone());
	this.sha256 = Sha2Crypt.sha256Crypt(keyBytes.clone());
	this.sha512 = Sha2Crypt.sha512Crypt(keyBytes.clone());
	Arrays.fill(keyBytes, (byte) 0);
}
 
Example 3
Source File: PasswordEncrypt.java    From sftpserver with Apache License 2.0 5 votes vote down vote up
public static boolean checkPassword(final String crypted, final String key) {
	String crypted2 = null;
	if (crypted == null)
		return false;
	if (crypted.length() < 24)
		return false;
	if (crypted.charAt(0) != '$')
		return false;
	final int offset2ndDolar = crypted.indexOf('$', 1);
	if (offset2ndDolar < 0)
		return false;
	final int offset3ndDolar = crypted.indexOf('$', offset2ndDolar + 1);
	if (offset3ndDolar < 0)
		return false;
	final String salt = crypted.substring(0, offset3ndDolar + 1);
	final byte[] keyBytes = key.getBytes(US_ASCII);
	if (crypted.startsWith("$1$")) { // MD5
		crypted2 = Md5Crypt.md5Crypt(keyBytes.clone(), salt);
	} else if (crypted.startsWith("$apr1$")) { // APR1
		crypted2 = Md5Crypt.apr1Crypt(keyBytes.clone(), salt);
	} else if (crypted.startsWith("$5$")) { // SHA2-256
		crypted2 = Sha2Crypt.sha256Crypt(keyBytes.clone(), salt);
	} else if (crypted.startsWith("$6$")) { // SHA2-512
		crypted2 = Sha2Crypt.sha512Crypt(keyBytes.clone(), salt);
	}
	Arrays.fill(keyBytes, (byte) 0);
	if (crypted2 == null)
		return false;
	return crypted.equals(crypted2);
}