-
-
Save facemao3/fae3b6855c5d7030adf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Security.Cryptography; | |
static class Program | |
{ | |
static void Run(IEnumerable<string> args) | |
{ | |
args = from arg in args | |
select arg.Trim() into arg | |
where !string.IsNullOrEmpty(arg) | |
select arg; | |
if (!args.Any()) | |
throw new Exception("Missing file path."); | |
foreach (var e in from path in args | |
select new | |
{ | |
Path = Path.GetFullPath(path), | |
Hash = BitConverter.ToString(MD5Hash(path)) | |
.ToLowerInvariant() | |
.Replace("-", string.Empty) | |
}) | |
{ | |
Console.WriteLine(e.Hash + " " + e.Path); | |
} | |
} | |
static byte[] MD5Hash(string path) | |
{ | |
using (var stream = new FileStream(path, FileMode.Open, | |
FileAccess.Read, | |
FileShare.Read, | |
4096, | |
FileOptions.SequentialScan)) | |
{ | |
return MD5.Create().ComputeHash(stream); | |
} | |
} | |
static int Main(string[] args) | |
{ | |
try | |
{ | |
Run(args); | |
return 0; | |
} | |
catch (Exception e) | |
{ | |
Console.Error.WriteLine(e.Message); | |
Trace.TraceError(e.ToString()); | |
return Environment.ExitCode != 0 ? Environment.ExitCode : 0xbad; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment