public final class PasswordUtil extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
CRYPT_BCRYPT_LENGTH
The CRYPT (BCrypt) hash length
|
static int |
CRYPT_LENGTH
The CRYPT (DES) hash length
|
static int |
CRYPT_MD5_LENGTH
The CRYPT (MD5) hash length
|
private static byte[] |
CRYPT_SALT_CHARS |
static int |
CRYPT_SHA256_LENGTH
The CRYPT (SHA-256) hash length
|
static int |
CRYPT_SHA512_LENGTH
The CRYPT (SHA-512) hash length
|
static int |
MD5_LENGTH
The MD5 hash length
|
static int |
PKCS5S2_LENGTH
The PKCS5S2 hash length
|
static int |
SHA1_LENGTH
The SHA1 hash length
|
static int |
SHA256_LENGTH
The SHA256 hash length
|
static int |
SHA384_LENGTH
The SHA384 hash length
|
static int |
SHA512_LENGTH
The SHA512 hash length
|
| Modifier | Constructor and Description |
|---|---|
private |
PasswordUtil() |
| Modifier and Type | Method and Description |
|---|---|
private static boolean |
compareBytes(byte[] provided,
byte[] stored)
Compare two byte[] in a constant time.
|
static boolean |
compareCredentials(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) |
private static byte[] |
digest(LdapSecurityConstants algorithm,
byte[] password,
byte[] salt)
Compute the hashed password given an algorithm, the credentials and
an optional salt.
|
private static byte[] |
encryptPassword(byte[] credentials,
LdapSecurityConstants algorithm,
byte[] salt)
encrypts the given credentials based on the algorithm name and optional salt
|
static LdapSecurityConstants |
findAlgorithm(byte[] credentials)
Get the algorithm from the stored password.
|
private static byte[] |
generateCryptSalt(int length) |
private static byte[] |
generatePbkdf2Hash(byte[] credentials,
LdapSecurityConstants algorithm,
byte[] salt)
generates a hash based on the PKCS5S2 spec
Note: this has been implemented to generate hashes compatible with what JIRA generates.
|
private static PasswordDetails |
getCredentials(byte[] credentials,
int algoLength,
int hashLen,
LdapSecurityConstants algorithm)
Compute the credentials
|
private static PasswordDetails |
getCryptCredentials(byte[] credentials,
int algoLength,
LdapSecurityConstants algorithm) |
private static PasswordDetails |
getPbkdf2Credentials(byte[] credentials,
int algoLength,
LdapSecurityConstants algorithm)
Gets the credentials from a PKCS5S2 hash.
|
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
|
private static void |
merge(byte[] all,
byte[] left,
byte[] right) |
private static void |
split(byte[] all,
int offset,
byte[] left,
byte[] right) |
static PasswordDetails |
splitCredentials(byte[] credentials)
Decompose the stored password in an algorithm, an eventual salt
and the password itself.
|
public static final int SHA1_LENGTH
public static final int SHA256_LENGTH
public static final int SHA384_LENGTH
public static final int SHA512_LENGTH
public static final int MD5_LENGTH
public static final int PKCS5S2_LENGTH
public static final int CRYPT_LENGTH
public static final int CRYPT_MD5_LENGTH
public static final int CRYPT_SHA256_LENGTH
public static final int CRYPT_SHA512_LENGTH
public static final int CRYPT_BCRYPT_LENGTH
private static final byte[] CRYPT_SALT_CHARS
public static LdapSecurityConstants findAlgorithm(byte[] credentials)
credentials - the credentials of the userpublic static byte[] createStoragePassword(String credentials, LdapSecurityConstants algorithm)
credentials - The passwordalgorithm - The algorithm to usecreateStoragePassword(byte[], LdapSecurityConstants)public static byte[] createStoragePassword(byte[] credentials,
LdapSecurityConstants algorithm)
credentials - the plain text passwordalgorithm - the hashing algorithm to be appliedpublic static boolean compareCredentials(byte[] receivedCredentials,
byte[] storedCredentials)
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 :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.
receivedCredentials - the credentials provided by userstoredCredentials - the credentials stored in the serverprivate static boolean compareBytes(byte[] provided,
byte[] stored)
provided - The provided passwordstored - The stored passwordprivate static byte[] encryptPassword(byte[] credentials,
LdapSecurityConstants algorithm,
byte[] salt)
credentials - the credentials to be encryptedalgorithm - the algorithm to be used for encrypting the credentialssalt - value to be used as salt (optional)private static byte[] digest(LdapSecurityConstants algorithm, byte[] password, byte[] salt)
algorithm - the algorithm to usepassword - the credentialssalt - the optional saltpublic static PasswordDetails splitCredentials(byte[] credentials)
credentials - The byte[] containing the credentials to splitprivate static PasswordDetails getCredentials(byte[] credentials, int algoLength, int hashLen, LdapSecurityConstants algorithm)
credentials - the credentialsalgoLength - The algorithm lengthhashLen - The hash lengthalgorithm - the algorithm to useprivate static void split(byte[] all,
int offset,
byte[] left,
byte[] right)
private static void merge(byte[] all,
byte[] left,
byte[] right)
public static boolean isPwdExpired(String pwdChangedZtime, int pwdMaxAgeSec, org.apache.directory.api.util.TimeProvider timeProvider)
pwdChangedZtime - time when the password was last changedpwdMaxAgeSec - the max age value in secondstimeProvider - The TimeProvider instance to useprivate static byte[] generatePbkdf2Hash(byte[] credentials,
LdapSecurityConstants algorithm,
byte[] salt)
credentials - the credentialsalgorithm - the algorithm to usesalt - the optional saltprivate static PasswordDetails getPbkdf2Credentials(byte[] credentials, int algoLength, LdapSecurityConstants algorithm)
credentials - The passwordalgoLength - The length of the algorithm partalgorithm - The algorithm in useprivate static byte[] generateCryptSalt(int length)
private static PasswordDetails getCryptCredentials(byte[] credentials, int algoLength, LdapSecurityConstants algorithm)
Copyright © 2003–2022 The Apache Software Foundation. All rights reserved.