There are four formats that Apache recognizes for basic-authentication
passwords. Note that not all formats work on every platform:
PLAIN TEXT (i.e. unencrypted)
Windows, BEOS, & Netware only.
CRYPT
Unix only. Uses the traditional Unix crypt(3) function
with a randomly-generated 32-bit salt (only 12 bits used) and the first 8
characters of the password.
SHA1
"{SHA}" + Base64-encoded SHA-1 digest of the password.
MD5
"$apr1$" + the result of an Apache-specific algorithm using an
iterated (1,000 times) MD5 digest of various combinations of a
random 32-bit salt and the password. See the APR source file
apr_md5.c
for the details of the algorithm.
Note that using myPasswo instead of
myPassword will produce the same result because only the
first 8 characters of CRYPT passwords are considered.
The salt for an MD5 password is between $apr1$ and the
following $ (as a Base64-encoded binary value - max 8 chars).
To validate myPassword against
$apr1$r31.....$HqJZimcKQFAMYayBlzkrA/
The SHA1 variant is probably the most useful format for DBD
authentication. Since the SHA1 and Base64 functions are commonly
available, other software can populate a database with encrypted passwords
that are usable by Apache basic authentication.
To create Apache SHA1-variant basic-authentication passwords in various
languages:
PHP
'{SHA}' . base64_encode(sha1($password, TRUE))
Java
"{SHA}" + new sun.misc.BASE64Encoder().encode(java.security.MessageDigest.getInstance("SHA1").digest(password.getBytes()))
Apache recognizes one format for
digest-authentication passwords - the MD5 hash of the string
user:realm:password as a 32-character string of hexadecimal
digits. realm is the Authorization Realm argument to the
AuthName directive in
httpd.conf.
Database password fields for mod_dbd
Since the MD5 function is commonly available, other software can
populate a database with encrypted passwords that are usable by Apache
digest authentication.
To create Apache digest-authentication passwords in various
languages:
PHP
md5($user . ':' . $realm . ':' .$password)
Java
byte b[] = java.security.MessageDigest.getInstance("MD5").digest( (user + ":" + realm + ":" + password ).getBytes());
java.math.BigInteger bi = new java.math.BigInteger(1, b);
String s = bi.toString(16);
while (s.length() < 32)
s = "0" + s;
// String s is the encrypted password