Class PasswordUtil
java.lang.Object
org.apache.directory.api.ldap.model.password.PasswordUtil
A utility class containing methods related to processing passwords.
- Author:
- Apache Directory Project
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe CRYPT (BCrypt) hash lengthstatic final intThe CRYPT (DES) hash lengthstatic final intThe CRYPT (MD5) hash lengthstatic final intThe CRYPT (SHA-256) hash lengthstatic final intThe CRYPT (SHA-512) hash lengthstatic final intThe MD5 hash lengthstatic final intThe PKCS5S2 hash lengthstatic final intThe SHA1 hash lengthstatic final intThe SHA256 hash lengthstatic final intThe SHA384 hash lengthstatic final intThe SHA512 hash length -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancompareCredentials(byte[] receivedCredentials, byte[] storedCredentials) Compare the credentials.static byte[]createStoragePassword(byte[] credentials, LdapSecurityConstants algorithm) create a hashed password in a format that can be stored in the server.static byte[]createStoragePassword(String credentials, LdapSecurityConstants algorithm) static LdapSecurityConstantsfindAlgorithm(byte[] credentials) Get the algorithm from the stored password.static booleanisPwdExpired(String pwdChangedZtime, int pwdMaxAgeSec, org.apache.directory.api.util.TimeProvider timeProvider) checks if the given password's change time is older than the max agestatic PasswordDetailssplitCredentials(byte[] credentials) Decompose the stored password in an algorithm, an eventual salt and the password itself.
-
Field Details
-
SHA1_LENGTH
public static final int SHA1_LENGTHThe SHA1 hash length- See Also:
-
SHA256_LENGTH
public static final int SHA256_LENGTHThe SHA256 hash length- See Also:
-
SHA384_LENGTH
public static final int SHA384_LENGTHThe SHA384 hash length- See Also:
-
SHA512_LENGTH
public static final int SHA512_LENGTHThe SHA512 hash length- See Also:
-
MD5_LENGTH
public static final int MD5_LENGTHThe MD5 hash length- See Also:
-
PKCS5S2_LENGTH
public static final int PKCS5S2_LENGTHThe PKCS5S2 hash length- See Also:
-
CRYPT_LENGTH
public static final int CRYPT_LENGTHThe CRYPT (DES) hash length- See Also:
-
CRYPT_MD5_LENGTH
public static final int CRYPT_MD5_LENGTHThe CRYPT (MD5) hash length- See Also:
-
CRYPT_SHA256_LENGTH
public static final int CRYPT_SHA256_LENGTHThe CRYPT (SHA-256) hash length- See Also:
-
CRYPT_SHA512_LENGTH
public static final int CRYPT_SHA512_LENGTHThe CRYPT (SHA-512) hash length- See Also:
-
CRYPT_BCRYPT_LENGTH
public static final int CRYPT_BCRYPT_LENGTHThe CRYPT (BCrypt) hash length- See Also:
-
-
Method Details
-
findAlgorithm
Get the algorithm from the stored password. It can be found on the beginning of the stored password, between curly brackets.- Parameters:
credentials- the credentials of the user- Returns:
- the name of the algorithm to use
-
createStoragePassword
- Parameters:
credentials- The passwordalgorithm- The algorithm to use- Returns:
- The resulting byte[] containing the paswword
- See Also:
-
createStoragePassword
create a hashed password in a format that can be stored in the server. If the specified algorithm requires a salt then a random salt of 8 byte size is used- Parameters:
credentials- the plain text passwordalgorithm- the hashing algorithm to be applied- Returns:
- the password after hashing with the given algorithm
- Throws:
IllegalArgumentException- if the specified algorithm is null or unknown
-
compareCredentials
public static boolean compareCredentials(byte[] receivedCredentials, byte[] storedCredentials) Compare the credentials. We have at least 6 algorithms to encrypt the password :- - SHA
- - SSHA (salted SHA)
- - SHA-2(256, 384 and 512 and their salted versions)
- - MD5
- - SMD5 (slated MD5)
- - PKCS5S2 (PBKDF2)
- - crypt (unix crypt)
- - plain text, ie no encryption.
If we get an encrypted password, it is prefixed by the used algorithm, between brackets : {SSHA}password ...
If the password is using SSHA, SMD5 or crypt, some 'salt' is added to the password :- - length(password) - 20, starting at 21st position for SSHA
- - length(password) - 16, starting at 16th position for SMD5
- - length(password) - 2, starting at 3rd position for crypt
For (S)SHA, SHA-256 and (S)MD5, we have to transform the password from Base64 encoded text to a byte[] before comparing the password with the stored one.
For PKCS5S2 the salt is stored in the beginning of the password
For crypt, we only have to remove the salt.
At the end, we use the digest() method for (S)SHA and (S)MD5, the crypt() method for the CRYPT algorithm and a straight comparison for PLAIN TEXT passwords.
The stored password is always using the unsalted form, and is stored as a bytes array.
- Parameters:
receivedCredentials- the credentials provided by userstoredCredentials- the credentials stored in the server- Returns:
- true if they are equal, false otherwise
-
splitCredentials
Decompose the stored password in an algorithm, an eventual salt and the password itself. If the algorithm is SHA, SSHA, MD5 or SMD5, the part following the algorithm is base64 encoded- Parameters:
credentials- The byte[] containing the credentials to split- Returns:
- The password
-
isPwdExpired
public static boolean isPwdExpired(String pwdChangedZtime, int pwdMaxAgeSec, org.apache.directory.api.util.TimeProvider timeProvider) checks if the given password's change time is older than the max age- Parameters:
pwdChangedZtime- time when the password was last changedpwdMaxAgeSec- the max age value in secondstimeProvider- The TimeProvider instance to use- Returns:
- true if expired, false otherwise
-