1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.Base64;
20  import com.liferay.portal.kernel.util.Digester;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  
24  import java.io.UnsupportedEncodingException;
25  
26  import java.security.MessageDigest;
27  import java.security.NoSuchAlgorithmException;
28  
29  import org.apache.commons.codec.binary.Hex;
30  
31  /**
32   * <a href="DigesterImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Alexander Chow
36   */
37  public class DigesterImpl implements Digester {
38  
39      public String digest(String text) {
40          return digest(Digester.DEFAULT_ALGORITHM, text);
41      }
42  
43      public String digest(String algorithm, String... text) {
44  
45          if (_BASE_64) {
46              byte[] bytes = digestRaw(algorithm, text);
47  
48              return Base64.encode(bytes);
49          }
50          else {
51              return digestHex(algorithm, text);
52          }
53      }
54  
55      public String digestHex(String text) {
56          return digestHex(Digester.DEFAULT_ALGORITHM, text);
57      }
58  
59      public String digestHex(String algorithm, String... text) {
60          byte[] bytes = digestRaw(algorithm, text);
61  
62          return Hex.encodeHexString(bytes);
63      }
64  
65      public byte[] digestRaw(String text) {
66          return digestRaw(Digester.DEFAULT_ALGORITHM, text);
67      }
68  
69      public byte[] digestRaw(String algorithm, String... text) {
70          MessageDigest messageDigest = null;
71  
72          try{
73              messageDigest = MessageDigest.getInstance(algorithm);
74  
75              StringBundler sb = new StringBundler(text.length * 2 - 1);
76  
77              for (String t : text) {
78                  if (sb.length() > 0) {
79                      sb.append(StringPool.COLON);
80                  }
81  
82                  sb.append(t);
83              }
84  
85              String s = sb.toString();
86  
87              messageDigest.update(s.getBytes(Digester.ENCODING));
88          }
89          catch (NoSuchAlgorithmException nsae) {
90              _log.error(nsae, nsae);
91          }
92          catch (UnsupportedEncodingException uee) {
93              _log.error(uee, uee);
94          }
95  
96          return messageDigest.digest();
97      }
98  
99      private static final boolean _BASE_64 =
100         PropsValues.PASSWORDS_DIGEST_ENCODING.equals("base64");
101 
102     private static Log _log = LogFactoryUtil.getLog(Digester.class);
103 
104 }