public static string HashSHA256(string password) { using (System.Security.Cryptography.SHA256Managed crypt = new System.Security.Cryptography.SHA256Managed()) { System.Text.StringBuilder hash = new System.Text.StringBuilder(); byte[] bytes = crypt.ComputeHash(Encoding.UTF8.GetBytes(password), 0, Encoding.UTF8.GetByteCount(password)); return(ExtensionsString.BytesToHex(bytes)); } }
public static byte[] AssocToUtf8Bytes(Dictionary <string, byte[]> assoc) { string output = ""; foreach (KeyValuePair <string, byte[]> kp in assoc) { output += ExtensionsString.Base64Encode(kp.Key.GetUtf8Bytes()) + ":" + ExtensionsString.Base64Encode(kp.Value) + "\n"; } return(System.Text.Encoding.UTF8.GetBytes(output)); }
public static string HashSHA512File(string path) { using (FileStream stream = File.OpenRead(path)) { using (SHA512 sha = SHA512.Create()) { byte[] bytes = sha.ComputeHash(stream); return(ExtensionsString.BytesToHex(bytes)); } } }
private static void CreateEventCSV() { // read CSV string[,] source = ToolsIOCSV.ReadCSVFile(ToolsTradingDataSet.GetPath() + "events.csv", Delimiter.SemiColon); //Allocate new array List <string[]> result_list = new List <string[]>(); string[] exclusion = new string[] { "day", "Day", "Data" }; //For each other column for (int index_0 = 0; index_0 < source.GetLength(0); index_0++) { if (!ExtensionsString.ContainsAny(source[index_0, 2], exclusion)) { string[] result = new string[3]; // Date symbol impact //Find dat DateTime week_start = DateTime.Parse(source[index_0, 0]); int year = week_start.Year; int month = ToolsForexFactory.MonthToInt(source[index_0, 1].Split(' ')[0]); int day = int.Parse(source[index_0, 1].Split(' ')[1]); if ((week_start.Month == 12) && (month == 1)) { year++; } int hour = int.Parse(source[index_0, 2].Split(':')[0]); int minute = int.Parse(source[index_0, 2].Split(':')[1].Substring(0, 2)); if (source[index_0, 2].Contains("pm")) { hour = hour + 12; } if (hour == 24) { hour = 0; DateTime date_time = new DateTime(year, month, day, hour, minute, 0); date_time = date_time.AddDays(1); result[0] = ToolsTime.DateTimeToUnixTimestampInt32(date_time).ToString(); } else { DateTime date_time = new DateTime(year, month, day, hour, minute, 0); result[0] = ToolsTime.DateTimeToUnixTimestampInt32(date_time).ToString(); } result[1] = source[index_0, 3]; switch (source[index_0, 4]) { case "Non-Economic": result[2] = "0"; // Impacs break; case "Low Impact Expected": result[2] = "1"; // Impacs break; case "Medium Impact Expected": result[2] = "2"; // Impacs break; case "High Impact Expected": result[2] = "3"; // Impacs break; default: throw new Exception("Unknown impact"); } result_list.Add(result); } } ToolsIOCSV.WriteCSVFile(ToolsTradingDataSet.GetPath() + "events_small.csv", ToolsCollection.ConvertToArray2D(result_list)); }
// This is the only method about exchange data between this software and AirVPN infrastructure. // We don't use SSL. Useless layer in our case, and we need to fetch hostname and direct IP that don't permit common-name match. // 'S' is the AES 256 bit one-time session key, crypted with a RSA 4096 public-key. // 'D' is the data from the client to our server, crypted with the AES. // The server answer is XML decrypted with the same AES session. public XmlDocument FetchUrl(string authPublicKey, string url, Dictionary <string, string> parameters) { // AES using (RijndaelManaged rijAlg = new RijndaelManaged()) { rijAlg.KeySize = 256; rijAlg.GenerateKey(); rijAlg.GenerateIV(); // Generate S // Bug workaround: Xamarin 6.1.2 macOS throw an 'Default constructor not found for type System.Diagnostics.FilterElement' error. // in 'new System.Xml.Serialization.XmlSerializer', so i avoid that. /* * StringReader sr = new System.IO.StringReader(authPublicKey); * System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters)); * RSAParameters publicKey = (RSAParameters)xs.Deserialize(sr); */ RSAParameters publicKey = new RSAParameters(); XmlDocument docAuthPublicKey = new XmlDocument(); docAuthPublicKey.LoadXml(authPublicKey); publicKey.Modulus = Convert.FromBase64String(docAuthPublicKey.DocumentElement["Modulus"].InnerText); publicKey.Exponent = Convert.FromBase64String(docAuthPublicKey.DocumentElement["Exponent"].InnerText); Dictionary <string, byte[]> assocParamS = new Dictionary <string, byte[]>(); assocParamS["key"] = rijAlg.Key; assocParamS["iv"] = rijAlg.IV; byte[] bytesParamS = null; using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider()) { csp.ImportParameters(publicKey); bytesParamS = csp.Encrypt(UtilsCore.AssocToUtf8Bytes(assocParamS), false); } // Generate D byte[] aesDataIn = UtilsCore.AssocToUtf8Bytes(parameters); byte[] bytesParamD = null; { MemoryStream aesCryptStream = null; CryptoStream aesCryptStream2 = null; try { aesCryptStream = new MemoryStream(); using (ICryptoTransform aesEncryptor = rijAlg.CreateEncryptor()) { aesCryptStream2 = new CryptoStream(aesCryptStream, aesEncryptor, CryptoStreamMode.Write); aesCryptStream2.Write(aesDataIn, 0, aesDataIn.Length); aesCryptStream2.FlushFinalBlock(); bytesParamD = aesCryptStream.ToArray(); } } finally { if (aesCryptStream2 != null) { aesCryptStream2.Dispose(); } else if (aesCryptStream != null) { aesCryptStream.Dispose(); } } } // HTTP Fetch HttpRequest request = new HttpRequest(); request.Url = url; request.Parameters["s"] = ExtensionsString.Base64Encode(bytesParamS); request.Parameters["d"] = ExtensionsString.Base64Encode(bytesParamD); HttpResponse response = Engine.Instance.FetchUrl(request); try { byte[] fetchResponse = response.BufferData; byte[] fetchResponsePlain = null; MemoryStream aesDecryptStream = null; CryptoStream aesDecryptStream2 = null; // Decrypt answer try { aesDecryptStream = new MemoryStream(); using (ICryptoTransform aesDecryptor = rijAlg.CreateDecryptor()) { aesDecryptStream2 = new CryptoStream(aesDecryptStream, aesDecryptor, CryptoStreamMode.Write); aesDecryptStream2.Write(fetchResponse, 0, fetchResponse.Length); aesDecryptStream2.FlushFinalBlock(); fetchResponsePlain = aesDecryptStream.ToArray(); } } finally { if (aesDecryptStream2 != null) { aesDecryptStream2.Dispose(); } else if (aesDecryptStream != null) { aesDecryptStream.Dispose(); } } string finalData = System.Text.Encoding.UTF8.GetString(fetchResponsePlain); XmlDocument doc = new XmlDocument(); doc.LoadXml(finalData); return(doc); } catch (Exception ex) { string message = ""; if (response.GetHeader("location") != "") { message = LanguageManager.GetText("ProviderRefreshFailUnexpected302", Title, response.GetHeader("location")); } else { message = ex.Message + " - " + response.GetLineReport(); } throw new Exception(message); } } }
public override void OnNormalizeVersion() { Version = ExtensionsString.RegExMatchOne(Version, "^curl\\s(.*?)\\s"); }