-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5bd0876
commit c46139e
Showing
4 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{07E74F34-9E8B-4CF7-B879-92DB5A088A30}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>FileCheck</RootNamespace> | ||
<AssemblyName>FileCheck</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace FileCheck | ||
{ | ||
class Program | ||
{ | ||
private const string HashFileExtension = ".sha1"; | ||
|
||
static void Main(string[] args) | ||
{ | ||
if (args.Length < 2) | ||
{ | ||
PrintHelp(); | ||
return; | ||
} | ||
|
||
string command = args[0].ToLower(); | ||
var files = GetFiles(args.Skip(1).ToArray()); | ||
|
||
switch (command) | ||
{ | ||
case "create": | ||
CreateHashes(files.Where(file => file.Extension != HashFileExtension)); | ||
break; | ||
case "verify": | ||
VerifyHashes(files.Where(file => file.Extension == HashFileExtension)); | ||
break; | ||
default: | ||
Console.WriteLine("Unknown command '{0}'", command); | ||
PrintHelp(); | ||
break; | ||
} | ||
} | ||
|
||
private static void PrintHelp() | ||
{ | ||
Console.WriteLine("Usage:"); | ||
Console.WriteLine(" FileCheck create <file>"); | ||
Console.WriteLine(" FileCheck create [-r] <directory>"); | ||
Console.WriteLine(""); | ||
Console.WriteLine(" FileCheck verify <file>"); | ||
Console.WriteLine(" FileCheck verify [-r] <directory>"); | ||
} | ||
|
||
private static IEnumerable<FileInfo> GetFiles(string[] args) | ||
{ | ||
string path = args.Last(); | ||
bool recurse = false; | ||
if (args.Length > 1 && args[0] == "-r") | ||
{ | ||
recurse = true; | ||
} | ||
|
||
if (Directory.Exists(path)) | ||
{ | ||
var directory = new DirectoryInfo(path); | ||
foreach (FileInfo file in EnumerateDirectory(directory, recurse)) | ||
{ | ||
yield return file; | ||
} | ||
} | ||
else if (File.Exists(path)) | ||
{ | ||
yield return new FileInfo(path); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Could not find the path '{0}'", path); | ||
} | ||
} | ||
|
||
private static IEnumerable<FileInfo> EnumerateDirectory(DirectoryInfo directory, bool recurse) | ||
{ | ||
if (recurse) | ||
{ | ||
foreach (DirectoryInfo subDirectory in directory.EnumerateDirectories()) | ||
{ | ||
foreach (FileInfo file in EnumerateDirectory(subDirectory, true)) | ||
{ | ||
yield return file; | ||
} | ||
} | ||
} | ||
|
||
foreach (FileInfo file in directory.EnumerateFiles()) | ||
{ | ||
yield return file; | ||
} | ||
} | ||
|
||
private static void CreateHashes(IEnumerable<FileInfo> files) | ||
{ | ||
Console.WriteLine("Generating Checksums..."); | ||
foreach (var file in files) | ||
{ | ||
Console.Write("... {0}", file.FullName); | ||
GenerateChecksum(file); | ||
Console.WriteLine("\rOK "); | ||
} | ||
} | ||
|
||
private static void GenerateChecksum(FileInfo file) | ||
{ | ||
SHA1 hashAlgorithm = SHA1.Create(); | ||
byte[] hash; | ||
using (var inStream = file.OpenRead()) | ||
using (var outStream = new FileStream(file.FullName + HashFileExtension, FileMode.Create)) | ||
{ | ||
hash = hashAlgorithm.ComputeHash(inStream); | ||
outStream.Write(hash, 0, hash.Length); | ||
outStream.Flush(); | ||
} | ||
} | ||
|
||
private static void VerifyHashes(IEnumerable<FileInfo> hashFiles) | ||
{ | ||
Console.WriteLine("Verifying checksums..."); | ||
foreach (var hashFile in hashFiles) | ||
{ | ||
string checkedFileName = hashFile.FullName.Substring(0, hashFile.FullName.Length - HashFileExtension.Length); | ||
Console.Write("... {0}", checkedFileName); | ||
|
||
FileInfo checkedFile = new FileInfo(checkedFileName); | ||
if (!checkedFile.Exists) | ||
{ | ||
Console.WriteLine("\rMISS"); | ||
} | ||
else | ||
{ | ||
if (VerifyFileHash(checkedFile, hashFile)) | ||
{ | ||
Console.WriteLine("\rOK "); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("\rFAIL"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static bool VerifyFileHash(FileInfo checkedFile, FileInfo hashFile) | ||
{ | ||
byte[] fileHash; | ||
using (var fileStream = checkedFile.OpenRead()) | ||
{ | ||
SHA1 hashAlgorithm = SHA1.Create(); | ||
fileHash = hashAlgorithm.ComputeHash(fileStream); | ||
} | ||
|
||
byte[] referenceHash; | ||
using (var fileStream = hashFile.OpenRead()) | ||
using (var reader = new BinaryReader(fileStream)) | ||
{ | ||
referenceHash = reader.ReadBytes(fileHash.Length); | ||
} | ||
|
||
return ByteArrayCompare(fileHash, referenceHash); | ||
} | ||
|
||
private static bool ByteArrayCompare(byte[] a1, byte[] a2) | ||
{ | ||
if (a1.Length != a2.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < a1.Length; i++) | ||
if (a1[i] != a2[i]) | ||
return false; | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("FileCheck")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("FileCheck")] | ||
[assembly: AssemblyCopyright("Copyright © 2014")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("ae6133b6-90a0-4414-9078-c85105c3318f")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains 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