public class PasswordDigest
extends java.lang.Object
If the realm is missing, the digest will calculate:
MD5(user + ':' + password)
If the realm is specified, the digest will calculate:
MD5(user + ':' + realm + ':' + password)
The second version matches the way HTTP digest authentication is handled, so it is the preferred method for storing passwords.
The returned result is the base64 encoding of the digest.
Constructor and Description |
---|
PasswordDigest() |
Modifier and Type | Method and Description |
---|---|
protected static void |
addDigestUTF8(java.security.MessageDigest digest,
char ch)
Adds the string to the digest using a UTF8 encoding.
|
protected static void |
addDigestUTF8(java.security.MessageDigest digest,
char[] string)
Adds the string to the digest using a UTF8 encoding.
|
protected static void |
addDigestUTF8(java.security.MessageDigest digest,
java.lang.String string)
Adds the string to the digest using a UTF8 encoding.
|
void |
addText(java.lang.String value)
Sets the algorithm for bean-style init.
|
protected static char[] |
digestToBase64(byte[] digest,
int len) |
protected char[] |
digestToCharArray(byte[] digest,
int len)
Convert the digest byte array to a string.
|
protected static char[] |
digestToHex(byte[] digest,
int len) |
protected java.lang.String |
digestToString(byte[] digest,
int len)
Convert the digest byte array to a string.
|
java.lang.String |
getAlgorithm()
Returns the message digest algorithm.
|
java.lang.String |
getFormat()
Returns the message digest format (base64 or hex).
|
java.lang.String |
getPasswordDigest(java.lang.String password)
Returns the digest of the password
|
char[] |
getPasswordDigest(java.lang.String user,
char[] password)
Returns the digest of the user/password
|
char[] |
getPasswordDigest(java.lang.String user,
char[] password,
java.lang.String realm)
Returns the digest of the user/password
|
java.lang.String |
getPasswordDigest(java.lang.String user,
java.lang.String password)
Returns the digest of the user/password
|
java.lang.String |
getPasswordDigest(java.lang.String user,
java.lang.String password,
HttpServletRequest request)
Returns the digest of the user/password
|
java.lang.String |
getPasswordDigest(java.lang.String user,
java.lang.String password,
java.lang.String realm)
Returns the digest of the user/password
|
java.lang.String |
getPasswordDigest(java.lang.String user,
java.lang.String password,
java.lang.String realm,
HttpServletRequest request)
Returns the digest of the user/password
|
java.lang.String |
getRealm()
Returns the message digest default realm.
|
java.lang.String |
getType() |
void |
init()
Initialize the digest.
|
void |
setAlgorithm(java.lang.String algorithm)
Returns the message digest algorithm.
|
void |
setFormat(java.lang.String format)
Set the message digest format (base64 or hex).
|
void |
setRealm(java.lang.String realm)
Set the message digest default realm
|
byte[] |
stringToDigest(char[] s)
Convert the string to a digest byte array.
|
byte[] |
stringToDigest(java.lang.String s)
Convert the string to a digest byte array.
|
java.lang.String |
toString() |
protected void |
updateDigest(java.security.MessageDigest digest,
java.lang.String user,
char[] password,
java.lang.String realm)
Updates the digest based on the user:realm:password
|
public void setAlgorithm(java.lang.String algorithm)
public java.lang.String getAlgorithm()
public void setFormat(java.lang.String format)
public java.lang.String getFormat()
public void setRealm(java.lang.String realm)
public java.lang.String getRealm()
public java.lang.String getType()
public void addText(java.lang.String value) throws ConfigException
ConfigException
@PostConstruct public void init()
public java.lang.String getPasswordDigest(java.lang.String password) throws ServletException
ServletException
public java.lang.String getPasswordDigest(java.lang.String user, java.lang.String password)
public java.lang.String getPasswordDigest(java.lang.String user, java.lang.String password, java.lang.String realm)
public java.lang.String getPasswordDigest(java.lang.String user, java.lang.String password, HttpServletRequest request)
The default implementation returns the digest of user:password or user:realm:password if a default realm has been configured.
request
- the http requestresponse
- the http responseapp
- the servlet contextuser
- the user namepassword
- the cleartext passwordpublic java.lang.String getPasswordDigest(java.lang.String user, java.lang.String password, java.lang.String realm, HttpServletRequest request)
The default implementation returns the digest of user:realm:password. If the realm is null, it will use user:password.
request
- the http requestuser
- the user namepassword
- the cleartext passwordrealm
- the security realmpublic char[] getPasswordDigest(java.lang.String user, char[] password)
The default implementation returns the digest of user:realm:password. If the realm is null, it will use user:password.
user
- the user namepassword
- the cleartext passwordrealm
- the security realmpublic char[] getPasswordDigest(java.lang.String user, char[] password, java.lang.String realm)
The default implementation returns the digest of user:realm:password. If the realm is null, it will use user:password.
user
- the user namepassword
- the cleartext passwordrealm
- the security realmprotected void updateDigest(java.security.MessageDigest digest, java.lang.String user, char[] password, java.lang.String realm)
protected static void addDigestUTF8(java.security.MessageDigest digest, java.lang.String string)
protected static void addDigestUTF8(java.security.MessageDigest digest, char[] string)
protected static void addDigestUTF8(java.security.MessageDigest digest, char ch)
public byte[] stringToDigest(java.lang.String s)
public byte[] stringToDigest(char[] s)
protected java.lang.String digestToString(byte[] digest, int len)
protected char[] digestToCharArray(byte[] digest, int len)
protected static char[] digestToBase64(byte[] digest, int len)
protected static char[] digestToHex(byte[] digest, int len)
public java.lang.String toString()
toString
in class java.lang.Object