001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.Base64;
020    import com.liferay.portal.kernel.util.Digester;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    
024    import java.io.UnsupportedEncodingException;
025    
026    import java.nio.ByteBuffer;
027    
028    import java.security.MessageDigest;
029    import java.security.NoSuchAlgorithmException;
030    
031    import org.apache.commons.codec.binary.Hex;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Alexander Chow
036     * @author Connor McKay
037     */
038    public class DigesterImpl implements Digester {
039    
040            public String digest(ByteBuffer byteBuffer) {
041                    return digest(Digester.DEFAULT_ALGORITHM, byteBuffer);
042            }
043    
044            public String digest(String text) {
045                    return digest(Digester.DEFAULT_ALGORITHM, text);
046            }
047    
048            public String digest(String algorithm, ByteBuffer byteBuffer) {
049                    if (_BASE_64) {
050                            return digestBase64(algorithm, byteBuffer);
051                    }
052                    else {
053                            return digestHex(algorithm, byteBuffer);
054                    }
055            }
056    
057            public String digest(String algorithm, String... text) {
058                    if (_BASE_64) {
059                            return digestBase64(algorithm, text);
060                    }
061                    else {
062                            return digestHex(algorithm, text);
063                    }
064            }
065    
066            public String digestBase64(ByteBuffer byteBuffer) {
067                    return digestBase64(Digester.DEFAULT_ALGORITHM, byteBuffer);
068            }
069    
070            public String digestBase64(String text) {
071                    return digestBase64(Digester.DEFAULT_ALGORITHM, text);
072            }
073    
074            public String digestBase64(String algorithm, ByteBuffer byteBuffer) {
075                    byte[] bytes = digestRaw(algorithm, byteBuffer);
076    
077                    return Base64.encode(bytes);
078            }
079    
080            public String digestBase64(String algorithm, String... text) {
081                    byte[] bytes = digestRaw(algorithm, text);
082    
083                    return Base64.encode(bytes);
084            }
085    
086            public String digestHex(ByteBuffer byteBuffer) {
087                    return digestHex(Digester.DEFAULT_ALGORITHM, byteBuffer);
088            }
089    
090            public String digestHex(String text) {
091                    return digestHex(Digester.DEFAULT_ALGORITHM, text);
092            }
093    
094            public String digestHex(String algorithm, ByteBuffer byteBuffer) {
095                    byte[] bytes = digestRaw(algorithm, byteBuffer);
096    
097                    return Hex.encodeHexString(bytes);
098            }
099    
100            public String digestHex(String algorithm, String... text) {
101                    byte[] bytes = digestRaw(algorithm, text);
102    
103                    return Hex.encodeHexString(bytes);
104            }
105    
106            public byte[] digestRaw(ByteBuffer byteBuffer) {
107                    return digestRaw(Digester.DEFAULT_ALGORITHM, byteBuffer);
108            }
109    
110            public byte[] digestRaw(String text) {
111                    return digestRaw(Digester.DEFAULT_ALGORITHM, text);
112            }
113    
114            public byte[] digestRaw(String algorithm, ByteBuffer byteBuffer) {
115                    MessageDigest messageDigest = null;
116    
117                    try {
118                            messageDigest = MessageDigest.getInstance(algorithm);
119    
120                            messageDigest.update(byteBuffer);
121                    }
122                    catch (NoSuchAlgorithmException nsae) {
123                            _log.error(nsae, nsae);
124                    }
125    
126                    return messageDigest.digest();
127            }
128    
129            public byte[] digestRaw(String algorithm, String... text) {
130                    MessageDigest messageDigest = null;
131    
132                    try {
133                            messageDigest = MessageDigest.getInstance(algorithm);
134    
135                            StringBundler sb = new StringBundler(text.length * 2 - 1);
136    
137                            for (String t : text) {
138                                    if (sb.length() > 0) {
139                                            sb.append(StringPool.COLON);
140                                    }
141    
142                                    sb.append(t);
143                            }
144    
145                            String s = sb.toString();
146    
147                            messageDigest.update(s.getBytes(Digester.ENCODING));
148                    }
149                    catch (NoSuchAlgorithmException nsae) {
150                            _log.error(nsae, nsae);
151                    }
152                    catch (UnsupportedEncodingException uee) {
153                            _log.error(uee, uee);
154                    }
155    
156                    return messageDigest.digest();
157            }
158    
159            private static final boolean _BASE_64 =
160                    PropsValues.PASSWORDS_DIGEST_ENCODING.equals("base64");
161    
162            private static Log _log = LogFactoryUtil.getLog(Digester.class);
163    
164    }