/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.utils;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

/**
 * @author 田平 create 2016年2月5日下午1:03:30
 */
public class JasyptEncryptorUtil {
	public static final String configUsername = "micro";
	public static final String configPassword = "{cipher}FaCXGfda384lhZ55WhcFrQ==";// micro/micro,.

	public static final String adminUsername = "admin";
	public static final String adminPassword = "{cipher}kcQQQRDU3aLUlgHoLH10Ew==";// admin/admin,.

	public final static String encryptPrefix = "{cipher}";
	private final static String JASYPT_PASSWORD = "cruniimicro,.";
	private final static String JASYPT_ALGORITHM = "PBEWithMD5AndDES";
	// spring cloud config client 客户端使用的用户名密码
	public static final String keyConfigUsername = "spring.cloud.config.username";
	public static final String keyConfigPassword = "spring.cloud.config.password";

	// spring security baseAuth 的用户名密码
	public static final String keySecurityUsername = "spring.security.user.name";
	public static final String keySecurityPassword = "spring.security.user.password";

	// spring admin client 客户端使用的用户名密码
	public static final String keyAdminUsername = "spring.boot.admin.client.username";
	public static final String keyAdminPassword = "spring.boot.admin.client.password";

	private static StandardPBEStringEncryptor encryptor;
	static {
		encryptor = new StandardPBEStringEncryptor();
		encryptor.setAlgorithm(JASYPT_ALGORITHM);
		encryptor.setPassword(JASYPT_PASSWORD);
	}

	public static String encrypt(String message) {
		return encryptor.encrypt(message);
	}

	public static String decrypt(String encryptedText) {
		return encryptor.decrypt(encryptedText);
	}

	public static boolean isEncryptedValue(String value) {
		if (!"".equals(value)) {
			return value.startsWith(encryptPrefix);
		} else {
			return false;
		}
	}

	

	public static void setConfigSecurityProps() {
		String realPassword = decrypt(configPassword.substring(encryptPrefix.length()));
		System.setProperty(keySecurityUsername, configUsername);
		System.setProperty(keySecurityPassword, realPassword);
	}
	
	public static void setConfigClientProps() {
		String realPassword = decrypt(configPassword.substring(encryptPrefix.length()));
		System.setProperty(keyConfigUsername, configUsername);
		System.setProperty(keyConfigPassword, realPassword);
	}
	
	public static void setAdminSecurityProps() {
		String realPassword = decrypt(adminPassword.substring(encryptPrefix.length()));
		System.setProperty(keySecurityUsername, adminUsername);
		System.setProperty(keySecurityPassword, realPassword);
	}

	public static void setAdminClientProps() {
		String realPassword = decrypt(adminPassword.substring(encryptPrefix.length()));
		System.setProperty(keyAdminUsername, adminUsername);
		System.setProperty(keyAdminPassword, realPassword);
	}

	public static void main(String[] args) {
		System.out.println(encryptor.decrypt("80nSCCjoQULHFRa3vWg5XRTtJNECW9Mc"));
		System.out.println(encryptor.encrypt("rootROOT@123,."));
		System.out.println(encryptor.encrypt("ADMIN"));
	}
}
