KeyHelper.java 4.57 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
package jp.agentec.sinaburocast.helper;

import java.util.Calendar;
import java.util.Date;

import jp.agentec.sinaburocast.common.SinaburoConstant.Formats;
import jp.agentec.sinaburocast.common.exception.ExpectedException;
import jp.agentec.sinaburocast.common.util.EncryptUtil;
import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import jp.agentec.sinaburocast.vo.EmailKey;
import jp.agentec.sinaburocast.vo.EnqueteKey;

import org.apache.log4j.Logger;
import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;

@Component(instance = InstanceType.SINGLETON)
public class KeyHelper {
	private final Logger logger = Logger.getLogger(getClass());
	private final static String ENQUETE_MAIL_KEY = "YQe0JrmPqC8A";
	private final static String ENQUETE_ID_KEY = "B7iGNqDv";

	/**
	 * アンケートURL用の暗号化文字列作成
	 * 
	 * @param memberId
	 * @param enqueteId
	 * @param month
	 * @param day
	 * @param hour
	 * @return
	 * @throws Exception
	 */
	public String encrypt(Integer memberId, Integer enqueteId, int month, int day, int hour) throws Exception {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, month);
		cal.add(Calendar.DAY_OF_MONTH, day);
		cal.add(Calendar.HOUR_OF_DAY, hour);
		return encrypt(memberId, enqueteId, cal.getTime());
	}

	/**
	 * メンバーID, アンケートID, 有効期限を暗号化した文字列を返す。
	 * URLに使用される+/=はそれぞれ-_@に置換する。
	 * 
	 * @param memberId
	 * @param enqueteId
	 * @param expired
	 * @return
	 * @throws Exception
	 */
	public String encrypt(Integer memberId, Integer enqueteId, Date expired) throws Exception {
		String dateTime = SinaburoUtil.convertTimestampToString(expired, Formats.DATE_TIME);
		String orgVal = memberId + "," + enqueteId + "," + dateTime;
		String encVal = EncryptUtil.encryptBASE64(ENQUETE_MAIL_KEY, orgVal);
		return encVal.replace("+", "-").replace("/", "_").replace("=", "@");
	}

	/**
	 * 文字列を復号化し、メンバーID, アンケートID, 有効期限を含むオブジェクトを返す。
	 * 
	 * @param encryptStr
	 * @return
	 * @throws Exception
	 */
	public EnqueteKey decrypt(String encryptStr) throws Exception {
		if (encryptStr == null) {
			throw new ExpectedException("argument is null");
		}
		try {
			String orgVal = EncryptUtil.decryptBASE64(ENQUETE_MAIL_KEY, encryptStr.replace("-", "+").replace("_", "/").replace("@", "="));
			return new EnqueteKey(orgVal);
		} catch (Exception e) {
			logger.error("Decrypt failed. " + e.toString());
			throw new ExpectedException("Invalid String");
		}
	}

	/**
	 * ユーザ登録用の暗号化文字列作成
	 * 
	 * @param email
	 * @param month
	 * @param day
	 * @param hour
	 * @return
	 * @throws Exception
	 */
	public String encryptEmail(String email, int month, int day, int hour) throws Exception {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, month);
		cal.add(Calendar.DAY_OF_MONTH, day);
		cal.add(Calendar.HOUR_OF_DAY, hour);
		return encryptEmail(email, cal.getTime());
	}

	/**
	 * メンバーID, アンケートID, 有効期限を暗号化した文字列を返す。
	 * URLに使用される+/はそれぞれ-_に置換する。
	 * 末尾の==は除去
	 * 
	 * @param memberId
	 * @param enqueteId
	 * @param expired
	 * @return
	 * @throws Exception
	 */
	public String encryptEmail(String email, Date expired) throws Exception {
		String dateTime = SinaburoUtil.convertTimestampToString(expired, Formats.DATE_TIME);
		String orgVal = email + "," + dateTime;
		String encVal = EncryptUtil.encryptBASE64(ENQUETE_MAIL_KEY, orgVal);
		return encVal.replace("+", "-").replace("/", "_").replace("=", "!");
	}

	/**
	 * 文字列を復号化し、メンバーID, アンケートID, 有効期限を含むオブジェクトを返す。
	 * 
	 * @param encryptStr
	 * @return
	 * @throws Exception
	 */
	public EmailKey decryptEmail(String encryptStr) throws Exception {
		if (encryptStr == null) {
			throw new ExpectedException("argument is null");
		}
		try {
			String orgVal = EncryptUtil.decryptBASE64(ENQUETE_MAIL_KEY, encryptStr.replace("-", "+").replace("_", "/").replace("!", "="));
			return new EmailKey(orgVal);
		} catch (Exception e) {
			logger.error("Decrypt failed. " + e.toString());
			throw new ExpectedException("Invalid String");
		}
	}

	public String encryptEnqueteId(Integer enqueteId) throws Exception {
		return EncryptUtil.encryptBASE64(ENQUETE_ID_KEY, "" + enqueteId);
	}

	public String decryptEnqueteId(String encryptStr) throws Exception {
		return EncryptUtil.decryptBASE64(ENQUETE_ID_KEY, encryptStr);
	}
}