public string CreateSignature(IToken token, Uri uri, string verb, string verifier = null, string callback = null) { var oAuthParameters = new OAuthParameters( new ConsumerKey(token.ConsumerKey), new TokenKey(token.TokenKey), "HMAC-SHA1", new DefaultTimestampSequence(), new DefaultNonceSequence(), string.Empty, "1.0", verifier, token.Session, callback); var signatureBaseString = new SignatureBaseString( new Request { Url = uri, Verb = verb }, oAuthParameters); var signature = new HmacSha1().Sign(signatureBaseString, token.ConsumerSecret, token.TokenSecret); oAuthParameters.SetSignature(signature); return(new AuthorizationHeader(oAuthParameters, string.Empty).Value); }
/// <summary> /// 检验是否合法 /// </summary> /// <returns></returns> public bool CheckSign(string secretKey, char separator = ';') { var strTicketParas = GetSignContent(separator); var signData = HmacSha1.EncryptBase64(strTicketParas.ToString(), secretKey); return(Sign == signData); }
public Fixture Then_the_signature_is(string expected) { string actual = new HmacSha1().Sign(_signatureBaseString, _consumerSecret, _tokenSecret); return(new YesNoFixture(expected == actual, "Invalid signature. Expected [" + expected + "]. Got [" + actual + "].", 2 )); }
/// <summary> /// 生成签名后的字符串 /// </summary> /// <returns></returns> public string ToSignData(string secretKey, char separator = ';') { TimeSpan = DateTime.Now.ToUtcSeconds(); var encrpStr = GetSignContent(separator); Sign = HmacSha1.EncryptBase64(encrpStr.ToString(), secretKey); AddSignDataValue("sign", Sign, separator, encrpStr); return(encrpStr.ToString()); }
public string Hash(HashType type, string input, string password) { switch (type) { case HashType.HmacSha1: HmacSha1 hmacSha1 = new HmacSha1(); return(hmacSha1.Hash(input, password)); case HashType.HmacSha2: break; default: return("Cannot hash with these inputs"); } return("Something went wrong during hashing"); }
public byte[] Hash(HashType type, byte[] input, byte[] password) { switch (type) { case HashType.HmacSha1: HmacSha1 hmacSha1 = new HmacSha1(); return(hmacSha1.Hash(input, password)); case HashType.HmacSha2: break; default: return(null); } return(null); }
public Eeprom(string file) { Data = File.ReadAllBytes(file); if (Data.Length != Size) { throw new InvalidDataException("EEPROM must be 256 bytes."); } Stream = new MemoryStream(Data); Reader = new BinaryReader(Stream); Writer = new BinaryWriter(Stream); HmacSha1 sha = new HmacSha1(); RC4 rc4 = new RC4(); EepromVersion version = 0; // loop through each version attempting to decrypt the security data while (version <= EepromVersion.RetailLast) { byte[] origHash = Data.Subset(0, 20); // decrypt according to the specified version rc4.Init(sha.Compute(version, origHash)); byte[] decrypted = Data.Subset(0x14, 0x1C); rc4.Crypt(decrypted, 0, decrypted.Length); // if decrypted data hash matches the original then it's valid and the specific version is now known if (origHash.IsEqual(sha.Compute(version, decrypted))) { // update with the decrypted data Stream.Position = 0x14; Writer.Write(decrypted); // set the detected version Version = version; break; } version++; } }
public Fixture Then_we_can_connect() { var oAuthParameters = new OAuthParameters( _consumer.ConsumerKey, new TokenKey(string.Empty), "HMAC-SHA1", new DefaultTimestampSequence(), new DefaultNonceSequence(), string.Empty, "1.0" ); var signatureBaseString = new SignatureBaseString( new Request { Url = new Uri("https://api.twitter.com/oauth/request_token"), Verb = "GET" }, oAuthParameters ); var signature = new HmacSha1().Sign(signatureBaseString.Value, _consumer.Secret, null); oAuthParameters.SetSignature(signature); var header = new AuthorizationHeader(oAuthParameters, string.Empty); var req = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/oauth/request_token"); req.Headers.Add("Authorization", header.Value); var response = TInternet.Get(req); const HttpStatusCode expected = HttpStatusCode.OK; var actual = response.StatusCode; return(new YesNoFixture(actual == expected, "Expected [" + expected + "]. Got [" + actual + "]", 2)); }
/// <summary> /// Saves the eeprom contents to file. /// If the version is unknown the security section will not be overwritten. /// </summary> /// <param name="file"></param> public void Save(string file) { UpdateChecksums(); // skip writing security section if version is unknown if (Version == EepromVersion.Unknown) { using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Write, FileShare.None)) { fs.Position = 0x30; fs.Write(Data, 0x30, Size - 0x30); } } else { // copy data to a separate buffer byte[] eepromCopy = new byte[Size]; Data.CopyTo(eepromCopy, 0); // update security section of copy if (Version != EepromVersion.Unknown) { HmacSha1 sha = new HmacSha1(); RC4 rc4 = new RC4(); // hash decrypted confounder/hddkey/region byte[] hash = sha.Compute(Version, eepromCopy.Subset(0x14, 0x1C)); hash.CopyTo(eepromCopy, 0); // re-encrypt data rc4.Init(sha.Compute(Version, hash)); rc4.Crypt(eepromCopy, 20, 28); } File.WriteAllBytes(file, eepromCopy); } }
public static byte[] GenerateChecksum(string checksum, int offset, byte[] buffer, int eof = -1) { byte[] returnValue = null; switch (checksum) { case "Adler8 - {1Bytes}": returnValue = eof == -1 ? Adler8.Compute(offset, buffer) : Adler8.Compute(offset, buffer, eof); break; case "Adler16 - {2Bytes}": returnValue = eof == -1 ? Adler16.Compute(offset, buffer) : Adler16.Compute(offset, buffer, eof); break; case "Adler32 - {4Bytes}": returnValue = eof == -1 ? Adler32.Compute(offset, buffer) : Adler32.Compute(offset, buffer, eof); break; case "Checksum8 - {1Bytes}": returnValue = eof == -1 ? Checksum8.Compute(offset, buffer) : Checksum8.Compute(offset, buffer, eof); break; case "Checksum16 - {2Bytes}": returnValue = eof == -1 ? Checksum16.Compute(offset, buffer) : Checksum16.Compute(offset, buffer, eof); break; case "Checksum24 - {3Bytes}": returnValue = eof == -1 ? Checksum24.Compute(offset, buffer) : Checksum24.Compute(offset, buffer, eof); break; case "Checksum32 - {4Bytes}": returnValue = eof == -1 ? Checksum32.Compute(offset, buffer) : Checksum32.Compute(offset, buffer, eof); break; case "Checksum40 - {5Bytes}": returnValue = eof == -1 ? Checksum40.Compute(offset, buffer) : Checksum40.Compute(offset, buffer, eof); break; case "Checksum48 - {6Bytes}": returnValue = eof == -1 ? Checksum48.Compute(offset, buffer) : Checksum48.Compute(offset, buffer, eof); break; case "Checksum56 - {7Bytes}": returnValue = eof == -1 ? Checksum56.Compute(offset, buffer) : Checksum56.Compute(offset, buffer, eof); break; case "Checksum64 - {8Bytes}": returnValue = eof == -1 ? Checksum64.Compute(offset, buffer) : Checksum64.Compute(offset, buffer, eof); break; case "CRC16 - {2Bytes}": Crc16 crc16 = new Crc16(); returnValue = eof == -1 ? crc16.Compute(offset, buffer) : crc16.Compute(offset, buffer, eof); break; case "CRC16 CCITT - {2Bytes}": Crc16ccitt crc16Ccitt = new Crc16ccitt(); returnValue = eof == -1 ? crc16Ccitt.Compute(offset, buffer) : crc16Ccitt.Compute(offset, buffer, eof); break; case "CRC32 - {4Bytes}": returnValue = eof == -1 ? Crc32.Compute(offset, buffer) : Crc32.Compute(offset, buffer, eof); break; case "HMAC SHA 1 (128) - {16Bytes}": returnValue = eof == -1 ? HmacSha1.Compute(offset, buffer) : HmacSha1.Compute(offset, buffer, eof); break; case "HMAC SHA 256 - {32Bytes}": returnValue = eof == -1 ? HmacSha256.Compute(offset, buffer) : HmacSha256.Compute(offset, buffer, eof); break; case "HMAC SHA 384 - {48Bytes}": returnValue = eof == -1 ? HmacSha384.Compute(offset, buffer) : HmacSha384.Compute(offset, buffer, eof); break; case "HMAC SHA 512 - {64Bytes}": returnValue = eof == -1 ? HmacSha512.Compute(offset, buffer) : HmacSha512.Compute(offset, buffer, eof); break; case "MD5 - {16Bytes}": returnValue = eof == -1 ? Md5.Compute(offset, buffer) : Md5.Compute(offset, buffer, eof); break; case "MD5 CNG - {16Bytes}": returnValue = eof == -1 ? Md5Cng.Compute(offset, buffer) : Md5Cng.Compute(offset, buffer, eof); break; } return(returnValue); }
public IActionResult TestCommon() { ViewData["ContentRootPath"] = FileHelper.ContentRootPath; ViewData["WebRootPath"] = FileHelper.WebRootPath; ViewData["WebRootName"] = FileHelper.WebRootName; string strRequestMsg = string.Empty; strRequestMsg += " HasFormContentType:" + MyHttpContext.Current.Request.HasFormContentType + Environment.NewLine; strRequestMsg += " Host:" + MyHttpContext.Current.Request.Host + Environment.NewLine; strRequestMsg += " IsHttps:" + MyHttpContext.Current.Request.IsHttps + Environment.NewLine; strRequestMsg += " Method:" + MyHttpContext.Current.Request.Method + Environment.NewLine; strRequestMsg += " Path:" + MyHttpContext.Current.Request.Path + Environment.NewLine; strRequestMsg += " PathBase:" + MyHttpContext.Current.Request.PathBase + Environment.NewLine; strRequestMsg += " Protocol:" + MyHttpContext.Current.Request.Protocol + Environment.NewLine; strRequestMsg += " Scheme:" + MyHttpContext.Current.Request.Scheme + Environment.NewLine; ViewData["RequestMsg"] = strRequestMsg; ViewData["RequestReferer"] = MyHttpContext.Current.Request.UrlReferrer(); string strHeaders = string.Empty; foreach (String strFormKey in MyHttpContext.Current.Request.Headers.Keys) { strHeaders += strFormKey + ":" + MyHttpContext.Current.Request.Headers[strFormKey] + "#####"; } ViewData["RequestHeaders"] = strHeaders; ViewData["RequestHeadersJSON"] = ""; //MyHttpContext.Current.Request.Headers.ToJson(); string strForm = string.Empty; if (MyHttpContext.Current.Request.Method == "POST") { foreach (String strFormKey in MyHttpContext.Current.Request.Form.Keys) { strForm += strFormKey + ":" + DataConverter.ToString(MyHttpContext.Current.Request.Form[strFormKey]) + "#####"; } } ViewData["RequestFormJSON"] = strForm; //ViewData["RequestFormJSON"] = MyHttpContext.Current.Request.Form.ToJson(); string strCurrentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); var resultAdd = CacheHelper.CacheServiceProvider.AddOrUpdate("CurrentTime", strCurrentTime); if (CacheHelper.CacheServiceProvider.Exists("CurrentTime")) { string resultGet = CacheHelper.CacheServiceProvider.Get <string>("CurrentTime"); ViewData["CurrentTime"] = resultGet; } string strMsg = "我是加密文件,请保管好"; string strKey = Guid.NewGuid().ToString().Replace("-", string.Empty); string strEncryptAes = AesRijndael.Encrypt(strMsg, strKey); string strDecryptAes = AesRijndael.Decrypt(strEncryptAes, strKey); ViewData["EncryptAes"] = strEncryptAes; ViewData["DecryptAes"] = strDecryptAes; string strEncryptHmacSha1 = HmacSha1.EncryptUtf8(strMsg, strKey); string strEncryptHmacSha1Base64 = HmacSha1.EncryptBase64(strMsg, strKey); ViewData["EncryptHmacSha1"] = strEncryptHmacSha1; ViewData["EncryptHmacSha1Base64"] = strEncryptHmacSha1Base64; string strEncryptMd5 = Md5.EncryptHexString(strMsg); ViewData["EncryptMd5"] = strEncryptMd5; string strEncryptSha1 = Sha1.Encrypt(strMsg); ViewData["EncryptSha1"] = strEncryptSha1; ViewData["ClientIP"] = IPHelper.GetClientIP(); ViewData["HostIP"] = IPHelper.GetHostIP(); ViewData["RandomFormatedNumeric"] = RandomHelper.GetFormatedNumeric(4, 10); ViewData["RandomString"] = RandomHelper.GetRandString(4); ViewData["RandomStringByPattern"] = RandomHelper.GetRandStringByPattern("JX###???***"); ViewData["StringHelper-GetHashKey"] = StringHelper.GetHashKey(strMsg, StringFilterOptions.HoldChinese); ViewData["StringHelper-GetStringHashKey"] = StringHelper.GetStringHashKey(strMsg); ViewData["StringHelper-MD5"] = StringHelper.MD5(strMsg); ViewData["StringHelper-MD5D"] = StringHelper.MD5D(strMsg); ViewData["StringHelper-MD5GB2312"] = StringHelper.MD5GB2312(strMsg); ViewData["StringHelper-SHA1"] = StringHelper.SHA1(strMsg); ViewData["StringHelper-ValidateMD5"] = StringHelper.ValidateMD5(strEncryptMd5, StringHelper.MD5(strMsg)); ViewData["StringHelper-SubString"] = StringHelper.SubString(strMsg, 12, "..."); ViewData["StringHelper-GetInitial"] = StringHelper.GetInitial(strMsg); ViewData["StringHelper-MakeSpellCode"] = ChineseSpell.MakeSpellCode(strMsg, SpellOptions.EnableUnicodeLetter); ViewData["StringHelper-FirstLetterOnly"] = ChineseSpell.MakeSpellCode(strMsg, SpellOptions.FirstLetterOnly); ViewData["XmlSerializer"] = ConfigHelper.Get <IPLockConfig>().ToXml(); var strXML = "<IPLockConfig> <AdminLockIPBlack>10.123.123.1</AdminLockIPBlack> <AdminLockIPType>30.123.123.3</AdminLockIPType> <AdminLockIPWhite>20.123.123.2</AdminLockIPWhite> <LockIPType>60.123.123.6</LockIPType> <LockIPBlack>40.123.123.4</LockIPBlack> <LockIPWhite>50.123.123.5</LockIPWhite> </IPLockConfig>"; var ipLockConfig = strXML.ToXmlObject <IPLockConfig>(); ViewData["XmlDeserialize"] = "IPLockConfig:" + ipLockConfig.AdminLockIPBlack + " " + ipLockConfig.AdminLockIPType + " " + ipLockConfig.AdminLockIPWhite; ViewData["JsonSerializer"] = ConfigHelper.Get <WebHostConfig>().ToJson(); ConfigHelper.Save(ConfigHelper.Get <ShopConfig>()); ConfigHelper.Save(ConfigHelper.Get <SiteConfig>()); ConfigHelper.Save(ConfigHelper.Get <SiteOptionConfig>()); ConfigHelper.Save(ConfigHelper.Get <SmsConfig>()); ConfigHelper.Save(ConfigHelper.Get <UserConfig>()); ConfigHelper.Save(ConfigHelper.Get <WebHostConfig>()); var strJSON = "{\"AuthenticationType\": 0,\"EnabledSsl\": false,\"MailFrom\": \"[email protected]\",\"MailServer\": \"smtp.qq.com\",\"MailServerPassWord\": \"lx123456\",\"MailServerUserName\": \"2094838895\",\"Port\": \"25\"}"; var mailConfig = strJSON.ToJsonObject <MailConfig>(); ViewData["JsonDeserialize"] = mailConfig.AuthenticationType + " " + mailConfig.EnabledSsl + " " + mailConfig.MailFrom + " " + mailConfig.MailServer; CookieHelper.CreateCookie("cName", "aspnetcore" + strCurrentTime, 10); ViewData["CookieHelper"] = CookieHelper.GetCookie("cName"); //CookieHelper.DeleteCookie("cName"); DateTime beginDate = new DateTime(2015, 12, 5); DateTime endDate = DateTime.Now; ViewData["DateDiff-yyyy"] = Utility.DateDiff(beginDate, endDate, "yyyy"); ViewData["DateDiff-q"] = Utility.DateDiff(beginDate, endDate, "q"); ViewData["DateDiff-m"] = Utility.DateDiff(beginDate, endDate, "m"); ViewData["DateDiff-d"] = Utility.DateDiff(beginDate, endDate, "d"); ViewData["DateDiff-w"] = Utility.DateDiff(beginDate, endDate, "w"); ViewData["DateDiff-h"] = Utility.DateDiff(beginDate, endDate, "h"); ViewData["DateDiff-n"] = Utility.DateDiff(beginDate, endDate, "n"); ViewData["DateDiff-s"] = Utility.DateDiff(beginDate, endDate, "s"); ViewData["Query-name"] = Utility.Query("name"); ViewData["Query-date"] = Utility.Query("date", DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss:fff"); ViewData["Query-age"] = Utility.Query("age", 0); ViewData["Query-mobile"] = Utility.AddQuery("mobile", "15871375589"); var mailConfigSession = ConfigHelper.Get <MailConfig>(); Utility.SetSession("sessionName", mailConfigSession); ViewData["GroupTypeEnum"] = EnumHelper.GetDescription((GroupTypeEnum)(0)); ViewData["GroupTypeEnum1"] = EnumHelper.GetDescription <GroupTypeEnum>(GroupTypeEnum.Agent.ToString()); LogFactory.SaveFileLog("日志标题get", "日志内容"); return(View()); }