001/*
002 * Copyright 2017-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2017-2022 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-2022 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.NotNull;
044import com.unboundid.util.StaticUtils;
045import com.unboundid.util.ThreadSafety;
046import com.unboundid.util.ThreadSafetyLevel;
047
048import static com.unboundid.ldap.listener.ListenerMessages.*;
049
050
051
052/**
053 * This class provides an implementation of a password encoder output formatter
054 * that will use hexadecimal digits to represent the bytes of the encoded
055 * password.
056 */
057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
058public final class HexPasswordEncoderOutputFormatter
059       extends PasswordEncoderOutputFormatter
060{
061  /**
062   * The singleton instance of this hex password encoder output formatter that
063   * uses lowercase versions of the hexadecimal digits 'a' through 'f'.
064   */
065  @NotNull private static final HexPasswordEncoderOutputFormatter
066       LOWERCASE_INSTANCE = new HexPasswordEncoderOutputFormatter(true);
067
068
069
070  /**
071   * The singleton instance of this hex password encoder output formatter that
072   * uses uppercase versions of the hexadecimal digits 'A' through 'F'.
073   */
074  @NotNull private static final HexPasswordEncoderOutputFormatter
075       UPPERCASE_INSTANCE = new HexPasswordEncoderOutputFormatter(false);
076
077
078
079  // Indicates whether to use lowercase letters for hexadecimal digits 'A'
080  // through 'F'.
081  private final boolean useLowercaseLetters;
082
083
084
085  /**
086   * Creates an instance of this hex password encoder output formatter with the
087   * specified configuration.
088   *
089   * @param  useLowercaseLetters Indicates whether the hexadecimal digits 'A'
090   *                             through 'F' should be output as lowercase
091   *                             letters (if {@code true} or as uppercase
092   *                             letters (if {@code false}).
093   */
094  private HexPasswordEncoderOutputFormatter(final boolean useLowercaseLetters)
095  {
096    this.useLowercaseLetters = useLowercaseLetters;
097  }
098
099
100
101  /**
102   * Retrieves a singleton instance of this hex password encoder that will
103   * represent the hexadecimal digits 'A' through 'F' as lowercase letters.
104   *
105   * @return  The hex password encoder instance.
106   */
107  @NotNull()
108  public static HexPasswordEncoderOutputFormatter getLowercaseInstance()
109  {
110    return LOWERCASE_INSTANCE;
111  }
112
113
114
115  /**
116   * Retrieves a singleton instance of this hex password encoder that will
117   * represent the hexadecimal digits 'A' through 'F' as uppercase letters.
118   *
119   * @return  The hex password encoder instance.
120   */
121  @NotNull()
122  public static HexPasswordEncoderOutputFormatter getUppercaseInstance()
123  {
124    return UPPERCASE_INSTANCE;
125  }
126
127
128
129  /**
130   * Indicates whether to represent the hexadecimal digits 'A' through 'F' as
131   * lowercase letters or uppercase letters.  Note that this setting only
132   * applies when formatting an encoded password.  When un-formatting a
133   * password, either uppercase or lowercase letters will be properly handled.
134   *
135   * @return  {@code true} if hexadecimal digits 'A' through 'F' should be
136   *          represented as lowercase letters, or {@code false} if they should
137   *          be represented as uppercase letters.
138   */
139  public boolean useLowercaseLetters()
140  {
141    return useLowercaseLetters;
142  }
143
144
145
146  /**
147   * {@inheritDoc}
148   */
149  @Override()
150  @NotNull()
151  public byte[] format(@NotNull final byte[] unformattedData)
152         throws LDAPException
153  {
154    String hexString = StaticUtils.toHex(unformattedData);
155    if (! useLowercaseLetters)
156    {
157      hexString = hexString.toUpperCase();
158    }
159
160    return StaticUtils.getBytes(hexString);
161  }
162
163
164
165  /**
166   * {@inheritDoc}
167   */
168  @Override()
169  @NotNull()
170  public byte[] unFormat(@NotNull final byte[] formattedData)
171         throws LDAPException
172  {
173    try
174    {
175      return StaticUtils.fromHex(StaticUtils.toUTF8String(formattedData));
176    }
177    catch (final Exception e)
178    {
179      Debug.debugException(e);
180      throw new LDAPException(ResultCode.PARAM_ERROR,
181           ERR_HEX_PW_FORMATTER_CANNOT_DECODE.get(), e);
182    }
183  }
184
185
186
187  /**
188   * {@inheritDoc}
189   */
190  @Override()
191  public void toString(@NotNull final StringBuilder buffer)
192  {
193    buffer.append("HexPasswordEncoderOutputFormatter(useLowercaseLetters=");
194    buffer.append(useLowercaseLetters);
195    buffer.append(')');
196  }
197}