View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.security;
35  
36  import  org.mindrot.jbcrypt.BCrypt;
37  import java.security.MessageDigest;
38  import java.security.NoSuchAlgorithmException;
39  
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  
44  /**
45   * Encryption helper.
46   *
47   * Supported algorithm:
48   * <ul>
49   * <li>SHA-1 </li>
50   * <li>MD5 </li>
51   * </ul>
52   * Future:
53   * <ul>
54   * <li>SHA-256 </li>
55   * <li>SHA-384 </li>
56   * <li>SHA-512 </li>
57   * </ul>
58   *
59   * @author Sameer Charles
60   * @version 2.0
61   */
62  public final class Digester {
63  
64      public static final String SHA1 = "SHA-1"; //$NON-NLS-1$
65  
66      public static final String MD5 = "MD5"; //$NON-NLS-1$
67  
68      /**
69       * There are five (5) FIPS-approved* algorithms for generating a condensed representation of a message (message
70       * digest): SHA-1, SHA-224, SHA-256,SHA-384, and SHA-512. <strong>Not supported yet </strong>
71       */
72      public static final String SHA256 = "SHA-256"; //$NON-NLS-1$
73  
74      public static final String SHA384 = "SHA-384"; //$NON-NLS-1$
75  
76      public static final String SHA512 = "SHA-512"; //$NON-NLS-1$
77  
78      private static Logger log = LoggerFactory.getLogger(Digester.class);
79  
80      /**
81       * Utility class, don't instantiate.
82       */
83      private Digester() {
84          // unused
85      }
86  
87      public static String getDigest(String data, String algorithm) throws NoSuchAlgorithmException {
88          MessageDigest md = MessageDigest.getInstance(algorithm);
89          md.reset();
90          return new String(md.digest(data.getBytes()));
91      }
92  
93      public static byte[] getDigest(byte[] data, String algorithm) throws NoSuchAlgorithmException {
94          MessageDigest md = MessageDigest.getInstance(algorithm);
95          md.reset();
96          return md.digest(data);
97      }
98  
99      /**
100      * Gets SHA-1 encoded -> hex string.
101      */
102     public static String getSHA1Hex(String data) {
103         try {
104             String result = Digester.getDigest(data, Digester.SHA1);
105             return Digester.toHEX(result);
106         }
107         catch (NoSuchAlgorithmException e) {
108             log.error(e.getMessage(), e);
109         }
110         return data;
111     }
112 
113     /**
114      * Gets MD5 encoded -> hex string.
115      */
116     public static String getMD5Hex(String data) {
117         try {
118             String result = Digester.getDigest(data, Digester.MD5);
119             return Digester.toHEX(result);
120         }
121         catch (NoSuchAlgorithmException e) {
122             log.error(e.getMessage(), e);
123         }
124         return data;
125     }
126 
127     /**
128      * Converts a byte array to a string Hex.
129      * @param data to be converted
130      * @return string representing hex values of the byte array
131      */
132     public static String toHEX(String data) {
133         return Digester.toHEX(data.getBytes());
134     }
135 
136     /**
137      * Converts a byte array to a string Hex.
138      * @param data to be converted
139      * @return string representing hex values of the byte array
140      */
141     public static String toHEX(byte[] data) {
142         StringBuffer hexValue = new StringBuffer();
143         char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
144         for (int i = 0; i < data.length; i++) {
145             byte byteValue = data[i];
146             hexValue.append(digits[(byteValue & 0xf0) >> 4]);
147             hexValue.append(digits[byteValue & 0x0f]);
148         }
149         return hexValue.toString();
150     }
151 
152     public static String getBCrypt(String text) {
153         // gensalt's log_rounds parameter determines the complexity
154         // the work factor is 2^log_rounds, and the default is 10
155         String hashed = BCrypt.hashpw(text, BCrypt.gensalt(12));
156         return hashed;
157     }
158 
159     public static boolean matchBCrypted(String candidate, String hash) {
160         // Check that an unencrypted password matches one that has
161         // previously been hashed
162         return BCrypt.checkpw(candidate, hash);
163     }
164 }