Zend_Ldap_Attribute::createPassword PHP Method

createPassword() public static method

Creates a LDAP password.
public static createPassword ( string $password, string $hashType = self::PASSWORD_HASH_MD5 ) : string
$password string
$hashType string
return string
    public static function createPassword($password, $hashType = self::PASSWORD_HASH_MD5)
    {
        switch ($hashType) {
            case self::PASSWORD_UNICODEPWD:
                /* see:
                 * http://msdn.microsoft.com/en-us/library/cc223248(PROT.10).aspx
                 */
                $password = '"' . $password . '"';
                if (function_exists('mb_convert_encoding')) {
                    $password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
                } else {
                    if (function_exists('iconv')) {
                        $password = iconv('UTF-8', 'UTF-16LE', $password);
                    } else {
                        $len = strlen($password);
                        $new = '';
                        for ($i = 0; $i < $len; $i++) {
                            $new .= $password[$i] . "";
                        }
                        $password = $new;
                    }
                }
                return $password;
            case self::PASSWORD_HASH_SSHA:
                $salt = Zend_Crypt_Math::randBytes(4);
                $rawHash = sha1($password . $salt, true) . $salt;
                $method = '{SSHA}';
                break;
            case self::PASSWORD_HASH_SHA:
                $rawHash = sha1($password, true);
                $method = '{SHA}';
                break;
            case self::PASSWORD_HASH_SMD5:
                $salt = Zend_Crypt_Math::randBytes(4);
                $rawHash = md5($password . $salt, true) . $salt;
                $method = '{SMD5}';
                break;
            case self::PASSWORD_HASH_MD5:
            default:
                $rawHash = md5($password, true);
                $method = '{MD5}';
                break;
        }
        return $method . base64_encode($rawHash);
    }

Usage Example

Exemplo n.º 1
0
 public function testPasswordGenerationUnicodePwd()
 {
     $password = '******';
     $unicodePwd = Zend_Ldap_Attribute::createPassword($password, Zend_Ldap_Attribute::PASSWORD_UNICODEPWD);
     $this->assertEquals(10, strlen($unicodePwd));
     $this->assertEquals("\"new\"", $unicodePwd);
 }