Ejemplo n.º 1
0
        /// <summary>
        /// 檢驗Request的MAC
        /// </summary>
        /// <param name="msgUtility">Request Msg Parser</param>
        /// <param name="msgBytes">Origin Request byte array</param>
        /// <returns>true:success/false:failed</returns>
        protected virtual bool CheckMac(IMsgUtility msgUtility, byte[] msgBytes)
        {
            bool result = false;

            try
            {
                log.Debug(m => m("1.開始驗證MAC"));
                //reader id 取前面16個字串
                this.readerId = msgUtility.GetStr(msgBytes, "ReaderId").Substring(0, 16);
                //取交易時間
                this.transDateTime = msgUtility.GetStr(msgBytes, "TransDateTime");;
                //取後面的mac:要用來檢查data部分是否有被串改
                this.mac = msgUtility.GetStr(msgBytes, "Mac");
                //錢
                this.amount = msgUtility.GetStr(msgBytes, "Amount");
                //將data的部分取出作SHA1
                byte[] hashData   = msgUtility.GetBytes(msgBytes, "HeaderVersion", "Mac");
                byte[] hashResult = CheckMacContainer.HashWorker.ComputeHash(hashData);
                //開始檢查hash過的data部分是否與mac値一致
                result = CheckMacContainer.Mac2Manager.ChkInitMac(this.readerId, this.transDateTime, hashResult, this.mac);
                log.Debug(m => m("2.結束驗證MAC:{0}", result.ToString()));
                return(result);
            }
            catch (Exception ex)
            {
                log.Error(m => m("[State_AutoLoad][CheckMac] Error: {0} \r\n{1}", ex.Message, ex.StackTrace));
                return(false);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 依據Request設定card情報屬性値
 /// </summary>
 /// <param name="msgBytes">Origin Request byte array</param>
 /// <param name="msgUtility">Request Msg Parser</param>
 protected virtual void SetCardInfo(IMsgUtility msgUtility, byte[] msgBytes)
 {
     //Response要用的資料部分
     this.cardFormatMemberId = msgUtility.GetBytes(msgBytes, "CardFormatMemberId");
     this.cardNo             = msgUtility.GetStr(msgBytes, "CardNo");
     this.cardExpireDate1    = msgUtility.GetStr(msgBytes, "CardExpireDate1");
     this.cardExpireDate2    = msgUtility.GetStr(msgBytes, "CardExpireDate2");
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Response寫入MAC
        /// </summary>
        /// <param name="resMsgUility">Response Parser</param>
        /// <param name="response">response byte array</param>
        /// <param name="reader_id">卡機裝置ID(length:16)</param>
        /// <param name="transTime">交易時間(length:14)</param>
        protected virtual void SetMAC(IMsgUtility resMsgUility, byte[] response, string reader_id, string transTime)
        {
            // fetch sha1 data
            byte[] hashDataBytes = resMsgUility.GetBytes(response, "HeaderVersion", "Mac");
            byte[] sha1Result    = CheckMacContainer.HashWorker.ComputeHash(hashDataBytes);
            string mac           = CheckMacContainer.Mac2Manager.GetAuthMac(reader_id, transTime, sha1Result);

            resMsgUility.SetStr(mac, response, "Mac");
            log.Debug(m => m("8.Response mac: {0}", mac));
        }
Ejemplo n.º 4
0
        private string GenMac(IMsgUtility msgUtility, string data, int macLength = 8)
        {
            byte[] result = Encoding.ASCII.GetBytes(data);
            result = result.Concat(new byte[macLength]).ToArray();//insert a byte[ mac length ]
            string readerId      = msgUtility.GetStr(result, "ReaderId").Substring(0, 16);
            string transDatetime = msgUtility.GetStr(result, "TransDateTime");

            // fetch sha1 data
            byte[] hashDataBytes = msgUtility.GetBytes(result, "HeaderVersion", "Mac");
            byte[] sha1Result    = MsgContainer.HashWorker.ComputeHash(hashDataBytes);
            string macStr        = MsgContainer.Mac2Manager.GetAuthMac(readerId, transDatetime, sha1Result);

            return(macStr);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Response寫入MAC
 /// </summary>
 /// <param name="resMsgUility">Response Msg Parser</param>
 /// <param name="response">response byte array</param>
 /// <param name="reader_id">卡機裝置ID(length:16)</param>
 /// <param name="transTime">交易時間(length:14)</param>
 /// <returns></returns>
 protected virtual void SetMAC(IMsgUtility resMsgUility, byte[] response, string reader_id, string transTime)
 {
     // fetch sha1 data
     byte[] hashDataBytes = resMsgUility.GetBytes(response, "HeaderVersion", "Mac");
     byte[] sha1Result = CheckMacContainer.HashWorker.ComputeHash(hashDataBytes);
     string mac = CheckMacContainer.Mac2Manager.GetAuthMac(reader_id, transTime, sha1Result);
     resMsgUility.SetStr(mac, response, "Mac");
     log.Debug(m => m("8.Response mac: {0}", mac));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 依據Request設定card情報屬性値
 /// </summary>
 /// <param name="msgBytes">Origin Request byte array</param>
 /// <param name="msgUtility">Request Msg Parser</param>
 protected virtual void SetCardInfo(IMsgUtility msgUtility,byte[] msgBytes)
 {
     //Response要用的資料部分
     this.cardFormatMemberId = msgUtility.GetBytes(msgBytes, "CardFormatMemberId");
     this.cardNo = msgUtility.GetStr(msgBytes, "CardNo");
     this.cardExpireDate1 = msgUtility.GetStr(msgBytes, "CardExpireDate1");
     this.cardExpireDate2 = msgUtility.GetStr(msgBytes, "CardExpireDate2");
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 檢驗Request的MAC
 /// </summary>
 /// <param name="msgBytes">Origin Request byte array</param>
 /// <param name="msgUtility">Request Msg Parser</param>
 /// <returns>true:success/false:failed</returns>
 protected virtual bool CheckMac(IMsgUtility msgUtility,byte[] msgBytes)
 {
     bool result = false;
     try
     {
         log.Debug(m => m("1.開始驗證MAC"));
         //設定Card情報:驗證失敗的時候用
         this.SetCardInfo(msgUtility, msgBytes);
         //reader id 取前面16個字串
         this.readerId = msgUtility.GetStr(msgBytes, "ReaderId").Substring(0, 16);
         //取交易時間
         this.transDateTime = msgUtility.GetStr(msgBytes, "TransDateTime"); ;
         //取後面的mac:要用來檢查data部分是否有被串改
         this.mac = msgUtility.GetStr(msgBytes, "Mac");
         //將data的部分取出作SHA1
         byte[] hashData = msgUtility.GetBytes(msgBytes, "HeaderVersion", "Mac");
         byte[] hashResult = CheckMacContainer.HashWorker.ComputeHash(hashData);
         //開始檢查hash過的data部分是否與mac値一致
         result = CheckMacContainer.Mac2Manager.ChkInitMac(this.readerId, this.transDateTime, hashResult, this.mac);
         log.Debug(m => m("2.結束驗證MAC:{0}", result.ToString()));
         return result;
     }
     catch (Exception ex)
     {
         log.Error(m => m("[State_AutoLoadQuery][CheckMac] Error: {0} \r\n{1}", ex.Message, ex.StackTrace));
         return false;
     }
 }
Ejemplo n.º 8
0
 private string GenMac(IMsgUtility msgUtility,string data,int macLength = 8)
 {
     byte[] result = Encoding.ASCII.GetBytes(data);
     result = result.Concat(new byte[macLength]).ToArray();//insert a byte[ mac length ]
     string readerId = msgUtility.GetStr(result, "ReaderId").Substring(0, 16);
     string transDatetime = msgUtility.GetStr(result, "TransDateTime");
     // fetch sha1 data
     byte[] hashDataBytes = msgUtility.GetBytes(result, "HeaderVersion", "Mac");
     byte[] sha1Result = MsgContainer.HashWorker.ComputeHash(hashDataBytes);
     string macStr = MsgContainer.Mac2Manager.GetAuthMac(readerId, transDatetime, sha1Result);
     return macStr;
 }