async public static Task <string> GetAsync(string fileName, Type type, byte[] key = null, IProgress <ProgressReport> progress = null, CancellationToken?cancel = null) { using (var stream = File.OpenRead(fileName)) { if (type == Type.QuickHash) { return(Coder.BytesToString(ComputeQuickHash(stream), Coder.CodePage.Hex)); } var hashAlg = GetHashAlgorithm(type); hashAlg.Initialize(); var buffer = new byte[65536]; while (stream.Position < stream.Length) { if (cancel?.IsCancellationRequested == true) { throw new Exception("Cancelled"); } progress?.Report(new ProgressReport(stream.Position, stream.Length)); var block = await stream.ReadAsync(buffer, 0, buffer.Length); hashAlg.TransformBlock(buffer, 0, block, null, 0); } hashAlg.TransformFinalBlock(buffer, 0, 0); return(Coder.BytesToString(hashAlg.Hash, Coder.CodePage.Hex)); } }
public static string Get(byte[] data, Type type, byte[] key = null) { if (type == Type.QuickHash) { return(Coder.BytesToString(ComputeQuickHash(data), Coder.CodePage.Hex)); } var hashAlg = GetHashAlgorithm(type); if ((key != null) && (hashAlg is BlockHashAlgorithm)) { hashAlg = new Hmac(hashAlg as BlockHashAlgorithm, key); } return(Coder.BytesToString(hashAlg.ComputeHash(data), Coder.CodePage.Hex)); }
public static XObject rToXML(string name, object obj, Type expectedType, bool createElement, Dictionary <object, XElement> references) { if (obj == null) { return(new XElement(name, new XAttribute(typeTag, nullType))); } var type = obj.GetType(); if ((type.IsPrimitive) || (type.IsEnum) || (type == typeof(string)) || (obj is Type)) { if (createElement) { return(new XElement(name, obj)); } return(new XAttribute(name, obj)); } if (references.ContainsKey(obj)) { var reference = references[obj]; if (reference.Attribute(guidTag) == null) { reference.Add(new XAttribute(guidTag, Guid.NewGuid().ToString())); } var guid = reference.Attribute(guidTag).Value; return(new XElement(name, new XAttribute(typeTag, referenceType), new XAttribute(guidTag, guid))); } var xml = references[obj] = new XElement(name); if (type != expectedType) { xml.Add(new XAttribute(typeTag, type.FullName)); } if (type == typeof(Regex)) { var regex = obj as Regex; xml.Add(regex.ToString(), new XAttribute(optionsTag, regex.Options)); } else if (type.IsArray) { var itemType = type.GetElementType(); if (itemType == typeof(byte)) { xml.Add(Coder.BytesToString(obj as byte[], Coder.CodePage.Hex)); } else { var items = obj as Array; foreach (var item in items) { xml.Add(rToXML(itemTag, item, itemType, true, references)); } } } else if ((type.IsGenericType) && ((type.GetGenericTypeDefinition() == typeof(List <>)) || (type.GetGenericTypeDefinition() == typeof(HashSet <>)))) { if (obj is IEnumerable <char> ) { xml.Add(new XAttribute("Values", Coder.ConvertString(new string((obj as IEnumerable <char>).ToArray()), Coder.CodePage.UTF8, Coder.CodePage.Base64))); } else { var items = obj as IEnumerable; var itemType = type.GetGenericArguments()[0]; foreach (var item in items) { xml.Add(rToXML(itemTag, item, itemType, true, references)); } } } else if ((type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Dictionary <,>))) { var items = obj as IDictionary; var keyType = type.GetGenericArguments()[0]; var valueType = type.GetGenericArguments()[1]; foreach (DictionaryEntry item in items) { xml.Add( new XElement(itemTag, rToXML(keyTag, item.Key, keyType, false, references), rToXML(valueTag, item.Value, valueType, false, references)) ); } } else { var toXML = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where(method => method.GetCustomAttribute <ToXMLAttribute>() != null).FirstOrDefault(); if (toXML != null) { xml.Add(toXML.Invoke(obj, new object[0])); } else { var found = new HashSet <string>(); var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (var field in fields) { var fieldName = EscapeField(field, found); var value = field.GetValue(obj); if (value == null) { continue; } xml.Add(rToXML(fieldName, value, field.FieldType, false, references)); } } } return(xml); }