Skip to content

Commit

Permalink
Merge pull request #14 from support-project/develop
Browse files Browse the repository at this point in the history
Release v1.5.0
  • Loading branch information
koda-masaru authored Aug 5, 2016
2 parents 9c1f48a + 54d21f5 commit cae3328
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 84 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.support-project</groupId>
<artifactId>common</artifactId>
<version>1.4.0</version>
<version>1.5.0</version>
<packaging>jar</packaging>

<name>common</name>
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/support/project/common/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.support.project.common.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Random;

import javax.xml.bind.DatatypeConverter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.common.util.FileUtil;
import org.support.project.common.util.StringUtils;
import org.support.project.common.util.SystemUtils;
import org.w3c.dom.Document;
Expand All @@ -18,6 +25,12 @@
* @author Koda
*/
public class AppConfig {
/** ログ */
private static final Log LOG = LogFactory.getLog(AppConfig.class);

/** Sync用オブジェクト */
public static final Object sync = new Object();

/** 設定ファイルのパス */
public static final String APP_CONFIG = "/appconfig.xml";
/** インスタンス */
Expand Down Expand Up @@ -87,6 +100,10 @@ public AppConfig() {
/** ユーザのホームディレクトリ(BasePath)を指定する環境変数のキー */
private static String envKey = "";

/** 暗号化キー */
private static String key = null;


/**
* 環境変数のキー文字列を設定
* @param envKey env key
Expand Down Expand Up @@ -248,5 +265,35 @@ public String getLogsPath() {
public void setLogsPath(String logsPath) {
this.logsPath = logsPath;
}
/**
* Get key
* @return the key
*/
public String getKey() {
synchronized (AppConfig.sync) {
if (StringUtils.isEmpty(AppConfig.key)) {
try {
File keyTxt = new File(AppConfig.get().getBasePath(), "key.txt");
if (keyTxt.exists()) {
LOG.info("Load key file: " + keyTxt.getAbsolutePath());
AppConfig.key = FileUtil.read(new FileInputStream(keyTxt), "UTF-8");
} else {
LOG.info("Generate key and write " + keyTxt.getAbsolutePath());
Random randomno = new Random();
byte[] nbyte = new byte[32];
randomno.nextBytes(nbyte);
String genKey = new String(DatatypeConverter.printHexBinary(nbyte));
FileUtil.write(keyTxt, genKey);
AppConfig.key = FileUtil.read(new FileInputStream(keyTxt), "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1); // システム終了
}
}
// LOG.trace("Key: " + AppConfig.key);
}
return AppConfig.key;
}

}
4 changes: 4 additions & 0 deletions src/main/java/org/support/project/common/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public static void copy(InputStream inputStream, OutputStream outputStream) thro
public static void write(File file, String string) throws FileNotFoundException, UnsupportedEncodingException, IOException {
PrintWriter pw = null;
try {
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
// 出力ストリームの生成(文字コード指定)
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
Expand Down
51 changes: 34 additions & 17 deletions src/main/java/org/support/project/common/util/PasswordUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.support.project.common.config.AppConfig;
import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;

/**
* パスワードのエンコード/デコードに利用するユーティリティ
*
* CBC(IVを使用)イニシャルバリュー(IV(アイブイ))は使っていない。
*
* @author koda
*
*/
public class PasswordUtil {
/** ログ */
private static final Log LOG = LogFactory.getLog(PasswordUtil.class);

/** ALGORITHM */
private static final String CIPHER_ALGORITHM = "AES";

// private static final String CIPHER_TRANSFORMATION = CIPHER_ALGORITHM + "/CBC/PKCS5Padding";

/**
Expand All @@ -32,9 +34,19 @@ public class PasswordUtil {
* @throws NoSuchAlgorithmException NoSuchAlgorithmException
*/
private static byte[] generatSecretKey(String key) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hash = digest.digest(key.getBytes());
return hash;
synchronized (CIPHER_ALGORITHM) {
MessageDigest digest = MessageDigest.getInstance("MD5");
StringBuilder builder = new StringBuilder();
// LOG.trace(AppConfig.get().getKey());
builder.append(key).append(AppConfig.get().getKey());
int hashIterations = 100;
byte[] hash = digest.digest(builder.toString().getBytes());
for (int i = 0; i < hashIterations; i++) {
hash = digest.digest(hash);
}
// LOG.trace(hash);
return hash;
}
}
/**
* hash on sha 256.
Expand Down Expand Up @@ -78,7 +90,7 @@ public static final String encrypt(String string, String key)
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(string.getBytes());

return Base64Utils.toBase64(bytes);
}

Expand All @@ -99,14 +111,19 @@ public static final String decrypt(String string, String key)
if (string == null) {
return null;
}
Key secretKey = makeKey(generatSecretKey(key));

Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);

byte[] bytes = Base64Utils.fromBase64(string);
byte[] dec = cipher.doFinal(bytes);
return new String(dec);
try {
Key secretKey = makeKey(generatSecretKey(key));

Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);

byte[] bytes = Base64Utils.fromBase64(string);
byte[] dec = cipher.doFinal(bytes);
return new String(dec);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
LOG.warn("decrypt error: " + e.getMessage());
throw e;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void testPrintLogLevelObjectThrowable() {
log.error("error");
log.fatal("fatal");

/*
AppConfig config = new AppConfig();
config.setTime_zone("aaa");
log.fatal(config);
Expand Down Expand Up @@ -67,7 +68,7 @@ public void testPrintLogLevelObjectThrowable() {
if (log.isFatalEnabled()) {
log.fatal(config);
}

*/

}

Expand Down
122 changes: 60 additions & 62 deletions src/test/java/org/support/project/common/log/Log4jLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,70 @@

public class Log4jLogTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() throws Exception {
}
@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}
@After
public void tearDown() throws Exception {
}

@Test
public void testLog4jLogImpl() {
Log4jLogInitializerImpl initializer = new Log4jLogInitializerImpl();
Log log = initializer.createLog(Log4jLogTest.class);
log.trace("trace");
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
log.fatal("fatal");

AppConfig config = new AppConfig();
config.setTime_zone("aaa");
log.trace(config);
log.debug(config);
log.info(config);
log.warn(config);
log.error(config);
log.fatal(config);


log.trace(config, new Exception("hoge"));
log.debug(config, new Exception("hoge"));
log.info(config, new Exception("hoge"));
log.warn(config, new Exception("hoge"));
log.error(config, new Exception("hoge"));
log.fatal(config, new Exception("hoge"));

if (log.isTraceEnabled()) {
log.trace(config);
}
if (log.isDebugEnabled()) {
log.debug(config);
}
if (log.isInfoEnabled()) {
log.info(config);
}
if (log.isWarnEnabled()) {
log.warn(config);
}
if (log.isErrorEnabled()) {
log.error(config);
}
if (log.isFatalEnabled()) {
log.fatal(config);
}

@Test
public void testLog4jLogImpl() {
// 目視確認
Log4jLogInitializerImpl initializer = new Log4jLogInitializerImpl();
Log log = initializer.createLog(Log4jLogTest.class);
log.trace("trace");
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
log.fatal("fatal");



}
/*
AppConfig config = new AppConfig();
config.setTime_zone("aaa");
log.trace(config);
log.debug(config);
log.info(config);
log.warn(config);
log.error(config);
log.fatal(config);
log.trace(config, new Exception("hoge"));
log.debug(config, new Exception("hoge"));
log.info(config, new Exception("hoge"));
log.warn(config, new Exception("hoge"));
log.error(config, new Exception("hoge"));
log.fatal(config, new Exception("hoge"));
if (log.isTraceEnabled()) {
log.trace(config);
}
if (log.isDebugEnabled()) {
log.debug(config);
}
if (log.isInfoEnabled()) {
log.info(config);
}
if (log.isWarnEnabled()) {
log.warn(config);
}
if (log.isErrorEnabled()) {
log.error(config);
}
if (log.isFatalEnabled()) {
log.fatal(config);
}
*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public void testSearch() {

ClobTableEntity entity = new ClobTableEntity();
entity.setContents("1234567890ABCDEFGH hoge IJKLMNOPQRSTUVWXYZ");
log.info(entity);
log.debug(entity);
entity = dao.insert(entity);
log.info(entity);
log.debug(entity);
Assert.assertNotNull(entity.getNo());

List<ClobTableEntity> finds = dao.searchContent("%hoge%");
log.info(finds);
log.debug(finds);
org.junit.Assert.assertFalse(finds.isEmpty());

// 少なくともH2 Databaseではtextのカラムを検索出来るようだ
Expand Down

0 comments on commit cae3328

Please sign in to comment.