View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.common.text;
25  
26  import java.security.NoSuchAlgorithmException;
27  import org.modeshape.common.util.Base64;
28  import org.modeshape.common.util.CheckArg;
29  import org.modeshape.common.util.SecureHash;
30  import org.modeshape.common.util.SecureHash.Algorithm;
31  
32  /**
33   * A text encoder that performs a secure hash of the input text and returns that hash as the encoded text. This encoder can be
34   * configured to use different secure hash algorithms and to return a fixed number of characters from the hash.
35   */
36  public class SecureHashTextEncoder implements TextEncoder {
37  
38      private final Algorithm algorithm;
39      private final int maxLength;
40  
41      /**
42       * Create an encoder that uses the supplied algorithm and returns only the supplied number of characters in the hash.
43       * 
44       * @param algorithm the algorithm that should be used
45       * @throws IllegalArgumentException if the algorithm is null
46       */
47      public SecureHashTextEncoder( Algorithm algorithm ) {
48          this(algorithm, Integer.MAX_VALUE);
49      }
50  
51      /**
52       * Create an encoder that uses the supplied algorithm and returns only the supplied number of characters in the hash.
53       * 
54       * @param algorithm the algorithm that should be used
55       * @param maxLength the maximumLength, or a non-positive number (or {@link Integer#MAX_VALUE}) if the full hash should be used
56       * @throws IllegalArgumentException if the algorithm is null
57       */
58      public SecureHashTextEncoder( Algorithm algorithm,
59                                    int maxLength ) {
60          CheckArg.isNotNull(algorithm, "algorithm");
61          this.algorithm = algorithm;
62          this.maxLength = maxLength < 1 ? Integer.MAX_VALUE : maxLength;
63      }
64  
65      /**
66       * Get the maximum length of the encoded string, or {@link Integer#MAX_VALUE} if there is no maximum.
67       * 
68       * @return the maximum encoded string length; always positive
69       */
70      public int getMaxLength() {
71          return maxLength;
72      }
73  
74      /**
75       * Return the secure hash algorithm used by this encoder.
76       * 
77       * @return the algorithm; never null
78       */
79      public Algorithm getAlgorithm() {
80          return algorithm;
81      }
82  
83      /**
84       * {@inheritDoc}
85       * 
86       * @see org.modeshape.common.text.TextEncoder#encode(java.lang.String)
87       */
88      public String encode( String text ) {
89          try {
90              byte[] hash = SecureHash.getHash(algorithm, text.getBytes());
91              String result = Base64.encodeBytes(hash);
92              return result.length() < maxLength ? result : result.substring(0, maxLength);
93          } catch (NoSuchAlgorithmException e) {
94              return text;
95          }
96      }
97  
98  }