public HashInfoProvider(IEnumerable <HashCalculator> hashCalculators) { infos = new InfoCollection(); int i = 0; if (hashCalculators.FirstOrDefault() == null) { return; } if (hashCalculators.FirstOrDefault().HashObj.Hash == null) { return; } foreach (var hashCalculator in hashCalculators) { BaseOption baseOption = ((hashCalculator.HashObj is TTH) ? BaseOption.Base32 : BaseOption.Heximal); Add(StreamType.Hash, i++, EntryKey.None, BaseConverter.ToString(hashCalculator.HashObj.Hash, baseOption).ToLower(), hashCalculator.Name.ToLower()); if (hashCalculator.HashObj is Ed2k) { Ed2k ed2k = (Ed2k)hashCalculator.HashObj; if (!ed2k.BlueIsRed) { baseOption = BaseOption.Heximal; Add(StreamType.Hash, i++, EntryKey.None, BaseConverter.ToString(ed2k.BlueHash, baseOption).ToLower(), hashCalculator.Name.ToLower() + "_alt"); } } } }
private String CalculateFromText(HashType ht, String str) { byte[] hashBytes = null; StringBuilder hashBuilder = new StringBuilder(); switch (ht) { case HashType.MD5: //Compute hash based on source data. hashBytes = new MD5CryptoServiceProvider().ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; case HashType.MD4: //Compute hash based on source data. hashBytes = new Md4().ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; case HashType.ED2K: //Compute hash based on source data. hashBytes = new Ed2k().ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; case HashType.CRC32: hashBytes = new Crc32().ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; case HashType.SHA1: //Compute hash based on source data. hashBytes = new SHA1CryptoServiceProvider().ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; case HashType.SHA256: //Compute hash based on source data. hashBytes = SHA256.Create().ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; case HashType.SHA384: //Compute hash based on source data. hashBytes = SHA384.Create().ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; case HashType.SHA512: //Compute hash based on source data. hashBytes = SHA512.Create().ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; case HashType.TTH: TTH tth = new TTH(); tth.Initialize(); hashBytes = tth.ComputeHash(UTF8Encoding.UTF8.GetBytes(str)); break; } foreach (Byte hashByte in hashBytes) { hashBuilder.Append(hashByte.ToString("X2").ToLower()); } return(hashBuilder.ToString()); }
private String CalculateFromFile(HashType ht, String filename) { byte[] hashBytes = null; using (FileStream file = new FileStream(filename, FileMode.Open)) { switch (ht) { case HashType.MD5: hashBytes = new MD5CryptoServiceProvider().ComputeHash(file); break; case HashType.MD4: hashBytes = new Md4().ComputeHash(file); break; case HashType.ED2K: hashBytes = new Ed2k().ComputeHash(file); break; case HashType.CRC32: hashBytes = new Crc32().ComputeHash(file); break; case HashType.SHA1: hashBytes = new SHA1CryptoServiceProvider().ComputeHash(file); break; case HashType.SHA256: hashBytes = SHA256.Create().ComputeHash(file); break; case HashType.SHA384: hashBytes = SHA384.Create().ComputeHash(file); break; case HashType.SHA512: hashBytes = SHA512.Create().ComputeHash(file); break; case HashType.TTH: TTH tth = new TTH(); tth.Initialize(); hashBytes = tth.ComputeHash(file); break; } } StringBuilder hashBuilder = new StringBuilder(); foreach (byte hashByte in hashBytes) { hashBuilder.Append(hashByte.ToString("x2").ToLower()); } return(hashBuilder.ToString()); }
public static XmlDocument CreateMediaInfoXMLLog(string filePath, IEnumerable <IBlockConsumer> blockConsumers) { XmlDocument xmlDoc = new XmlDocument(); XmlNode node, subNode; IMediaInfo mediaInfo = CreateMediaInfoInstance(); mediaInfo.Open(filePath); node = xmlDoc.AppendChild(xmlDoc.CreateElement("File")); subNode = node.AppendChild(xmlDoc.CreateElement("Hashes")); AppendLeaf(xmlDoc, subNode, "Size", (new FileInfo(filePath)).Length.ToString(), null); if (blockConsumers != null) { foreach (HashCalculator hashExecute in blockConsumers.Where(blockConsumer => { return(blockConsumer is HashCalculator); })) { AppendLeaf(xmlDoc, subNode, hashExecute.Name, BaseConverter.ToString(hashExecute.HashObj.Hash), null); if (hashExecute.HashObj is Ed2k) { Ed2k ed2k = (Ed2k)hashExecute.HashObj; if (!ed2k.BlueIsRed) { AppendLeaf(xmlDoc, subNode, "Ed2k_Alt", BaseConverter.ToString(ed2k.BlueHash), null); } } } } int streamCount, entryCount; string name, text, measure; foreach (eStreamType streamKind in Enum.GetValues(typeof(eStreamType))) { streamCount = mediaInfo.Count_Get(streamKind); for (int i = 0; i < streamCount; i++) { entryCount = mediaInfo.Count_Get(streamKind, i); subNode = node.AppendChild(xmlDoc.CreateElement(streamKind.ToString())); for (int j = 0; j < entryCount; j++) { name = mediaInfo.Get(streamKind, i, j, eInfoType.Name).Replace("/", "-").Replace("(", "").Replace(")", ""); if (name.Equals("Chapters_Pos_End") || name.Equals("Chapters_Pos_Begin") || name.Contains("-String")) { continue; } if (name.Equals("Bits-Pixel*Frame")) { name = "BitsPerPixel"; } text = mediaInfo.Get(streamKind, i, j, eInfoType.Text); measure = mediaInfo.Get(streamKind, i, j, eInfoType.Measure).Trim(); if (name.IndexOfAny(new char[] { ')', ':' }) < 0 && !String.IsNullOrEmpty(text)) { AppendLeaf(xmlDoc, subNode, name, text, String.IsNullOrEmpty(measure) ? null : new string[, ] { { "Unit", measure } }); } else { //Debug.Print(name + " " + text + " " + measure); } } if (streamKind == eStreamType.Menu) { int indexStart; int indexEnd; XmlNode chapterNode; if (int.TryParse(mediaInfo.Get(streamKind, i, "Chapters_Pos_Begin"), out indexStart) && int.TryParse(mediaInfo.Get(streamKind, i, "Chapters_Pos_End"), out indexEnd)) { chapterNode = subNode.AppendChild(xmlDoc.CreateElement("Chapters")); for (; indexStart < indexEnd; indexStart++) { AppendLeaf(xmlDoc, chapterNode, "Chapter", mediaInfo.Get(streamKind, i, indexStart, eInfoType.Text), new string[, ] { { "TimeStamp", mediaInfo.Get(streamKind, i, indexStart, eInfoType.Name) } }); } } } } } mediaInfo.Close(); return(xmlDoc); }