All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.cdl.client.config.LocalCfg Maven / Gradle / Ivy

There is a newer version: 2.3.9
Show newest version
package com.github.javaclub.cdl.client.config;

import java.io.File;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.github.javaclub.cdl.client.util.LocalFileUtils;
import com.github.javaclub.configcenter.client.util.DESCoder;

public class LocalCfg {
	private final static String salt = "23rwfsfwer11";
	private static final Logger logger = LoggerFactory.getLogger(LocalCfg.class);
	public static Map getDBAKey(String appId,String appkey){
		try{
			File f = new File(LocalFileUtils.getLocalSaveDirPath() + "/db-" + appId);
			if (!f.exists()) {
				f.createNewFile();
				return null;
			}
			String cfg = FileUtils.readFileToString(f);
			if (StringUtils.isNotBlank(cfg)) {
				@SuppressWarnings("unchecked")
				Map map = JSONObject.parseObject(DESCoder.decrpty(cfg, appkey), Map.class);
				if (map != null && map.get("md5") != null) {
					StringBuilder sb = new StringBuilder();
					sb.append(map.get("dbaid"));
					sb.append(map.get("dbakey"));
					if (map.get("md5").equals(MD5(sb.toString() + salt))) {
						return map;
					}
				}
			}
		}catch(Exception e){
			logger.error("",e);
		}
		
		return null;
	}

	public static void saveDBAKey(Map dbaMap,String appId, String appKey){
		try{
			StringBuilder sb = new StringBuilder();
			sb.append(dbaMap.get("dbaid"));
			sb.append(dbaMap.get("dbakey"));
			dbaMap.put("md5", MD5(sb.toString() + salt));

			File f = new File(LocalFileUtils.getLocalSaveDirPath() + "/db-" + appId);
			if (!f.exists()) {
				f.createNewFile();
			}
			FileUtils.writeStringToFile(f, DESCoder.encrypt(JSONObject.toJSONString(dbaMap), appKey), false);
		}catch(Exception e){
			logger.error("",e);
		}
	}

	// 来自stackoverflow的MD5计算方法,调用了MessageDigest库函数,并把byte数组结果转换成16进制
	public static String MD5(String md5) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy