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    
022    import java.io.UnsupportedEncodingException;
023    
024    import java.security.MessageDigest;
025    import java.security.NoSuchAlgorithmException;
026    
027    import org.apache.commons.codec.binary.Hex;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class DigesterImpl implements Digester {
033    
034            public String digest(String text) {
035                    return digest(Digester.DIGEST_ALGORITHM, text);
036            }
037    
038            public String digest(String algorithm, String text) {
039                    byte[] bytes = digestRaw(algorithm, text);
040    
041                    if (_BASE_64) {
042                            return Base64.encode(bytes);
043                    }
044                    else {
045                            return new String(Hex.encodeHex(bytes));
046                    }
047            }
048    
049            public byte[] digestRaw(String text) {
050                    return digestRaw(Digester.DIGEST_ALGORITHM, text);
051            }
052    
053            public byte[] digestRaw(String algorithm, String text) {
054                    MessageDigest messageDigest = null;
055    
056                    try{
057                            messageDigest = MessageDigest.getInstance(algorithm);
058    
059                            messageDigest.update(text.getBytes(Digester.ENCODING));
060                    }
061                    catch (NoSuchAlgorithmException nsae) {
062                            _log.error(nsae, nsae);
063                    }
064                    catch (UnsupportedEncodingException uee) {
065                            _log.error(uee, uee);
066                    }
067    
068                    return messageDigest.digest();
069            }
070    
071            private static final boolean _BASE_64 =
072                    PropsValues.PASSWORDS_DIGEST_ENCODING.equals("base64");
073    
074            private static Log _log = LogFactoryUtil.getLog(Digester.class);
075    
076    }