public async Task <CoinMarketCapData> GetCoinMarketCapData(CryptoType cryptoType) { if (!_coinMarketcapCrypoLookupNames.ContainsKey(cryptoType)) { return(null); } var coinMarketCapData = this._cache.Get(cryptoType.ToString()) as CoinMarketCapData; if (coinMarketCapData == null) { var url = new UriBuilder("https://api.coingecko.com/api/v3/simple/price"); var queryString = HttpUtility.ParseQueryString(string.Empty); queryString["ids"] = _coinMarketcapCrypoLookupNames[cryptoType]; queryString["vs_currencies"] = "USD"; queryString["include_market_cap"] = "true"; queryString["include_24hr_change"] = "true"; url.Query = queryString.ToString(); var client = new HttpClient { BaseAddress = url.Uri }; try { HttpResponseMessage response = await client.GetAsync(""); var content = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var data = Newtonsoft.Json.Linq.JObject.Parse(content).First?.First; if (data != null) { coinMarketCapData = new CoinMarketCapData { Crypto = cryptoType, PriceInUsd = data.Value <decimal>("usd"), TwentyFourHourChangePercent = data.Value <decimal>("usd_24h_change") / (decimal)100.0, MarketCap = data.Value <decimal>("usd_market_cap"), }; } this._cache.Set(new CacheItem(cryptoType.ToString(), coinMarketCapData), this._defaultCacheItemPolicyFactory()); } } catch { /* swallow exception */ } } return(coinMarketCapData); }
public string GetTradedPairName(CryptoType Ctype, FiatType Ftype, XBTPossiblePairs Xtype, ETHPossiblePairs Etype) { if (Etype.ToString() != "-1") { return(!Etype.ToString().Equals("ZUSD") ? Etype.ToString() + Ctype.ToString() : Ctype.ToString() + Etype.ToString()); } else if (Xtype.ToString() != "-1" && !Xtype.ToString().Equals("BCH")) { return(!Xtype.ToString().Equals("ZUSD") ? Xtype.ToString() + Ctype.ToString() : Ctype.ToString() + Xtype.ToString()); } else { return("BCHXBT"); } }
/// <summary> /// 解密 /// </summary> /// <param name="ciphertext"></param> /// <param name="cryptoType"></param> /// <returns></returns> public static string Decode(string ciphertext, CryptoType cryptoType) { if (ciphertext == null || ciphertext == "") { return(""); } if (cryptoType == CryptoType.Base64) { return(DecodeByBase64(ciphertext)); } throw new Exception(cryptoType.ToString() + " 类型无法解密"); }
/// <summary> /// 加密 /// </summary> /// <param name="plaintext">明文</param> /// <param name="cryptoType">加密类型</param> /// <returns>密文</returns> public static string Encrypt(string plaintext, CryptoType cryptoType) { if (plaintext == null || plaintext == "") { return(""); } if (cryptoType == CryptoType.Base64) { return(EncryptByBase64(plaintext)); } Byte[] plaintextBytes = Encoding.ASCII.GetBytes(plaintext); Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName(cryptoType.ToString())).ComputeHash(plaintextBytes); StringBuilder ciphertext = new StringBuilder(); for (int i = 0; i < hashedBytes.Length; i++) { ciphertext.Append(hashedBytes[i].ToString("X2")); } return(ciphertext.ToString()); }