001/*
002 * Copyright (c) 2016-2018 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
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
021package cascading.operation.hash;
022
023import java.beans.ConstructorProperties;
024import java.util.Base64;
025
026import cascading.operation.SerFunction;
027import cascading.tuple.Fields;
028
029/**
030 * Class Base64HashFunction is a sub-class of {@link BaseHashFunction} that Base 64 encodes the digest hash value.
031 * <p>
032 * This class calls {@code Base64.getEncoder().withoutPadding().encode( digest ) }
033 */
034public class Base64HashFunction extends BaseHashFunction
035  {
036  /**
037   * Constructor Base64HashFunction creates a new Base64HashFunction instance.
038   *
039   * @param fieldDeclaration of Fields
040   */
041  @ConstructorProperties("fieldDeclaration")
042  public Base64HashFunction( Fields fieldDeclaration )
043    {
044    super( fieldDeclaration );
045    }
046
047  /**
048   * Constructor Base64HashFunction creates a new Base64HashFunction instance.
049   *
050   * @param fieldDeclaration of Fields
051   * @param preDigest        of SerFunction<String, String>
052   * @param postEncoding     of SerFunction<StringBuilder, StringBuilder>
053   */
054  @ConstructorProperties({"fieldDeclaration", "preDigest", "postEncoding"})
055  public Base64HashFunction( Fields fieldDeclaration, SerFunction<String, String> preDigest, SerFunction<StringBuilder, StringBuilder> postEncoding )
056    {
057    super( fieldDeclaration, preDigest, postEncoding );
058    }
059
060  /**
061   * Constructor Base64HashFunction creates a new Base64HashFunction instance.
062   *
063   * @param fieldDeclaration of Fields
064   * @param algorithm        of String
065   */
066  @ConstructorProperties({"fieldDeclaration", "algorithm"})
067  public Base64HashFunction( Fields fieldDeclaration, String algorithm )
068    {
069    super( fieldDeclaration, algorithm );
070    }
071
072  /**
073   * Constructor Base64HashFunction creates a new Base64HashFunction instance.
074   *
075   * @param fieldDeclaration of Fields
076   * @param algorithm        of String
077   * @param preDigest        of SerFunction<String, String>
078   * @param postEncoding     of SerFunction<StringBuilder, StringBuilder>
079   */
080  @ConstructorProperties({"fieldDeclaration", "algorithm", "preDigest", "postEncoding"})
081  public Base64HashFunction( Fields fieldDeclaration, String algorithm, SerFunction<String, String> preDigest, SerFunction<StringBuilder, StringBuilder> postEncoding )
082    {
083    super( fieldDeclaration, algorithm, preDigest, postEncoding );
084    }
085
086  /**
087   * Constructor Base64HashFunction creates a new Base64HashFunction instance.
088   *
089   * @param fieldDeclaration of Fields
090   * @param algorithm        of String
091   * @param maxLength        of int
092   */
093  @ConstructorProperties({"fieldDeclaration", "algorithm", "maxLength"})
094  public Base64HashFunction( Fields fieldDeclaration, String algorithm, int maxLength )
095    {
096    super( fieldDeclaration, algorithm, maxLength );
097    }
098
099  /**
100   * Constructor Base64HashFunction creates a new Base64HashFunction instance.
101   *
102   * @param fieldDeclaration of Fields
103   * @param algorithm        of String
104   * @param maxLength        of int
105   * @param preDigest        of SerFunction<String, String>
106   * @param postEncoding     of SerFunction<StringBuilder, StringBuilder>
107   */
108  @ConstructorProperties({"fieldDeclaration", "algorithm", "maxLength", "preDigest", "postEncoding"})
109  public Base64HashFunction( Fields fieldDeclaration, String algorithm, int maxLength, SerFunction<String, String> preDigest, SerFunction<StringBuilder, StringBuilder> postEncoding )
110    {
111    super( fieldDeclaration, algorithm, maxLength, preDigest, postEncoding );
112    }
113
114  /**
115   * Constructor Base64HashFunction creates a new Base64HashFunction instance.
116   *
117   * @param fieldDeclaration of Fields
118   * @param algorithm        of String
119   * @param maxLength        of int
120   * @param charsetName      of String
121   */
122  @ConstructorProperties({"fieldDeclaration", "algorithm", "maxLength", "charsetName"})
123  public Base64HashFunction( Fields fieldDeclaration, String algorithm, int maxLength, String charsetName )
124    {
125    super( fieldDeclaration, algorithm, maxLength, charsetName );
126    }
127
128  /**
129   * Constructor Base64HashFunction creates a new Base64HashFunction instance.
130   *
131   * @param fieldDeclaration of Fields
132   * @param algorithm        of String
133   * @param maxLength        of int
134   * @param charsetName      of String
135   * @param preDigest        of SerFunction<String, String>
136   * @param postEncoding     of SerFunction<StringBuilder, StringBuilder>
137   */
138  @ConstructorProperties({"fieldDeclaration", "algorithm", "maxLength", "charsetName", "preDigest", "postEncoding"})
139  public Base64HashFunction( Fields fieldDeclaration, String algorithm, int maxLength, String charsetName, SerFunction<String, String> preDigest, SerFunction<StringBuilder, StringBuilder> postEncoding )
140    {
141    super( fieldDeclaration, algorithm, maxLength, charsetName, preDigest, postEncoding );
142    }
143
144  /**
145   * Method performEncoding ...
146   *
147   * @param buffer of StringBuilder
148   * @param digest of byte[]
149   */
150  @Override
151  protected void performEncoding( StringBuilder buffer, byte[] digest )
152    {
153    buffer.append( new String( Base64.getEncoder().withoutPadding().encode( digest ), getCharset() ) );
154    }
155  }