001    /**
002     * Copyright (c) 2000-2010 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.security.MessageDigest;
027    import java.security.NoSuchAlgorithmException;
028    
029    import org.apache.commons.codec.binary.Hex;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Alexander Chow
034     */
035    public class DigesterImpl implements Digester {
036    
037            public String digest(String text) {
038                    return digest(Digester.DEFAULT_ALGORITHM, text);
039            }
040    
041            public String digest(String algorithm, String... text) {
042    
043                    if (_BASE_64) {
044                            byte[] bytes = digestRaw(algorithm, text);
045    
046                            return Base64.encode(bytes);
047                    }
048                    else {
049                            return digestHex(algorithm, text);
050                    }
051            }
052    
053            public String digestHex(String text) {
054                    return digestHex(Digester.DEFAULT_ALGORITHM, text);
055            }
056    
057            public String digestHex(String algorithm, String... text) {
058                    byte[] bytes = digestRaw(algorithm, text);
059    
060                    return Hex.encodeHexString(bytes);
061            }
062    
063            public byte[] digestRaw(String text) {
064                    return digestRaw(Digester.DEFAULT_ALGORITHM, text);
065            }
066    
067            public byte[] digestRaw(String algorithm, String... text) {
068                    MessageDigest messageDigest = null;
069    
070                    try{
071                            messageDigest = MessageDigest.getInstance(algorithm);
072    
073                            StringBundler sb = new StringBundler(text.length * 2 - 1);
074    
075                            for (String t : text) {
076                                    if (sb.length() > 0) {
077                                            sb.append(StringPool.COLON);
078                                    }
079    
080                                    sb.append(t);
081                            }
082    
083                            String s = sb.toString();
084    
085                            messageDigest.update(s.getBytes(Digester.ENCODING));
086                    }
087                    catch (NoSuchAlgorithmException nsae) {
088                            _log.error(nsae, nsae);
089                    }
090                    catch (UnsupportedEncodingException uee) {
091                            _log.error(uee, uee);
092                    }
093    
094                    return messageDigest.digest();
095            }
096    
097            private static final boolean _BASE_64 =
098                    PropsValues.PASSWORDS_DIGEST_ENCODING.equals("base64");
099    
100            private static Log _log = LogFactoryUtil.getLog(Digester.class);
101    
102    }