play.modules.fileauth.utils
Class MD5Crypt

java.lang.Object
  extended by play.modules.fileauth.utils.MD5Crypt

public final class MD5Crypt
extends java.lang.Object

This class defines a method, crypt(), which takes a password and a salt string and generates an OpenBSD/FreeBSD/Linux-compatible md5-encoded password entry.


Field Summary
static java.lang.String itoa64
           
static java.lang.String SALTCHARS
           
 
Constructor Summary
MD5Crypt()
           
 
Method Summary
static java.lang.String apacheCrypt(java.lang.String password)
          This method generates an Apache MD5 compatible md5-encoded password hash from a plaintext password and a salt.
static java.lang.String apacheCrypt(java.lang.String password, java.lang.String salt)
          This method actually generates an Apache MD5 compatible md5-encoded password hash from a plaintext password and a salt.
static int bytes2u(byte inp)
          convert an encoded unsigned byte value into a int with the unsigned value.
static void clearbits(byte[] bits)
           
static java.lang.String crypt(java.lang.String password)
          This method actually generates a OpenBSD/FreeBSD/Linux PAM compatible md5-encoded password hash from a plaintext password and a salt.
static java.lang.String crypt(java.lang.String password, java.lang.String salt)
          This method actually generates a OpenBSD/FreeBSD/Linux PAM compatible md5-encoded password hash from a plaintext password and a salt.
static java.lang.String crypt(java.lang.String password, java.lang.String salt, java.lang.String magic)
          This method actually generates md5-encoded password hash from a plaintext password, a salt, and a magic string.
static void main(java.lang.String[] argv)
          Command line test rig.
static java.lang.String to64(long v, int size)
           
static boolean verifyPassword(java.lang.String plaintextPass, java.lang.String md5CryptText)
          This method tests a plaintext password against a md5Crypt'ed hash and returns true if the password matches the hash.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SALTCHARS

public static final java.lang.String SALTCHARS
See Also:
Constant Field Values

itoa64

public static final java.lang.String itoa64
See Also:
Constant Field Values
Constructor Detail

MD5Crypt

public MD5Crypt()
Method Detail

main

public static void main(java.lang.String[] argv)
Command line test rig.


to64

public static final java.lang.String to64(long v,
                                          int size)

clearbits

public static final void clearbits(byte[] bits)

bytes2u

public static final int bytes2u(byte inp)
convert an encoded unsigned byte value into a int with the unsigned value.


crypt

public static final java.lang.String crypt(java.lang.String password)

This method actually generates a OpenBSD/FreeBSD/Linux PAM compatible md5-encoded password hash from a plaintext password and a salt.

The resulting string will be in the form '$1$<salt>$<hashed mess>

Parameters:
password - Plaintext password
Returns:
An OpenBSD/FreeBSD/Linux-compatible md5-hashed password field.

crypt

public static final java.lang.String crypt(java.lang.String password,
                                           java.lang.String salt)

This method actually generates a OpenBSD/FreeBSD/Linux PAM compatible md5-encoded password hash from a plaintext password and a salt.

The resulting string will be in the form '$1$<salt>$<hashed mess>

Parameters:
password - Plaintext password
salt - A short string to use to randomize md5. May start with $1$, which will be ignored. It is explicitly permitted to pass a pre-existing MD5Crypt'ed password entry as the salt. crypt() will strip the salt chars out properly.
Returns:
An OpenBSD/FreeBSD/Linux-compatible md5-hashed password field.

apacheCrypt

public static final java.lang.String apacheCrypt(java.lang.String password)

This method generates an Apache MD5 compatible md5-encoded password hash from a plaintext password and a salt.

The resulting string will be in the form '$apr1$<salt>$<hashed mess>

Parameters:
password - Plaintext password
Returns:
An Apache-compatible md5-hashed password string.

apacheCrypt

public static final java.lang.String apacheCrypt(java.lang.String password,
                                                 java.lang.String salt)

This method actually generates an Apache MD5 compatible md5-encoded password hash from a plaintext password and a salt.

The resulting string will be in the form '$apr1$<salt>$<hashed mess>

Parameters:
password - Plaintext password
salt - A short string to use to randomize md5. May start with $apr1$, which will be ignored. It is explicitly permitted to pass a pre-existing MD5Crypt'ed password entry as the salt. crypt() will strip the salt chars out properly.
Returns:
An Apache-compatible md5-hashed password string.

crypt

public static final java.lang.String crypt(java.lang.String password,
                                           java.lang.String salt,
                                           java.lang.String magic)

This method actually generates md5-encoded password hash from a plaintext password, a salt, and a magic string.

There are two magic strings that make sense to use here.. '$1$' is the magic string used by the FreeBSD/Linux/OpenBSD MD5Crypt algorithm, and '$apr1$' is the magic string used by the Apache MD5Crypt algorithm.

The resulting string will be in the form '<magic><salt>$<hashed mess>

Parameters:
password - Plaintext password
salt - A short string to use to randomize md5. May start with the magic string, which will be ignored. It is explicitly permitted to pass a pre-existing MD5Crypt'ed password entry as the salt. crypt() will strip the salt chars out properly.
magic - Either "$apr1$" or "$1$", which controls whether we are doing Apache-style or FreeBSD-style md5Crypt.
Returns:
An md5-hashed password string.

verifyPassword

public static final boolean verifyPassword(java.lang.String plaintextPass,
                                           java.lang.String md5CryptText)
This method tests a plaintext password against a md5Crypt'ed hash and returns true if the password matches the hash. This method will work properly whether the hashtext was crypted using the default FreeBSD md5Crypt algorithm or the Apache md5Crypt variant.

Parameters:
plaintextPass - The plaintext password text to test.
md5CryptText - The Apache or FreeBSD-md5Crypted hash used to authenticate the plaintextPass.