Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update For Hashing. #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Hashing/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
</configuration>
103 changes: 50 additions & 53 deletions Hashing/EmbeddedAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,76 @@ namespace Hashing
{
public class EmbeddedAssembly
{
private static Dictionary<string, Assembly> dic;
private static readonly Dictionary<string, Assembly> _assemblies = new Dictionary<string, Assembly>();

public static void Load(string embeddedResource, string fileName)
{
if (dic == null)
dic = new Dictionary<string, Assembly>();
var assemblyData = GetEmbeddedResourceBytes(embeddedResource);
var assembly = TryLoadAssemblyFromBytes(assemblyData);

byte[] ba = null;
Assembly asm = null;
var curAsm = Assembly.GetExecutingAssembly();

using (var stm = curAsm.GetManifestResourceStream(embeddedResource))
if (assembly == null)
{
if (stm == null)
throw new Exception(embeddedResource + " is not found in Embedded Resources.");

ba = new byte[(int)stm.Length];
stm.Read(ba, 0, (int)stm.Length);
try
{
asm = Assembly.Load(ba);

dic.Add(asm.FullName, asm);
return;
}
catch { }
var tempFilePath = GetOrCreateTemporaryFile(assemblyData, fileName);
assembly = Assembly.LoadFile(tempFilePath);
}

var fileOk = false;
var tempFile = "";

using (var sha1 = new SHA1CryptoServiceProvider())
{
var fileHash = BitConverter.ToString(sha1.ComputeHash(ba)).Replace("-", string.Empty);
;
_assemblies[assembly.FullName] = assembly;
}

tempFile = Path.GetTempPath() + fileName;
public static Assembly Get(string assemblyFullName)
{
_assemblies.TryGetValue(assemblyFullName, out var assembly);
return assembly;
}

if (File.Exists(tempFile))
{
var bb = File.ReadAllBytes(tempFile);
var fileHash2 = BitConverter.ToString(sha1.ComputeHash(bb)).Replace("-", string.Empty);
private static byte[] GetEmbeddedResourceBytes(string embeddedResource)
{
var currentAssembly = Assembly.GetExecutingAssembly();
using (var stream = currentAssembly.GetManifestResourceStream(embeddedResource))
{
if (stream == null)
throw new Exception($"{embeddedResource} is not found in Embedded Resources.");

if (fileHash == fileHash2)
{
fileOk = true;
}
}
else
{
fileOk = false;
}
var resourceBytes = new byte[stream.Length];
stream.Read(resourceBytes, 0, resourceBytes.Length);
return resourceBytes;
}
}

if (!fileOk)
private static Assembly TryLoadAssemblyFromBytes(byte[] assemblyData)
{
try
{
File.WriteAllBytes(tempFile, ba);
return Assembly.Load(assemblyData);
}
catch
{
return null;
}
}

asm = Assembly.LoadFile(tempFile);

dic.Add(asm.FullName, asm);
private static string GetOrCreateTemporaryFile(byte[] assemblyData, string fileName)
{
var tempFilePath = Path.Combine(Path.GetTempPath(), fileName);
if (!IsFileContentEqual(tempFilePath, assemblyData))
{
File.WriteAllBytes(tempFilePath, assemblyData);
}
return tempFilePath;
}

public static Assembly Get(string assemblyFullName)
private static bool IsFileContentEqual(string filePath, byte[] data)
{
if (dic == null || dic.Count == 0)
return null;
if (!File.Exists(filePath)) return false;

if (dic.ContainsKey(assemblyFullName))
return dic[assemblyFullName];
var fileData = File.ReadAllBytes(filePath);
using (var sha1 = new SHA1CryptoServiceProvider())
{
var fileHash = BitConverter.ToString(sha1.ComputeHash(fileData)).Replace("-", string.Empty);
var dataHash = BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", string.Empty);

return null;
return fileHash == dataHash;
}
}
}
}
3 changes: 2 additions & 1 deletion Hashing/Hashing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Hashing</RootNamespace>
<AssemblyName>Hashing</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
157 changes: 50 additions & 107 deletions Hashing/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,53 @@ namespace Hashing
[Serializable]
public class SettingsJson
{
public Theme Color { get; set; }
public HashOptions HashOptions { get; set; }
public Theme Color { get; set; } = Theme.Amethyst;
public HashOptions HashOptions { get; set; } = new HashOptions();
public bool LowerCasing { get; set; }
public bool TrayIcon { get; set; }
public bool HighPriority { get; set; }
public bool CRC32Decimal { get; set; }
public short ActiveHash { get; set; }
public bool StayOnTop { get; set; }
public bool SingleInstance { get; set; }
public Size WindowSize { get; set; }

public Size WindowSize { get; set; } = new Size(820, 632);
public Point? WindowLocation { get; set; }
public FormWindowState WindowState { get; set; }
public FormWindowState WindowState { get; set; } = FormWindowState.Normal;

public LanguageCode LanguageCode { get; set; }

public SettingsJson()
{
HashOptions = new HashOptions();
}
}

public class Options
{
internal static Color ForegroundColor = Color.FromArgb(153, 102, 204);
internal static Color ForegroundAccentColor = Color.FromArgb(134, 89, 179);

internal static Color ForegroundColor;
internal static Color ForegroundAccentColor;
internal static Color BackgroundColor = Color.FromArgb(20, 20, 20);

internal readonly static string ThemeFlag = "themeable";
readonly static string SettingsFile = Application.StartupPath + "\\Hashing.json";

internal static SettingsJson CurrentOptions = new SettingsJson();

internal static dynamic TranslationList;

internal static IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent)
{
List<Control> controls = new List<Control>();
internal static string ThemeFlag = "ThemeTag";

foreach (Control child in parent.Controls)
{
controls.AddRange(GetSelfAndChildrenRecursive(child));
}
private static readonly Dictionary<Theme, (Color, Color)> ThemeColors = new Dictionary<Theme, (Color, Color)>
{
{ Theme.Amber, (Color.FromArgb(195, 146, 0), Color.FromArgb(171, 128, 0)) },
{ Theme.Jade, (Color.FromArgb(70, 175, 105), Color.FromArgb(61, 153, 92)) },
{ Theme.Ruby, (Color.FromArgb(205, 22, 39), Color.FromArgb(155, 17, 30)) },
{ Theme.Silver, (Color.Gray, Color.DimGray) },
{ Theme.Azurite, (Color.FromArgb(0, 127, 255), Color.FromArgb(0, 111, 223)) },
{ Theme.Amethyst, (Color.FromArgb(153, 102, 204), Color.FromArgb(134, 89, 179)) },
};

controls.Add(parent);
return controls;
internal static IEnumerable<Control> GetSelfAndChildrenRecursive(Control parent)
{
return parent.Controls.Cast<Control>().SelectMany(GetSelfAndChildrenRecursive).Append(parent);
}

internal static void ApplyTheme(Form f)
{
switch (CurrentOptions.Color)
if (ThemeColors.TryGetValue(CurrentOptions.Color, out var colors))
{
case Theme.Amber:
SetTheme(f, Color.FromArgb(195, 146, 0), Color.FromArgb(171, 128, 0));
break;
case Theme.Jade:
SetTheme(f, Color.FromArgb(70, 175, 105), Color.FromArgb(61, 153, 92));
break;
case Theme.Ruby:
SetTheme(f, Color.FromArgb(205, 22, 39), Color.FromArgb(155, 17, 30));
break;
case Theme.Silver:
SetTheme(f, Color.Gray, Color.DimGray);
break;
case Theme.Azurite:
SetTheme(f, Color.FromArgb(0, 127, 255), Color.FromArgb(0, 111, 223));
break;
case Theme.Amethyst:
SetTheme(f, Color.FromArgb(153, 102, 204), Color.FromArgb(134, 89, 179));
break;
SetTheme(f, colors.Item1, colors.Item2);
}
}

Expand All @@ -90,7 +67,6 @@ private static void SetTheme(Form f, Color c1, Color c2)
dynamic c;
ForegroundColor = c1;
ForegroundAccentColor = c2;

GetSelfAndChildrenRecursive(f).ToList().ForEach(x =>
{
c = x;
Expand Down Expand Up @@ -124,81 +100,48 @@ private static void SetTheme(Form f, Color c1, Color c2)

internal static void SaveSettings()
{
if (File.Exists(SettingsFile))
{
File.Delete(SettingsFile);

using (FileStream fs = File.Open(SettingsFile, FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;

JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, CurrentOptions);
}
}
string settingsFile = GetSettingsFilePath();
File.WriteAllText(settingsFile, JsonConvert.SerializeObject(CurrentOptions, Formatting.Indented));
}

internal static void LoadSettings()
{
if (!File.Exists(SettingsFile))
string settingsFile = GetSettingsFilePath();
if (File.Exists(settingsFile))
{
CurrentOptions.Color = Theme.Amethyst;
CurrentOptions.LowerCasing = false;
CurrentOptions.TrayIcon = false;
CurrentOptions.HighPriority = false;
CurrentOptions.CRC32Decimal = false;
CurrentOptions.ActiveHash = 1;
CurrentOptions.StayOnTop = false;
CurrentOptions.SingleInstance = true;

CurrentOptions.HashOptions.MD5 = false;
CurrentOptions.HashOptions.SHA1 = true;
CurrentOptions.HashOptions.SHA256 = true;
CurrentOptions.HashOptions.SHA384 = false;
CurrentOptions.HashOptions.SHA512 = false;
CurrentOptions.HashOptions.CRC32 = false;
CurrentOptions.HashOptions.RIPEMD160 = false;
CurrentOptions.HashOptions.SHA3_256 = false;
CurrentOptions.HashOptions.SHA3_384 = false;
CurrentOptions.HashOptions.SHA3_512 = false;

CurrentOptions.WindowLocation = null;
CurrentOptions.WindowSize = new Size(820, 632);
CurrentOptions.WindowState = FormWindowState.Normal;

using (FileStream fs = File.Open(SettingsFile, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(fs))
using (JsonWriter jw = new JsonTextWriter(sw))
{
jw.Formatting = Formatting.Indented;

JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, CurrentOptions);
}
CurrentOptions = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(settingsFile));
}
else
{
CurrentOptions = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(SettingsFile));

if (CurrentOptions.WindowSize.IsEmpty)
{
CurrentOptions.WindowSize = new Size(820, 632);
SaveSettings();
}
SaveSettings();
}

LoadTranslation();
}

private static string GetSettingsFilePath()
{
return Path.Combine(Application.StartupPath, "Hashing.json");
}

internal static void LoadTranslation()
{
// load proper translation list
if (CurrentOptions.LanguageCode == LanguageCode.EN) TranslationList = JObject.Parse(Properties.Resources.EN);
if (CurrentOptions.LanguageCode == LanguageCode.EL) TranslationList = JObject.Parse(Properties.Resources.EL);
if (CurrentOptions.LanguageCode == LanguageCode.CN) TranslationList = JObject.Parse(Properties.Resources.CN);
if (CurrentOptions.LanguageCode == LanguageCode.DE) TranslationList = JObject.Parse(Properties.Resources.DE);
var languageResources = new Dictionary<LanguageCode, string>
{
{ LanguageCode.EN, Properties.Resources.EN },
{ LanguageCode.EL, Properties.Resources.EL },
{ LanguageCode.CN, Properties.Resources.CN },
{ LanguageCode.DE, Properties.Resources.DE },
};

if (languageResources.TryGetValue(CurrentOptions.LanguageCode, out var resource))
{
TranslationList = JObject.Parse(resource);
}
else
{
TranslationList = JObject.Parse(Properties.Resources.EN);
}
}

}
}
Loading