Skip to content

Commit

Permalink
Added Cryptography utility, Processes and KeyCodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Nov 21, 2017
1 parent 18e1269 commit b16e87b
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 3 deletions.
62 changes: 62 additions & 0 deletions src/GM.Utility/GM.Utility/CryptographyUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
MIT License
Copyright (c) 2017 Grega Mohorko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Project: GM.Utility
Created: 2017-10-29
Author: Grega Mohorko
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace GM.Utility
{
/// <summary>
/// Cryptography utilities.
/// </summary>
public static class CryptographyUtility
{
/// <summary>
/// Computes and returns the SHA256 hash value of the specified file.
/// </summary>
/// <param name="file">The file of which to compute the checksum.</param>
public static string GetChecksum(string file)
{
byte[] checksumBytes;
using(var sha256=new SHA256Managed()) {
using(FileStream stream = File.OpenRead(file)) {
checksumBytes = sha256.ComputeHash(stream);
}
}

string checksum = BitConverter.ToString(checksumBytes);
// remove the hyphen
return checksum.Replace("-", string.Empty);
}
}
}
13 changes: 13 additions & 0 deletions src/GM.Utility/GM.Utility/GM.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@
<NoWarn>
</NoWarn>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>GM.StrongNameKey.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -47,6 +54,7 @@
<ItemGroup>
<Compile Include="ArrayUtility.cs" />
<Compile Include="BoolUtility.cs" />
<Compile Include="CryptographyUtility.cs" />
<Compile Include="DateTimeUtility.cs" />
<Compile Include="DecimalUtility.cs" />
<Compile Include="DictionaryUtility.cs" />
Expand All @@ -55,6 +63,7 @@
<Compile Include="IEnumerableUtility.cs" />
<Compile Include="IntUtility.cs" />
<Compile Include="IOUtility.cs" />
<Compile Include="KeyCodes.cs" />
<Compile Include="LongUtility.cs" />
<Compile Include="ObjectUtility.cs" />
<Compile Include="ProcessesUtility.cs" />
Expand All @@ -66,6 +75,10 @@
<Compile Include="Util.cs" />
<Compile Include="XMLUtility.cs" />
</ItemGroup>
<ItemGroup>
<None Include="GM.StrongNameKey.pfx" />
<None Include="GM.Utility.licenseheader" />
</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.
Expand Down
47 changes: 47 additions & 0 deletions src/GM.Utility/GM.Utility/KeyCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
MIT License
Copyright (c) 2017 Grega Mohorko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Project: GM.Utility
Created: 2017-11-21
Author: Grega Mohorko
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GM.Utility
{
/// <summary>
/// A class that contains various key codes that can be used in different scenarios. One of them is sending the key codes to an external process via an input stream.
/// </summary>
public static class KeyCodes
{
/// <summary>
/// Represents a Ctrl + C command.
/// </summary>
public const string CTRL_C = "^(C)";
}
}
49 changes: 48 additions & 1 deletion src/GM.Utility/GM.Utility/ProcessesUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -41,13 +43,58 @@ namespace GM.Utility
public static class ProcessesUtility
{
/// <summary>
/// Determines whether a process with the specified name is running on the local computer. Returns true even if more than one instance is running.
/// Gets the executable path of the specified process. Can be slow (~2 seconds) if the current process is 32bit and the specified process is 64bit.
/// </summary>
/// <param name="process">The process.</param>
public static string GetProcessPath(Process process)
{
try {
return process.MainModule.FileName;
} catch(Win32Exception) {
// the slower version
return GetProcessPath(process.Id);
}
}

/// <summary>
/// Gets the executable path of the process with the specified ID. This can be quite slow (~2 seconds).
/// </summary>
/// <param name="processId">The unique identifier of the process.</param>
public static string GetProcessPath(int processId)
{
string query = $"SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = {processId}";

using(var mos = new ManagementObjectSearcher(query)) {
using(ManagementObjectCollection moc = mos.Get()) {
return (from mo in moc.Cast<ManagementObject>() select mo["ExecutablePath"]).First().ToString();
}
}
}

/// <summary>
/// Determines whether a process with the specified name is running on the local computer. Returns true if at least one instance is running.
/// </summary>
/// <param name="processName">The friendly name of the process.</param>
public static bool IsRunning(string processName)
{
Process[] processes = Process.GetProcessesByName(processName);
return processes.Length > 0;
}

/// <summary>
/// Attempts to kill all non-exited processes in the provided list of processes. It also disposes all of them.
/// </summary>
/// <param name="processes">The processes to kill and dispose.</param>
public static void KillAllProcesses(IEnumerable<Process> processes)
{
foreach(var process in processes) {
try {
if(!process.HasExited) {
process.Kill();
}
process.Dispose();
} catch { }
}
}
}
}
4 changes: 2 additions & 2 deletions src/GM.Utility/GM.Utility/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// 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.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.1.1.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]

0 comments on commit b16e87b

Please sign in to comment.