
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
package froala.editor;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import egovframework.rte.fdl.cmmn.exception.EgovBizException;
/**
* Basic utils.
*
* @author florin@froala.com
*
*/
public final class Utils {
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
/**
* Private constructor.
*/
private Utils() {
}
public static String generateUniqueString() throws NoSuchAlgorithmException {
String miliseconds = System.currentTimeMillis() + "";
MessageDigest d = null;
d = java.security.MessageDigest.getInstance("SHA-256");
d.reset();
d.update(miliseconds.getBytes());
return bytesToHex(d.digest());
}
public static String bytesToHex(byte[] in) {
StringBuilder builder = new StringBuilder();
for (byte b : in) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
// public static String getFileName(Part part) {
// for (String cd : part.getHeader("content-disposition").split(";")) {
// if (cd.trim().startsWith("filename")) {
// return cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
// }
// }
// return null;
// }
public static String getFileName(MultipartFile file) {
if( !file.isEmpty() ) {
return file.getOriginalFilename();
};
return null;
}
public static byte[] hmac(byte[] key, String data) {
Mac sha256_HMAC;
byte[] result = null;
try {
sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key, "HmacSHA256");
sha256_HMAC.init(secret_key);
result = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException | InvalidKeyException | IllegalStateException | UnsupportedEncodingException e) {
return null;
}
return result;
}
public static String hmac_hex(byte[] key, String data) throws EgovBizException {
return new String(Hex.encodeHex(hmac(key, data)));
}
public static String getAddDateRoute(String route) {
if( route == null ) return "";
// 파일 저장 경로에 날짜 추가
Calendar cal = Calendar.getInstance();
//현재 년도, 월, 일
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int date = cal.get(Calendar.DATE);
return route + year + "/" + month + "/" + date + "/";
}
public static String getAddTempRoute(String route) {
if( route == null ) return "";
// 파일 저장 경로에 임시경로 추가
String directory = null;
try {
directory = Utils.generateUniqueString();
} catch (NoSuchAlgorithmException e) {
LOGGER.error("NoSuchAlgorithmException! {}", e);
directory = "";
}
return route + directory + "/";
}
}