001
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
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 }