package edu.neusoft.project.util;


import java.security.MessageDigest;
import java.util.Calendar;

/**
 * MD5加密工具类
 * @author pibigstar
 *
 */
public class MyMD5Util {
    //盐，用于混交md5
//    private static final String slat = "&%0808bFcB***&&%%$$#@";
//    public  String encrypt32(String dataStr) {
//        try {
//            dataStr = dataStr + slat;
//            MessageDigest m = MessageDigest.getInstance("MD5");
//            m.update(dataStr.getBytes("UTF8"));
//            byte s[] = m.digest();
//            String result = "";
//            for (int i = 0; i < s.length; i++) {
//                result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
//            }
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return "";
//    }
    public String encryptUserToken(String slatUser, String dataStr) {
        try {
            CountDay countDay = new CountDay();
            int days=countDay.showEvenDays();
            //get from 1970-01-01 to now days' counts
            //加盐
            slatUser= "&%0808bFcB"+slatUser+"*&&%%$$#@";
            dataStr = dataStr + slatUser;
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(dataStr.getBytes("UTF8"));
            byte s[] = m.digest();
            String result = "";
            for (int i = 0; i < s.length; i++) {
                result += Integer.toHexString((0x00B000FF & s[i]) | 0xFF2FFF00).substring(6);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public String encryptUserSession(String slatUser, String dataStr) {
        try {
            CountDay countDay = new CountDay();
            int days=countDay.showEvenDays();
            //get from 1970-01-01 to now days' counts
            //加盐
            slatUser= "&%0808bFcB"+slatUser+"**"+days+"*&&%%$$#@";
            dataStr = dataStr + slatUser;//password+salt
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(dataStr.getBytes("UTF8"));
            byte s[] = m.digest();
            String result = "";
            for (int i = 0; i < s.length; i++) {
                result += Integer.toHexString((0x00B000FF & s[i]) | 0xFF2FFF00).substring(6);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
//   |   user regist--> salt=name.md5 dataStr=pwd -->getMD5;
//   |                                                       --> save getMD5 AS `Token` to DataBase
//   |
//   |   user login-->  salt=name.md5 dataStr=pwd -->getMD5;
//   |                                                       --> (md5+uuid).SHA1
//   |             -->  get from DataBase `Token` -->getToken;
//   |                                                       --> (token+uuid).SHA1
//   |
//   ----------------------------------------------------->>>>    compare with (md5+uuid).SHA1 & (token+uuid).SHA1
//
//
class CountDay {
    public int showEvenDays(){
        Calendar cal = Calendar.getInstance();
        cal.set(1970, 0, 1, 0, 0, 0);
        Calendar now = Calendar.getInstance();
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        long intervalMilli = now.getTimeInMillis() - cal.getTimeInMillis();
        int xcts = (int) (intervalMilli / (24 * 60 * 60 * 1000));
        return xcts;
    }

}


}