001    /**
002     * Copyright (c) 2000-2013 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.security.pacl.DoPrivileged;
020    import com.liferay.portal.kernel.util.Base64;
021    import com.liferay.portal.kernel.util.Digester;
022    import com.liferay.portal.kernel.util.StreamUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    
026    import java.io.IOException;
027    import java.io.InputStream;
028    import java.io.UnsupportedEncodingException;
029    
030    import java.nio.ByteBuffer;
031    
032    import java.security.MessageDigest;
033    import java.security.NoSuchAlgorithmException;
034    
035    import org.apache.commons.codec.binary.Hex;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Alexander Chow
040     * @author Connor McKay
041     */
042    @DoPrivileged
043    public class DigesterImpl implements Digester {
044    
045            public String digest(ByteBuffer byteBuffer) {
046                    return digest(Digester.DEFAULT_ALGORITHM, byteBuffer);
047            }
048    
049            public String digest(InputStream inputStream) {
050                    return digest(Digester.DEFAULT_ALGORITHM, inputStream);
051            }
052    
053            public String digest(String text) {
054                    return digest(Digester.DEFAULT_ALGORITHM, text);
055            }
056    
057            public String digest(String algorithm, ByteBuffer byteBuffer) {
058                    if (_BASE_64) {
059                            return digestBase64(algorithm, byteBuffer);
060                    }
061                    else {
062                            return digestHex(algorithm, byteBuffer);
063                    }
064            }
065    
066            public String digest(String algorithm, InputStream inputStream) {
067                    if (_BASE_64) {
068                            return digestBase64(algorithm, inputStream);
069                    }
070                    else {
071                            return digestHex(algorithm, inputStream);
072                    }
073            }
074    
075            public String digest(String algorithm, String... text) {
076                    if (_BASE_64) {
077                            return digestBase64(algorithm, text);
078                    }
079                    else {
080                            return digestHex(algorithm, text);
081                    }
082            }
083    
084            public String digestBase64(ByteBuffer byteBuffer) {
085                    return digestBase64(Digester.DEFAULT_ALGORITHM, byteBuffer);
086            }
087    
088            public String digestBase64(InputStream inputStream) {
089                    return digestBase64(Digester.DEFAULT_ALGORITHM, inputStream);
090            }
091    
092            public String digestBase64(String text) {
093                    return digestBase64(Digester.DEFAULT_ALGORITHM, text);
094            }
095    
096            public String digestBase64(String algorithm, ByteBuffer byteBuffer) {
097                    byte[] bytes = digestRaw(algorithm, byteBuffer);
098    
099                    return Base64.encode(bytes);
100            }
101    
102            public String digestBase64(String algorithm, InputStream inputStream) {
103                    byte[] bytes = digestRaw(algorithm, inputStream);
104    
105                    return Base64.encode(bytes);
106            }
107    
108            public String digestBase64(String algorithm, String... text) {
109                    byte[] bytes = digestRaw(algorithm, text);
110    
111                    return Base64.encode(bytes);
112            }
113    
114            public String digestHex(ByteBuffer byteBuffer) {
115                    return digestHex(Digester.DEFAULT_ALGORITHM, byteBuffer);
116            }
117    
118            public String digestHex(InputStream inputStream) {
119                    return digestHex(Digester.DEFAULT_ALGORITHM, inputStream);
120            }
121    
122            public String digestHex(String text) {
123                    return digestHex(Digester.DEFAULT_ALGORITHM, text);
124            }
125    
126            public String digestHex(String algorithm, ByteBuffer byteBuffer) {
127                    byte[] bytes = digestRaw(algorithm, byteBuffer);
128    
129                    return Hex.encodeHexString(bytes);
130            }
131    
132            public String digestHex(String algorithm, InputStream inputStream) {
133                    byte[] bytes = digestRaw(algorithm, inputStream);
134    
135                    return Hex.encodeHexString(bytes);
136            }
137    
138            public String digestHex(String algorithm, String... text) {
139                    byte[] bytes = digestRaw(algorithm, text);
140    
141                    return Hex.encodeHexString(bytes);
142            }
143    
144            public byte[] digestRaw(ByteBuffer byteBuffer) {
145                    return digestRaw(Digester.DEFAULT_ALGORITHM, byteBuffer);
146            }
147    
148            public byte[] digestRaw(String text) {
149                    return digestRaw(Digester.DEFAULT_ALGORITHM, text);
150            }
151    
152            public byte[] digestRaw(String algorithm, ByteBuffer byteBuffer) {
153                    MessageDigest messageDigest = null;
154    
155                    try {
156                            messageDigest = MessageDigest.getInstance(algorithm);
157    
158                            messageDigest.update(byteBuffer);
159                    }
160                    catch (NoSuchAlgorithmException nsae) {
161                            _log.error(nsae, nsae);
162                    }
163    
164                    return messageDigest.digest();
165            }
166    
167            public byte[] digestRaw(String algorithm, InputStream inputStream) {
168                    MessageDigest messageDigest = null;
169    
170                    try {
171                            messageDigest = MessageDigest.getInstance(algorithm);
172    
173                            byte[] buffer = new byte[StreamUtil.BUFFER_SIZE];
174    
175                            int read = 0;
176    
177                            while ((read = inputStream.read(buffer)) != -1) {
178                                    if (read > 0) {
179                                            messageDigest.update(buffer, 0, read);
180                                    }
181                            }
182                    }
183                    catch (IOException ioe) {
184                            _log.error(ioe, ioe);
185                    }
186                    catch (NoSuchAlgorithmException nsae) {
187                            _log.error(nsae, nsae);
188                    }
189                    finally {
190                            StreamUtil.cleanUp(inputStream);
191                    }
192    
193                    return messageDigest.digest();
194            }
195    
196            public byte[] digestRaw(String algorithm, String... text) {
197                    MessageDigest messageDigest = null;
198    
199                    try {
200                            messageDigest = MessageDigest.getInstance(algorithm);
201    
202                            StringBundler sb = new StringBundler(text.length * 2 - 1);
203    
204                            for (String t : text) {
205                                    if (sb.length() > 0) {
206                                            sb.append(StringPool.COLON);
207                                    }
208    
209                                    sb.append(t);
210                            }
211    
212                            String s = sb.toString();
213    
214                            messageDigest.update(s.getBytes(Digester.ENCODING));
215                    }
216                    catch (NoSuchAlgorithmException nsae) {
217                            _log.error(nsae, nsae);
218                    }
219                    catch (UnsupportedEncodingException uee) {
220                            _log.error(uee, uee);
221                    }
222    
223                    return messageDigest.digest();
224            }
225    
226            private static final boolean _BASE_64 =
227                    PropsValues.PASSWORDS_DIGEST_ENCODING.equals("base64");
228    
229            private static Log _log = LogFactoryUtil.getLog(Digester.class);
230    
231    }