/// <summary> /// Retrieve the NeathCheckSum Definition of the File. /// NeathCheckSum Compare the length and starting 1024 bytes /// and lastest 1024 bytes as well. /// </summary> /// <param name="finfo"></param> /// <returns></returns> private static string NeathCheckSum(Alphaleonis.Win32.Filesystem.FileInfo finfo) { int bytesCount = 1024; int totalBytes = bytesCount * 2; byte[] buffer = new byte[bytesCount]; byte[] start = new byte[bytesCount]; byte[] end = new byte[bytesCount]; if (finfo.Length <= totalBytes) { using (FileStream fs = finfo.OpenRead()) { fs.Read(buffer, 0, bytesCount); return(BitConverter.ToString(buffer, 0)); } } else { using (FileStream fs = finfo.OpenRead()) { fs.Read(start, 0, bytesCount); fs.Position = finfo.Length - bytesCount; fs.Read(end, 0, bytesCount); return(BitConverter.ToString(start, 0) + BitConverter.ToString(end, 0)); } } }
public static string GetHash(this Alphaleonis.Win32.Filesystem.FileInfo file, HashAlgorithms algorithm) { byte[] hash = null; using (var fileStream = file.OpenRead()) { switch (algorithm) { case HashAlgorithms.MD5: hash = System.Security.Cryptography.MD5.Create().ComputeHash(fileStream); break; case HashAlgorithms.SHA1: hash = System.Security.Cryptography.SHA1.Create().ComputeHash(fileStream); break; case HashAlgorithms.SHA256: hash = System.Security.Cryptography.SHA256.Create().ComputeHash(fileStream); break; case HashAlgorithms.SHA384: hash = System.Security.Cryptography.SHA384.Create().ComputeHash(fileStream); break; case HashAlgorithms.SHA512: hash = System.Security.Cryptography.SHA512.Create().ComputeHash(fileStream); break; case HashAlgorithms.MACTripleDES: hash = System.Security.Cryptography.MACTripleDES.Create().ComputeHash(fileStream); break; case HashAlgorithms.RIPEMD160: hash = System.Security.Cryptography.RIPEMD160.Create().ComputeHash(fileStream); break; } fileStream.Close(); } var sb = new StringBuilder(hash.Length); for (var i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("X2")); } return(sb.ToString()); }
public override Stream OpenRead() { return(instance.OpenRead()); }