package com.kxs.adminap.util;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.tomcat.util.codec.binary.Base64;

public class Util {
    public static String CheckInt(Object str) {
        if(str instanceof Integer) {
            return str.toString();
        }
        return "0";
    }

    public static String CheckNull(String str) {
        if(str.isEmpty() || str == null) {
            return "";
        }
        return str;
    }

    public static Boolean IsNullOrEmpty(String str) {
        if(str.isEmpty() || str == null) {
            return false;
        }
        return true;
    }

    public static Date AddMonth(Date now, Integer month) {
        Calendar rightNow = Calendar.getInstance();  
        rightNow.setTime(now);  
        rightNow.add(Calendar.MONTH, month);  
        Date dt = rightNow.getTime();
        return dt;
    }

    public static String DateFormat(Date date, String patten) {
        return new SimpleDateFormat(patten).format(date);
    }

    public static String MD532(String str) {
        String result = str;
        try {
            // 加密对象，指定加密方式
            MessageDigest md5 = MessageDigest.getInstance("md5");
            // 准备要加密的数据
            byte[] b = result.getBytes();
            // 加密
            byte[] digest = md5.digest(b);
            // 十六进制的字符
            char[] chars = new char[] { '0', '1', '2', '3', '4', '5',
                 '6', '7' , '8', '9', 'A', 'B', 'C', 'D', 'E','F' };
            StringBuffer sb = new StringBuffer();
            // 处理成十六进制的字符串(通常)
            for (byte bb : digest) {
                sb.append(chars[(bb >> 4) & 15]);
                sb.append(chars[bb & 15]);
            }
            result = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}