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