Skip to content

Commit 5a39ea3

Browse files
committed
Allows to generate hash of text and removed some silly bug
1 parent df37f46 commit 5a39ea3

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

GenerateMd5Hash.v12.suo

-8.5 KB
Binary file not shown.

GenerateMd5Hash/GenerateMd5Hash.csproj

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<OutputType>Exe</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>GenerateMd5Hash</RootNamespace>
11-
<AssemblyName>GenerateMd5Hash</AssemblyName>
11+
<AssemblyName>md5</AssemblyName>
1212
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<PublishUrl>publish\</PublishUrl>
@@ -49,11 +49,8 @@
4949
</PropertyGroup>
5050
<ItemGroup>
5151
<Reference Include="System" />
52-
<Reference Include="System.Core" />
52+
<Reference Include="System.Drawing" />
5353
<Reference Include="System.Windows.Forms" />
54-
<Reference Include="System.Xml.Linq" />
55-
<Reference Include="System.Data.DataSetExtensions" />
56-
<Reference Include="Microsoft.CSharp" />
5754
<Reference Include="System.Data" />
5855
<Reference Include="System.Xml" />
5956
</ItemGroup>

GenerateMd5Hash/Program.cs

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,54 @@ class Program
1111
[STAThread]
1212
static void Main(string[] args)
1313
{
14-
#region Basic file- check
14+
var bIsFile = false;
15+
var text = String.Empty;
16+
17+
#region Basic checks...
1518

1619
if (args.Length <= 0)
1720
{
18-
Console.WriteLine("You really need to specify a file!");
21+
Console.WriteLine("You really need to specify a file/ string!");
22+
PrintHelp();
1923
Environment.Exit(401);
2024
}
2125

26+
if (args[0].StartsWith("--?"))
27+
{
28+
PrintHelp();
29+
Environment.Exit(40);
30+
}
31+
2232
if (!File.Exists(args[0]))
2333
{
24-
Console.WriteLine("File not found!");
25-
Environment.Exit(404);
34+
bIsFile = false;
35+
text = args[0].StartsWith("@") ? args[0].Substring(1) : args[0];
36+
}
37+
38+
else
39+
{
40+
bIsFile = true;
41+
2642
}
2743

2844
#endregion
2945

3046

31-
byte[] bHashBuffer;
47+
byte[] bHashBuffer = new byte[0];
3248

3349
using (var md5 = MD5.Create())
3450
{
51+
if (bIsFile)
52+
{
53+
using (var fs = File.OpenRead(args[0]))
54+
{
55+
bHashBuffer = md5.ComputeHash(fs);
56+
}
57+
}
3558

36-
using (var fs = File.OpenRead(args[0]))
59+
else
3760
{
38-
bHashBuffer = new byte[fs.Length];
39-
bHashBuffer = md5.ComputeHash(fs);
61+
bHashBuffer = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
4062
}
4163
}
4264

@@ -52,9 +74,42 @@ static void Main(string[] args)
5274
if (key.Key == ConsoleKey.Y)
5375
{
5476
Clipboard.SetText(strComputedHash.ToString());
55-
Console.WriteLine("Copied to Clipboard!");
77+
Console.WriteLine("\nCopied to Clipboard!");
5678
Console.WriteLine("");
5779
}
5880
}
81+
82+
static void PrintHelp()
83+
{
84+
const int iPosition = 30;
85+
86+
FillConsoleLineWithChar("-");
87+
Console.Write("| --help");
88+
Console.SetCursorPosition(iPosition, Console.CursorTop);
89+
Console.Write("Prints helptext");
90+
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop);
91+
Console.Write("|");
92+
FillConsoleLineWithChar("-");
93+
94+
Console.Write("| Generate md5sum of File:");
95+
Console.SetCursorPosition(iPosition, Console.CursorTop);
96+
Console.Write("[PATH_TO_FILE]");
97+
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop);
98+
Console.Write("|");
99+
Console.Write("| Generate md5sum of text:");
100+
Console.SetCursorPosition(iPosition, Console.CursorTop);
101+
Console.Write("@[TEXT]");
102+
Console.SetCursorPosition(Console.BufferWidth - 1, Console.CursorTop);
103+
Console.Write("|");
104+
FillConsoleLineWithChar("-");
105+
}
106+
107+
static void FillConsoleLineWithChar(string character)
108+
{
109+
for (var i = 0; i < Console.BufferWidth; i++)
110+
{
111+
Console.Write(character);
112+
}
113+
}
59114
}
60115
}

0 commit comments

Comments
 (0)