001/*
002 * Copyright 2017-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2017-2019 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.listener;
022
023
024
025import com.unboundid.ldap.sdk.LDAPException;
026import com.unboundid.ldap.sdk.ResultCode;
027import com.unboundid.util.Base64;
028import com.unboundid.util.Debug;
029import com.unboundid.util.StaticUtils;
030import com.unboundid.util.ThreadSafety;
031import com.unboundid.util.ThreadSafetyLevel;
032
033import static com.unboundid.ldap.listener.ListenerMessages.*;
034
035
036
037/**
038 * This class provides an implementation of a password encoder output formatter
039 * that will format the encoded password using the base64 mechanism described in
040 * <A HREF="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</A>.
041 */
042@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
043public final class Base64PasswordEncoderOutputFormatter
044       extends PasswordEncoderOutputFormatter
045{
046  /**
047   * The singleton instance of this base64 password encoder output formatter.
048   */
049  private static final Base64PasswordEncoderOutputFormatter INSTANCE =
050       new Base64PasswordEncoderOutputFormatter();
051
052
053
054  /**
055   * Creates an instance of this base64 password encoder output formatter.
056   */
057  private Base64PasswordEncoderOutputFormatter()
058  {
059    // No implementation is required.
060  }
061
062
063
064  /**
065   * Retrieves the singleton instance of this base64 password encoder output
066   * formatter.
067   *
068   * @return  The singleton instance of this base64 password encoder output
069   *          formatter.
070   */
071  public static Base64PasswordEncoderOutputFormatter getInstance()
072  {
073    return INSTANCE;
074  }
075
076
077
078  /**
079   * {@inheritDoc}
080   */
081  @Override()
082  public byte[] format(final byte[] unformattedData)
083         throws LDAPException
084  {
085    return StaticUtils.getBytes(Base64.encode(unformattedData));
086  }
087
088
089
090  /**
091   * {@inheritDoc}
092   */
093  @Override()
094  public byte[] unFormat(final byte[] formattedData)
095         throws LDAPException
096  {
097    try
098    {
099      return Base64.decode(StaticUtils.toUTF8String(formattedData));
100    }
101    catch (final Exception e)
102    {
103      Debug.debugException(e);
104      throw new LDAPException(ResultCode.PARAM_ERROR,
105           ERR_BASE64_PW_FORMATTER_CANNOT_DECODE.get(), e);
106    }
107  }
108
109
110
111  /**
112   * {@inheritDoc}
113   */
114  @Override()
115  public void toString(final StringBuilder buffer)
116  {
117    buffer.append("Base64PasswordEncoderOutputFormatter()");
118  }
119}