| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359 |
- using CommonObj.lotoModel;
- using CommonObj.model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Security.Cryptography;
- using System.Text;
- using System.Xml;
- namespace CommonObj.common
- {
- public class CommonFunction_test
- {
- public static string getGuiId()
- {
- string _key = Guid.NewGuid().ToString();
- return _key;
- }
- public static tokenObj createToken(redisConnection _redis,string user,string pass, string channel,string type,string msisdn,string role)
- {
- tokenObj _result = new tokenObj();
- string _key = Guid.NewGuid().ToString();
- string _token = CustomEncryption.Encrypt(_key, _key);
- _result.key = _key;
- _result.token = _token;
- _result.users = user;
- _result.password = pass;
- _result.channel = channel;
- _result.type = type;
- _result.msisdn = msisdn;
- _result.role = role;
- //ghi lai token vao DB(1)
- int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT; //10 phút
- try
- {
- int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- if (timeout_temp > 0) _token_timeout = timeout_temp;
- }
- catch
- { }
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setByKey(_redis.DB_INDEX_TOKEN, _token, _result.ToString(), _timeSpam);
- return _result;
- }
- public static bool dellToken(redisConnection _redis, string _token)
- {
- _redis.delByKey(_redis.DB_INDEX_TOKEN, _token);
- return true;
- }
- public static bool setToken(redisConnection _redis, tokenObj _tokenObj)
- {
- //ghi lai token vao DB(1)
- int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setByKey(_redis.DB_INDEX_TOKEN, _tokenObj.token, _tokenObj.ToString(), _timeSpam);
- return true;
- }
- public static tokenObj createToken(redisConnection _redis, string user, string pass, string channel, string type, string msisdn, string role,object _obj)
- {
- tokenObj _result = new tokenObj();
- string _key = Guid.NewGuid().ToString();
- string _token = CustomEncryption.Encrypt(_key, _key);
- _result.key = _key;
- _result.token = _token;
- _result.users = user;
- _result.password = pass;
- _result.channel = channel;
- _result.type = type;
- _result.msisdn = msisdn;
- _result.role = role;
- _result.obj = _obj;
- //ghi lai token vao DB(1)
- int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT; //10 phút
- try
- {
- _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- }
- catch
- {}
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setByKey(_redis.DB_INDEX_TOKEN, _token, _result.ToString(), _timeSpam);
- return _result;
- }
- //Ham nay check token xem co bi sua doi gi ko? true
- public static string decryptToken(string token, string key, log4net.ILog _log)
- {
- string result = "";
- try
- {
- if (string.IsNullOrEmpty(token))
- return result;
- result = CustomEncryption.Decrypt(token, key);
-
- }
- catch (Exception ex)
- {
- _log.Info("Loi khi check token: " + ex.Message, ex);
-
- }
- _log.Info("Decrypt toke: "+ token +" result:" + result);
- return result;
- }
- //Kiem tra xem token nay co phai la mot partner hop phap hay ko?
- //token sau deco thanh cong se co dang
- //select a.PARTNER_CODE||'_'||a.CODE||'_'||a.USERS_NAME||'_'||a.PASSWORD from LOTO_CHANNEL a
- public static bool checkPartnerToken(string token,string channel,string clientIp,redisConnection _redis, log4net.ILog _log)
- {
- bool result = false; //check token ko thanh cong
- try
- {
- //Lay ve key Encrypt thong tin cua partner
- string _keyEncryptPartner = getParamObjFromRedis(_redis, "KEY_ENCRYPT", "KEY_ENCRYPT", "SYSTEM").values;
- string _plaintext = decryptToken(token, _keyEncryptPartner, _log);
- _log.Info("checkPartnerToken: _keyEncryptPartner: " + _keyEncryptPartner + " _plaintext:" + _plaintext);
- if (string.IsNullOrEmpty(_plaintext))
- {
- result= false;
- }
-
- else
- {
- //Neu deco thanh cong se tien hanh boc tach thong tin
- string partnerCode = _plaintext.Split('_')[0];
- string code = _plaintext.Split('_')[1];
- string user = _plaintext.Split('_')[2];
- string pass = _plaintext.Split('_')[3];
- _log.Info("checkPartnerToken: partnerCode: " + partnerCode + " code:" + code + " user:" + user + " pass:" + pass);
- //Lay ve channel cua partner tuong ung de so sanh ket qua
- channelObj _channelObj = getChannelObjFromRedis(_redis, code);
- if(_channelObj.partnerCode== partnerCode && _channelObj.usersName==user && _channelObj.password==pass && code== channel)
- {
- //tiep tuc check IP
- if (_channelObj.listIp == "*")
- result = true;
- else
- {
- if (_channelObj.listIp.IndexOf("," + clientIp + ",") > 00)
- result = true;
- else
- result = false;
- }
- }
- else
- {
- _log.Info("_channelObj.partnerCode== partnerCode && _channelObj.usersName==user && _channelObj.password==pass && code== channel bi sau. Hay kiem tra lai " );
- result = false;
- }
- }
- }
- catch (Exception ex)
- {
- _log.Info("Loi khi check token partner: " + ex.Message, ex);
- result = false;
- }
- _log.Info("Ket qua check checkPartnerToken : " + result);
- return result;
- }
- //Ham nay check token xem co bi sua doi gi ko?
- public static bool firstCheckToken(tokenObj _tokenObj,string token, log4net.ILog _log)
- {
- bool result = false; //check token ko thanh cong
- //Lay ve channel tuong ung
- try
- {
- string giaima_key = CustomEncryption.Decrypt(token,_tokenObj.key);
- if (string.IsNullOrEmpty(giaima_key))
- {
- _log.Info("Token :" + token + "da bi sua doi roi nhe. Co hacker");
- result = false;
- }
-
- else
- {
- if (giaima_key == _tokenObj.key)
- result = true;
- else
- result = false;
- }
-
- }
- catch (Exception ex)
- {
- _log.Info("Loi khi check token: "+ex.Message,ex);
- result = false;
- }
- return result;
- }
- public static bool firstCheckToken(tokenObj _tokenObj, string token)
- {
- bool result = false; //check token ko thanh cong
- //Lay ve channel tuong ung
- try
- {
- string giaima_key = CustomEncryption.Decrypt(token, _tokenObj.key);
- if (string.IsNullOrEmpty(giaima_key))
- {
- //_log.Info("Token :" + token + "da bi sua doi roi nhe. Co hacker");
- result = false;
- }
- else
- {
- if (giaima_key == _tokenObj.token)
- result = true;
- else
- result = false;
- }
- }
- catch (Exception ex)
- {
- //_log.Info("Loi khi check token: "+ex.Message,ex);
- result = false;
- }
- return result;
- }
- //ham nay kiem tra token co hop le hay ko?
- public static bool checkToken(string clientIp, string token,string channel, redisConnection _redis, log4net.ILog _log)
- {
- bool result = false; //check token ko thanh cong
- try
- {
- //1.Kiem tra xem token nay co phai cua partner nao ko?
- //- Neu thuc su cua partner thi tien thanh kiem tra channel,partnercode, user, pass va clientIp
- //- Neu ko phai cua partner thi tiep tuc kiem tra token nay co phai la mot login nao ko?
- //
- bool isPartner = checkPartnerToken(token, channel, clientIp, _redis, _log);
- if (isPartner)
- {
- _log.Info("the token: " + token + "la mot partner va da xac thuc thong tin dung");
- result = true;
- return result;
- }
- else
- {
- //truong ho nay se check token nay co phai la mot login hop le ko?
- if (!_redis.existsByKey(_redis.DB_INDEX_TOKEN, token))
- {
- //Truong hop nay kenh channel nay ko authen
- result = false;
- _log.Info("The token: " + token + " is not exits in redis select 1");
- return result;
- }
- tokenObj _token = JsonConvert.DeserializeObject<tokenObj>(_redis.getByKey(_redis.DB_INDEX_TOKEN, token));
- string plaintext = decryptToken(token, _token.key,_log);
- if (string.IsNullOrEmpty(plaintext))
- {
- result = false;
- _log.Info("The token: " + token + " have change");
- _redis.delByKey(_redis.DB_INDEX_TOKEN, token);
- return result;
-
- }
- else
- {
- if (plaintext == _token.key && channel == _token.channel)
- {
- result = true;
- //truong hop xac thuc thanh cong thi se gia han them thoi gian
- //int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT;//10 phút
- try
- {
- int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- if (timeout_temp > 0) _token_timeout = timeout_temp;
- }
- catch
- { }
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setKeyTimeOut(_redis.DB_INDEX_TOKEN, token, _timeSpam);
- return result;
- }
- else
- {
- result = false;
- _log.Info("Decode token success but content invalid");
- return result;
- }
-
- }
- }
- }
- catch (Exception ex)
- {
- _log.Info("Lôi khi checkToken "+ex.Message,ex);
- }
- _log.Info("Ket qua Authen " + result );
- return result;
- }
- //ham nay kiem tra token co hop le hay ko?
- //Nhu vay la neu out _outTokenObj ma null thi do la check token cua mot Partner local
- //Neu out _outTokenObj !=null thi do la mot token xac thuc tu Internet
- public static bool checkToken(string clientIp, string token, string channel, redisConnection _redis, log4net.ILog _log,out tokenObj _outTokenObj)
- {
- bool result = false; //check token ko thanh cong
- _outTokenObj = null;
- try
- {
- //1.Kiem tra xem token nay co phai cua partner nao ko?
- //- Neu thuc su cua partner thi tien thanh kiem tra channel,partnercode, user, pass va clientIp
- //- Neu ko phai cua partner thi tiep tuc kiem tra token nay co phai la mot login nao ko?
- //
- bool isPartner = checkPartnerToken(token, channel, clientIp, _redis, _log);
- if (isPartner)
- {
- _log.Info("the token: " + token + "la mot partner va da xac thuc thong tin dung");
- result = true;
- return result;
- }
- else
- {
- //truong ho nay se check token nay co phai la mot login hop le ko?
- if (!_redis.existsByKey(_redis.DB_INDEX_TOKEN, token))
- {
- //Truong hop nay kenh channel nay ko authen
- result = false;
- _log.Info("The token: " + token + " is not exits in redis select 1");
- return result;
- }
- tokenObj _token = JsonConvert.DeserializeObject<tokenObj>(_redis.getByKey(_redis.DB_INDEX_TOKEN, token));
- string plaintext = decryptToken(token, _token.key, _log);
- if (string.IsNullOrEmpty(plaintext))
- {
- result = false;
- _log.Info("The token: " + token + " have change");
- _redis.delByKey(_redis.DB_INDEX_TOKEN, token);
- return result;
- }
- else
- {
- if (plaintext == _token.key && channel==_token.channel)
- {
- result = true;
- //truong hop xac thuc thanh cong thi se gia han them thoi gian
- //int _token_timeout = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- int _token_timeout = _redis.TOKEN_TIMEOUT_DEFAULT;//10 phút
- try
- {
- int timeout_temp = Convert.ToInt16(((paramObj)getParamObjFromRedis(_redis, "TOKEN_TIME_OUT", "TOKEN_TIME_OUT", "SYSTEM")).values);
- if (timeout_temp > 0) _token_timeout = timeout_temp;
- }
- catch
- { }
- TimeSpan _timeSpam = new TimeSpan(0, 0, _token_timeout);
- _redis.setKeyTimeOut(_redis.DB_INDEX_TOKEN, token, _timeSpam);
- _outTokenObj = _token;
- return result;
- }
- else
- {
- result = false;
- _log.Info("Decode token success but content invalid");
- return result;
- }
- }
- }
- }
- catch (Exception ex)
- {
- _log.Info("Lôi khi checkToken " + ex.Message, ex);
- }
- _log.Info("Ket qua Authen " + result);
- return result;
- }
- public static paramObj getParamObjFromRedis(redisConnection _redis, string code,string codeGroup,string channel)
- {
- paramObj result = new paramObj();
-
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "PARAM_" + code + "_" + codeGroup + "_" + channel);
- result = JsonConvert.DeserializeObject<paramObj>(_objSting);
- }
- catch (Exception ex)
- {
-
- }
- return result;
- }
- ////KEY="BLACK_"+TYPE+"_"+SV_ID+"_"+MSISDN
- //--1= Nhan vien,2=Blacklist all,3=blacklist theo dich vu,4=whitelist theo dich vu
- public static blacklistObj getBlackListFromRedis(redisConnection _redis, string type,string serviceId,string msisdn)
- {
- blacklistObj result = new blacklistObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_BLACKLIST, "BLACK_" + type + "_" + serviceId + "_" + msisdn);
- if (string.IsNullOrEmpty(_objSting)) return null;
- result = JsonConvert.DeserializeObject<blacklistObj>(_objSting);
- }
- catch (Exception ex)
- {
- }
- return result;
- }
- public static channelObj getChannelObjFromRedis(redisConnection _redis, string channel)
- {
- channelObj result = new channelObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "CHANNEL_" + channel);
- result = JsonConvert.DeserializeObject<channelObj>(_objSting);
- }
- catch (Exception ex)
- {
- }
- return result;
- }
- public static termObj getTermObjObjFromRedis(redisConnection _redis, string gameId)
- {
- termObj result = new termObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "TERM_" + gameId);
- result = JsonConvert.DeserializeObject<termObj>(_objSting);
- }
- catch (Exception ex)
- {
- }
- return result;
- }
- public static string getErrCodeObjFromRedis(redisConnection _redis, string channel,string errCode,string langauge)
- {
- //string _channelTemp = channel;
- string _channelTemp = "WEB";
- string _language = "0";
- if (!string.IsNullOrEmpty(langauge))
- _language = langauge;
- string _result = "";
- errCodeObj _obj = new errCodeObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "ERRCODE_" + _channelTemp + "_"+errCode);
- _obj = JsonConvert.DeserializeObject<errCodeObj>(_objSting);
- if (_language == "0")
- _result = _obj.msg0;
- else if (_language == "1")
- _result = _obj.msg1;
- else if (_language == "2")
- _result = _obj.msg2;
- else if (_language == "3")
- _result = _obj.msg3;
- else if (_language == "4")
- _result = _obj.msg4;
- else if (_language == "5")
- _result = _obj.msg5;
- else
- _result = _obj.msg0;
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- public static string getErrCodeObjFromRedis(redisConnection _redis, string channel, string errCode)
- {
- string _result = "";
- errCodeObj _obj = new errCodeObj();
- try
- {
- string _objSting = _redis.getByKey(_redis.DB_INDEX_PARAM_CAMPAIGN, "ERRCODE_" + channel + "_" + errCode);
- _obj = JsonConvert.DeserializeObject<errCodeObj>(_objSting);
- _result = _obj.msg0;
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- public static bool checkTicket(buyTicketObj _obj,out long totalMoney)
- {
- bool _result = false;
- totalMoney = 0;
- try
- {
- string gameId =_obj.gameId;
- int gameIdTemp;
- if (!int.TryParse(gameId, out gameIdTemp))
- {
- _result = false;
- return _result;
- }
- if(gameIdTemp <4 || gameIdTemp >9)
- {
- _result = false;
- return _result;
- }
- foreach (ticketObj _o in _obj.ticket)
- {
- string code = _o.code;
- int _codeTem;
- if(!int.TryParse(code, out _codeTem))
- {
- _result = false;
- return _result;
- }
- if((gameId=="4" || gameId=="7") && code.Length!=2)
- {
- _result = false;
- return _result;
- }
- if ((gameId == "5" || gameId == "8") && code.Length != 4)
- {
- _result = false;
- return _result;
- }
- if ((gameId == "6" || gameId == "9") && code.Length != 3)
- {
- _result = false;
- return _result;
- }
- long _money = 0;
- if(long.TryParse(_o.money,out _money))
- {
- if(_money>0)
- totalMoney = totalMoney + _money;
- else
- {
- _result = false;
- return _result;
- }
- }
- else
- {
- _result = false;
- return _result;
- }
-
- }
- }
- catch (Exception ex)
- {
- }
- _result = true;
- return _result;
- }
- //Lay ve goi cuoc khach hang dang su dung, de biet thue bao tra truoc hay tra sau
- public static string chargeGWGetPackage(redisConnection _redis, string reqeust, string msisdn, log4net.ILog _log)
- {
- string _result = "unknow";
- try
- {
- string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <soapenv:Header/>\n" +
- " <soapenv:Body>\n" +
- " <web:gwOperation>\n" +
- " <Input>\n" +
- " <username>#username#</username>\n" +
- " <password>#password#</password>\n" +
- " <wscode>view8585</wscode>\n" +
- " <param name=\"GWORDER\" value=\"1\"/>\n" +
- " <param name=\"isdn\" value=\"#msisdn#\"/>\n" +
- " <!--Optional:-->\n" +
- " <rawData>1</rawData>\n" +
- " </Input>\n" +
- " </web:gwOperation>\n" +
- " </soapenv:Body>\n" +
- "</soapenv:Envelope>";
- string _msisdnTemp = msisdn;
- if (msisdn.Length > 3)
- {
- if (msisdn.Substring(0, 3) == "509")
- {
- _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
- }
- }
- string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_WSCODE", "CHARGE_BASIC_WSCODE", "SYSTEM").values;
- string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
- string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_USER", "CHARGE_BASIC_USER", "SYSTEM").values;
- string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_PASS", "CHARGE_BASIC_PASS", "SYSTEM").values;
- string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_URL", "CHARGE_BASIC_URL", "SYSTEM").values;
- //string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "CHARGE_NATCOM_LOTTO|1|" + money + "|" + "NATCOMLOTTO" + reqeust;
- xml = xml.Replace("#username#", topupBasicUser);
- xml = xml.Replace("#password#", topupBasicPass);
- //xml = xml.Replace("#wscode#", topupBasicWscode);
- xml = xml.Replace("#msisdn#", _msisdnTemp);
- _log.Info("Request :"+ reqeust+", XML: " + xml);
- _log.Info("View of request :" + reqeust);
- //string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
- //Tra co dinh 1 ket qua cho viec test local
- string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
- " <S:Body>\n" +
- " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <Result>\n" +
- " <error>0</error>\n" +
- " <description>success</description>\n" +
- " <return>11|8900692|33889999|2|00|NATMANGO|null|05/26/2017|18643.094|null|null|null|null</return>\n" +
- " <original><?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:viewResponse xmlns:ns2=\"http://ws.api.vasgw.viettel.com/\"><return>22|7476613|HAI|42029864|2|000|ROYAL_2|null|07/13/2020</return></ns2:viewResponse></S:Body></S:Envelope></original>\n" +
- " </Result>\n" +
- " </ns2:gwOperationResponse>\n" +
- " </S:Body>\n" +
- "</S:Envelope>";
- _log.Info("Response :"+ reqeust+ ": " + resultPost);
- string err_code_cmd = "-2000";
- string err_msg_cmd = "cmd err unknow";
- if (!string.IsNullOrEmpty(resultPost))
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(resultPost);
- err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
- err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
-
- if (err_code_cmd == "0")
- {
- string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
- _log.Info("ressult view :" + ketqua);
- _result = ketqua.Split('|')[6];
- _log.Info("The packet of msisdn :"+ _msisdnTemp+" is: " + _result);
- }
- }
- }
- catch (Exception ex)
- {
- }
- return _result.ToUpper();
- }
- //tru tien khac hang
- public static string chargeGW(redisConnection _redis ,string reqeust,string msisdn,string money, log4net.ILog _log)
- {
- string _result = "-2"; //He thong nang cap -2:nang cap,0=thanh cong,1=Het tien,21=Thue bao ko active,23=Khong ton tai thue bao
- try
- {
- string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <soapenv:Header/>\n" +
- " <soapenv:Body>\n" +
- " <web:gwOperation>\n" +
- " <Input>\n" +
- " <username>#username#</username>\n" +
- " <password>#password#</password>\n" +
- " <wscode>charge8585</wscode>\n" +
- " <param name=\"GWORDER\" value=\"1\"/>\n" +
- " <param name=\"input\" value=\"#input#\"/>\n" +
- " <!--Optional:-->\n" +
- " <rawData></rawData>\n" +
- " </Input>\n" +
- " </web:gwOperation>\n" +
- " </soapenv:Body>\n" +
- "</soapenv:Envelope>";
- string _msisdnTemp = msisdn;
- if (msisdn.Length > 3)
- {
- if (msisdn.Substring(0, 3) == "509")
- {
- _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
- }
- }
- string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_WSCODE", "CHARGE_BASIC_WSCODE", "SYSTEM").values;
- string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
- string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_USER", "CHARGE_BASIC_USER", "SYSTEM").values;
- string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_PASS", "CHARGE_BASIC_PASS", "SYSTEM").values;
- string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "CHARGE_BASIC_URL", "CHARGE_BASIC_URL", "SYSTEM").values;
- string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "CHARGE_NATCOM_LOTTO|1|" + money + "|" + "NATCOMLOTTO" + reqeust;
- xml = xml.Replace("#username#", topupBasicUser);
- xml = xml.Replace("#password#", topupBasicPass);
- xml = xml.Replace("#wscode#", topupBasicWscode);
- xml = xml.Replace("#input#", input);
- _log.Info("Request :" + reqeust + ", XML: " + xml);
- //string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
- //Tra co dinh 1 ket qua cho viec test local
- string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
- " <S:Body>\n" +
- " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <Result>\n" +
- " <error>0</error>\n" +
- " <description>success</description>\n" +
- " <return>0|The transaction was done successfully</return>\n" +
- " <original><![CDATA[<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:chargeResponse xmlns:ns2=\"http://ws.api.vasgw.viettel.com/\"><return>0|The transaction was done successfully</return></ns2:chargeResponse></S:Body></S:Envelope>]]></original>\n" +
- " </Result>\n" +
- " </ns2:gwOperationResponse>\n" +
- " </S:Body>\n" +
- "</S:Envelope>";
- _log.Info("Response :" + reqeust + ": " + resultPost);
- string err_code_cmd = "-2000";
- string err_msg_cmd = "cmd err unknow";
- if (!string.IsNullOrEmpty(resultPost))
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(resultPost);
- err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
- err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
- string errCode, errMsg;
- if (err_code_cmd == "0")
- {
- string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
- _log.Info("ressult :" + ketqua);
- //He thong nang cap -2:nang cap,0=thanh cong,1=Het tien,21=Thue bao ko active,23=Khong ton tai thue bao
- errCode = ketqua.Split('|')[0];
- errMsg = ketqua.Split('|')[1];
- if (errCode == "0")
- _result = "0";
- else if(errCode == "S-PRF-00025")
- _result = "21";
- else if (errCode == "55")
- _result = "1";
-
- else
- _result = "-2";
- }
- }
-
- }
- catch (Exception ex)
- {
- }
-
- return _result;
- }
- //Chu y ca 2 ham cong tien nay cho tra ve 2 trang thai 0= thanh cong, -1 = that bai do trycat (trang thai nay se pending)
- //cong tai khoan goc
- public static string topupBasicAcount(redisConnection _redis, string reqeust, string msisdn, string money,
- out string transId,out string errCode,out string errMsg, log4net.ILog _log)
- {
- transId = reqeust;
- errCode = "-1";
- errMsg = "Flase";
-
- string _result = "-1"; //He thong nang cap
- try
- {
- string xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <soapenv:Header/>\n" +
- " <soapenv:Body>\n" +
- " <web:gwOperation>\n" +
- " <Input>\n" +
- " <username>#username#</username>\n" +
- " <password>#password#</password>\n" +
- " <wscode>#wscode#</wscode>\n" +
- " <param name=\"GWORDER\" value=\"1\"/>\n" +
- " <param name=\"input\" value=\"#input#\"/>\n" +
- " <rawData>1</rawData>\n" +
- " </Input>\n" +
- " </web:gwOperation>\n" +
- " </soapenv:Body>\n" +
- "</soapenv:Envelope>";
- string _msisdnTemp = msisdn;
- if (msisdn.Length > 3)
- {
- if (msisdn.Substring(0, 3) == "509")
- {
- _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
- }
- }
- string topupBasicWscode = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_WSCODE", "TOPUP_BASIC_WSCODE", "SYSTEM").values;
- string topupBasicShortCode = CommonFunction.getParamObjFromRedis(_redis, "MT_SHORT_CODE", "MT_SHORT_CODE", "SYSTEM").values;
- string topupBasicUser = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_USER", "TOPUP_BASIC_USER", "SYSTEM").values;
- string topupBasicPass = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_PASS", "TOPUP_BASIC_PASS", "SYSTEM").values;
- string topupBasicUrl = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_BASIC_URL", "TOPUP_BASIC_URL", "SYSTEM").values;
- string input = _msisdnTemp + "|" + topupBasicShortCode + "|" + "ADD_NATCOM_LOTTO|1|" + money + ".0|0|" + "NATCOMLOTTO"+reqeust;
- xml = xml.Replace("#username#", topupBasicUser);
- xml = xml.Replace("#password#", topupBasicPass);
- xml = xml.Replace("#wscode#", topupBasicWscode);
- xml = xml.Replace("#input#", input);
- _log.Info("Request :" + reqeust + ", XML: " + xml);
- //string resultPost = sendPostXML(topupBasicUrl, "300000", xml);
- //Tra co dinh 1 ket qua cho viec test local
- string resultPost = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
- " <S:Body>\n" +
- " <ns2:gwOperationResponse xmlns:ns2=\"http://webservice.bccsgw.viettel.com/\">\n" +
- " <Result>\n" +
- " <error>0</error>\n" +
- " <description>success</description>\n" +
- " <return>0|The transaction was done successfully</return>\n" +
- " <original><![CDATA[<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:chargeResponse xmlns:ns2=\"http://ws.api.vasgw.viettel.com/\"><return>0|The transaction was done successfully</return></ns2:chargeResponse></S:Body></S:Envelope>]]></original>\n" +
- " </Result>\n" +
- " </ns2:gwOperationResponse>\n" +
- " </S:Body>\n" +
- "</S:Envelope>";
- _log.Info("Response :" + reqeust + ": " + resultPost);
- string err_code_cmd = "-2000";
- string err_msg_cmd = "cmd err unknow";
- if (!string.IsNullOrEmpty(resultPost))
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(resultPost);
- err_code_cmd = xmlDoc.GetElementsByTagName("error").Item(0).InnerText;
- err_msg_cmd = xmlDoc.GetElementsByTagName("description").Item(0).InnerText;
- if (err_code_cmd == "0")
- {
- string ketqua = xmlDoc.GetElementsByTagName("return").Item(0).InnerText;
- _log.Info("ressult :" + ketqua);
- errCode = ketqua.Split('|')[0];
- errMsg = ketqua.Split('|')[1];
-
- }
- }
- _result = "0";//neu da den day thi chac chan hoac success hoac revert. Khong bao gio pending
- }
- catch (Exception ex)
- {
- }
-
- return _result;
- }
- //Chu y ca 2 ham cong tien nay cho tra ve 2 trang thai 0= thanh cong, -1 = that bai do trycat (trang thai nay se pending)
- public static string topupToWallet(redisConnection _redis, string reqeust, string msisdn, string money,
- out string transId, out string errCode, out string errMsg,log4net.ILog _log)
- {
- transId = reqeust;
- errCode = "-1";
- errMsg = "false";
- string _result = "-1"; //truong hop nay la loi try cath nen phai pending
- try
- {
-
- string jsomPost = "{\n" +
- " \"partnerCode\": \"#partnerCode#\",\n" +
- " \"accessKey\": \"#accessKey#\",\n" +
- " \"requestId\": \"#requestId#\",\n" +
- " \"requestTime\": \"#requestTime#\",\n" +
- " \"requestInfo\": \"#requestInfo#\",\n" +
- " \"functionName\": \"#functionName#\",\n" +
- " \"functionParams\": \"#functionParams#\",\n" +
- " \"signature\": \"#signature#\"\n" +
- "}";
- string topupWalletKeyPrivate = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_KEY_PRIVATE", "TOPUP_WALLET_KEY_PRIVATE", "SYSTEM").values;
- string topupWalletPartnerCode = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_PARTNER_CODE", "TOPUP_WALLET_PARTNER_CODE", "SYSTEM").values;
- string topupWalletUrl = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_URL", "TOPUP_WALLET_URL", "SYSTEM").values;
- string topupWalletContentPay = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CONTEN_PAY", "TOPUP_WALLET_CONTEN_PAY", "SYSTEM").values;
- string accessKey = Sha256Hash(topupWalletKeyPrivate + reqeust);
- string requestTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fff");
- string functionName = "dc92836b9910aecce8f4b216eeeb5506fd8de7a12f224a8938ddd3b79c500a1e";
- string _msisdnTemp = msisdn;
- //if (msisdn.Length > 3)
- //{
- // if (msisdn.Substring(0, 3) == "509")
- // {
- // _msisdnTemp = msisdn.Substring(3, msisdn.Length - 3);
- // }
- //}
- if (msisdn.Length > 3)
- {
- if (msisdn.Substring(0, 3) != "509")
- {
- _msisdnTemp = "509"+msisdn;
- }
- }
- _log.Info("topup for msisdn: " + _msisdnTemp);
- _log.Info("topupToWallet :" + reqeust + ", _msisdnTemp: " + _msisdnTemp);
- string param = "{\"msisdn\":"+ _msisdnTemp + ",\"amount\":"+money+"}";
- string functionParams = Base64Encode(param);
- string signature = "{accessKey=$accessKey&functionName=$functionName&functionParams=$functionParams&partnerCode=$partnerCode&requestId=$requestId&requestInfo=$requestInfo&requestTime=$requestTime}";
- signature = signature.Replace("$accessKey", accessKey);
- signature = signature.Replace("$functionName", functionName);
- signature = signature.Replace("$functionParams", functionParams);
- signature = signature.Replace("$partnerCode", topupWalletPartnerCode);
- signature = signature.Replace("$requestId", reqeust);
- signature = signature.Replace("$requestInfo", topupWalletContentPay);
- signature = signature.Replace("$requestTime", requestTime);
- signature = HMACSHA256Encode(signature, topupWalletKeyPrivate);
- jsomPost = jsomPost.Replace("#partnerCode#", topupWalletPartnerCode);
- jsomPost = jsomPost.Replace("#accessKey#", accessKey);
- jsomPost = jsomPost.Replace("#requestId#", reqeust);
- jsomPost = jsomPost.Replace("#requestTime#", requestTime);
- jsomPost = jsomPost.Replace("#requestInfo#", topupWalletContentPay);
- jsomPost = jsomPost.Replace("#functionName#", functionName);
- jsomPost = jsomPost.Replace("#functionParams#", functionParams);
- jsomPost = jsomPost.Replace("#signature#", signature);
- //string strResponse =SendPost(topupWalletUrl, jsomPost,_log);
- //Tra ve ket qua co dinh, danh cho test local
- string strResponse = "{\n" +
- " \"partnerCode\": \"natcom_loto\",\n" +
- " \"requestId\": \"123457430\",\n" +
- " \"requestTime\": \"2022-02-18T14:06:03.547\",\n" +
- " \"responseTime\": \"2022-02-18T14:06:04.678\",\n" +
- " \"status\": \"200\",\n" +
- " \"message\": \"OK\",\n" +
- " \"result\": {\n" +
- " \"phoneNumber\": \"50940825038\",\n" +
- " \"responseDescription\": \"Transaction successfully!\",\n" +
- " \"transactionId\": \"22021840220258\",\n" +
- " \"responseCode\": \"00000\"\n" +
- " }\n" +
- "}";
- _log.Info("Response :" + reqeust + ": " + strResponse);
- topupWalletResponseObj response = JsonConvert.DeserializeObject<topupWalletResponseObj>(strResponse);
- if(response.Status=="200")
- {
- errCode = response.Result.ResponseCode; //den day thi chac chan se ko pending, hoac la thanh cong, hoac la revert
- errMsg = response.Result.ResponseDescription;
- }
- _result = "0"; //khi chay den day la moi lenh da thanh cong roi.
- }
- catch (Exception ex)
- {
- _log.Info("topup exeption: " + ex.ToString(),ex);
- }
-
- return _result;
- }
- //-1: He thong nang cap,1= Vi ton tai va active,2=Khong ton tai vi,3= Vi khong active
- public static string checkExistsWallet(redisConnection _redis, string reqeust, string msisdn, log4net.ILog _log)
- {
-
- string _result = "-1"; //he thong nang cap
- try
- {
- string jsomPost =
- "{\n" +
- " \"username\": \"$user\",\n" +
- " \"password\": \"$pass\",\n" +
- " \"msisdn\": \"$msisdn\",\n" +
- " \"requestId\": \"$reqeust\"\n" +
- "}";
- string TOPUP_WALLET_CHECK_USER = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_USER", "TOPUP_WALLET_CHECK_USER", "SYSTEM").values;
- string TOPUP_WALLET_CHECK_PASS = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_PASS", "TOPUP_WALLET_CHECK_PASS", "SYSTEM").values;
- string TOPUP_WALLET_CHECK_URL = CommonFunction.getParamObjFromRedis(_redis, "TOPUP_WALLET_CHECK_URL", "TOPUP_WALLET_CHECK_URL", "SYSTEM").values;
- string accessKey = Sha256Hash(TOPUP_WALLET_CHECK_PASS + reqeust);
- _log.Info("accessKey: " + accessKey);
- jsomPost = jsomPost.Replace("$user", TOPUP_WALLET_CHECK_USER);
- jsomPost = jsomPost.Replace("$pass", accessKey);
- jsomPost = jsomPost.Replace("$msisdn", msisdn);
- jsomPost = jsomPost.Replace("$reqeust", reqeust);
- _log.Info("Request :" + reqeust + ", jsomPost: " + jsomPost);
- //string strResponse =SendPost(TOPUP_WALLET_CHECK_URL, jsomPost,_log);
-
- //Tra ve ket qua co dinh, danh cho test local
- string strResponse = "{\n" +
- " \"status\": \"200\",\n" +
- " \"timestamp\": \"2022-02-24T05:51:56.800\",\n" +
- " \"message\": \"OK\",\n" +
- " \"result\": {\n" +
- " \"accountState\": 1,\n" +
- " \"role\": \"EU\",\n" +
- " \"msisdn\": \"50940825038\",\n" +
- " \"registerDate\": \"2022-01-27 22:43:01\"\n" +
- " }\n" +
- "}";
- _log.Info("Response :" + reqeust + ": " + strResponse);
- //-1: He thong nang cap,1= Vi ton tai va active,2=Khong ton tai vi,3= Vi khong active
- walletCheckExists response = JsonConvert.DeserializeObject<walletCheckExists>(strResponse);
- if (response.Status == "200")
- {
- if (response.Result.AccountState == "1" && response.Result.Role=="EU")
- _result = "1";
- else if(response.Result.AccountState == "-1")
- _result = "2";
- else
- _result = "3";
- }
-
- }
- catch (Exception ex)
- {
- }
- return _result;
- }
- public static String SendPost(string url, string str, log4net.ILog _log)
- {
- _log.Info("start port URL: " + url);
- _log.Info("Reqeust: " + str);
- var data = new StringContent(str, Encoding.UTF8, "application/json");
- //StringBuilder sb = new StringBuilder();
- //sb.Append("Request: " + data);
- using (var client = new HttpClient())
- {
- TimeSpan timeSpam = new TimeSpan(0, 5, 0);
- client.Timeout = timeSpam;
- var response = client.PostAsync(url, data).Result;
- if (response.IsSuccessStatusCode)
- {
- var responseContent = response.Content;
- //sb.Append("\r\nResponse: " + responseContent);
- //log.Debug(sb.ToString());
- // by calling .Result you are synchronously reading the result
- string responseString = responseContent.ReadAsStringAsync().Result;
- _log.Info("Response: " + responseString);
- return responseString;
- }
- else
- {
- _log.Info("Post not succssss: " + response.IsSuccessStatusCode.ToString());
-
- }
- //sb.Append("\r\nResponse: " + response);
- //log.Debug(sb.ToString());
- return response.StatusCode.ToString();
- }
- }
- public static string sendPostXML(string api_path, string timeout, string xml)
- {
- string rp = "";
- try
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api_path);
- byte[] bytes;
- bytes = System.Text.Encoding.UTF8.GetBytes(xml);
- request.ContentType = "text/xml; encoding='utf-8'";
- request.ContentLength = bytes.Length;
- request.Method = "POST";
- request.Timeout = Convert.ToInt32(timeout);
- Stream requestStream = request.GetRequestStream();
- requestStream.Write(bytes, 0, bytes.Length);
- requestStream.Close();
- HttpWebResponse response;
- response = (HttpWebResponse)request.GetResponse();
- Stream responseStream = response.GetResponseStream();
- rp = new StreamReader(responseStream).ReadToEnd();
- }
- catch (Exception ex)
- {
-
- }
- return rp;
- }
- public static bool WriteTextFile(String fileName, String t)
- {
- TextWriter textWriter;
- try
- {
- textWriter = new StreamWriter(fileName);
- }
- catch (Exception)
- {
- return false;
- }
- try
- {
- textWriter.WriteLine(t);
- }
- catch (Exception)
- {
- return false;
- }
- textWriter.Close();
- return true;
- }
- public static string subStrBetween(string STR, string FirstString, string LastString)
- {
- string FinalString;
- int Pos1 = STR.IndexOf(FirstString) + FirstString.Length;
- int Pos2 = STR.IndexOf(LastString);
- FinalString = STR.Substring(Pos1, Pos2 - Pos1);
- return FinalString;
- }
- public static string stringRemoveFrom0ToString(string STR, string Stringkey)
- {
- string FinalString;
- int Pos1 = STR.IndexOf(Stringkey) + Stringkey.Length;
-
- FinalString = STR.Remove(0,Pos1);
- return FinalString;
- }
- public static string stringGet1CharFirstString(string STR, string Stringkey)
- {
- string FinalString;
- int Pos1 = STR.IndexOf(Stringkey) ;
- FinalString = STR.Substring(Pos1-1,1);
- return FinalString;
- }
- public static string Sha256Hash(string rawData)
- {
- // Create a SHA256
- using (SHA256 sha256Hash = SHA256.Create())
- {
- // ComputeHash - returns byte array
- byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
- // Convert byte array to a string
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- {
- builder.Append(bytes[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- public static string Base64Encode(string plainText)
- {
- var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
- return System.Convert.ToBase64String(plainTextBytes);
- }
- public static string HMACSHA256Encode(string input, string key)
- {
- byte[] k = Encoding.ASCII.GetBytes(key);
- HMACSHA256 myhmacsha256 = new HMACSHA256(k);
- byte[] byteArray = Encoding.ASCII.GetBytes(input);
- using (MemoryStream stream = new MemoryStream(byteArray))
- {
- return byteToHex(myhmacsha256.ComputeHash(stream));
- }
- }
- private static string byteToHex(byte[] hash)
- {
- return BitConverter.ToString(hash).Replace("-", "").ToLower();
- }
-
- public static string ByteArrayToHexString(byte[] ba)
- {
- StringBuilder hex = new StringBuilder(ba.Length * 2);
- foreach (byte b in ba)
- {
- hex.AppendFormat("{0:X2}", b);
- }
- return hex.ToString();
- }
- }
- }
|