001/*
002 * Copyright 2017-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2017-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2017-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.listener;
037
038
039
040import com.unboundid.ldap.sdk.LDAPException;
041import com.unboundid.ldap.sdk.ResultCode;
042import com.unboundid.util.Debug;
043import com.unboundid.util.StaticUtils;
044import com.unboundid.util.ThreadSafety;
045import com.unboundid.util.ThreadSafetyLevel;
046
047import static com.unboundid.ldap.listener.ListenerMessages.*;
048
049
050
051/**
052 * This class provides an implementation of a password encoder output formatter
053 * that will use hexadecimal digits to represent the bytes of the encoded
054 * password.
055 */
056@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
057public final class HexPasswordEncoderOutputFormatter
058       extends PasswordEncoderOutputFormatter
059{
060  /**
061   * The singleton instance of this hex password encoder output formatter that
062   * uses lowercase versions of the hexadecimal digits 'a' through 'f'.
063   */
064  private static final HexPasswordEncoderOutputFormatter LOWERCASE_INSTANCE =
065       new HexPasswordEncoderOutputFormatter(true);
066
067
068
069  /**
070   * The singleton instance of this hex password encoder output formatter that
071   * uses uppercase versions of the hexadecimal digits 'A' through 'F'.
072   */
073  private static final HexPasswordEncoderOutputFormatter UPPERCASE_INSTANCE =
074       new HexPasswordEncoderOutputFormatter(false);
075
076
077
078  // Indicates whether to use lowercase letters for hexadecimal digits 'A'
079  // through 'F'.
080  private final boolean useLowercaseLetters;
081
082
083
084  /**
085   * Creates an instance of this hex password encoder output formatter with the
086   * specified configuration.
087   *
088   * @param  useLowercaseLetters Indicates whether the hexadecimal digits 'A'
089   *                             through 'F' should be output as lowercase
090   *                             letters (if {@code true} or as uppercase
091   *                             letters (if {@code false}).
092   */
093  private HexPasswordEncoderOutputFormatter(final boolean useLowercaseLetters)
094  {
095    this.useLowercaseLetters = useLowercaseLetters;
096  }
097
098
099
100  /**
101   * Retrieves a singleton instance of this hex password encoder that will
102   * represent the hexadecimal digits 'A' through 'F' as lowercase letters.
103   *
104   * @return  The hex password encoder instance.
105   */
106  public static HexPasswordEncoderOutputFormatter getLowercaseInstance()
107  {
108    return LOWERCASE_INSTANCE;
109  }
110
111
112
113  /**
114   * Retrieves a singleton instance of this hex password encoder that will
115   * represent the hexadecimal digits 'A' through 'F' as uppercase letters.
116   *
117   * @return  The hex password encoder instance.
118   */
119  public static HexPasswordEncoderOutputFormatter getUppercaseInstance()
120  {
121    return UPPERCASE_INSTANCE;
122  }
123
124
125
126  /**
127   * Indicates whether to represent the hexadecimal digits 'A' through 'F' as
128   * lowercase letters or uppercase letters.  Note that this setting only
129   * applies when formatting an encoded password.  When un-formatting a
130   * password, either uppercase or lowercase letters will be properly handled.
131   *
132   * @return  {@code true} if hexadecimal digits 'A' through 'F' should be
133   *          represented as lowercase letters, or {@code false} if they should
134   *          be represented as uppercase letters.
135   */
136  public boolean useLowercaseLetters()
137  {
138    return useLowercaseLetters;
139  }
140
141
142
143  /**
144   * {@inheritDoc}
145   */
146  @Override()
147  public byte[] format(final byte[] unformattedData)
148         throws LDAPException
149  {
150    String hexString = StaticUtils.toHex(unformattedData);
151    if (! useLowercaseLetters)
152    {
153      hexString = hexString.toUpperCase();
154    }
155
156    return StaticUtils.getBytes(hexString);
157  }
158
159
160
161  /**
162   * {@inheritDoc}
163   */
164  @Override()
165  public byte[] unFormat(final byte[] formattedData)
166         throws LDAPException
167  {
168    try
169    {
170      return StaticUtils.fromHex(StaticUtils.toUTF8String(formattedData));
171    }
172    catch (final Exception e)
173    {
174      Debug.debugException(e);
175      throw new LDAPException(ResultCode.PARAM_ERROR,
176           ERR_HEX_PW_FORMATTER_CANNOT_DECODE.get(), e);
177    }
178  }
179
180
181
182  /**
183   * {@inheritDoc}
184   */
185  @Override()
186  public void toString(final StringBuilder buffer)
187  {
188    buffer.append("HexPasswordEncoderOutputFormatter(useLowercaseLetters=");
189    buffer.append(useLowercaseLetters);
190    buffer.append(')');
191  }
192}